11<?php namespace web \frontend ;
22
3- use lang \IllegalArgumentException ;
3+ use lang \reflection \Method ;
4+ use lang \{IllegalArgumentException , Reflection };
45use web \frontend \View ;
5- use lang \reflect \Method ;
66
77class Delegate {
88 private static $ SOURCES ;
@@ -25,11 +25,11 @@ static function __static() {
2525 * Creates a new delegate
2626 *
2727 * @param object $instance
28- * @param string|lang.reflect .Method $method
28+ * @param string|lang.reflection .Method $method
2929 */
3030 public function __construct ($ instance , $ method ) {
3131 $ this ->instance = $ instance ;
32- $ this ->method = $ method instanceof Method ? $ method : typeof ($ instance )->getMethod ($ method );
32+ $ this ->method = $ method instanceof Method ? $ method : Reflection:: type ($ instance )->method ($ method );
3333 }
3434
3535 /** @return string */
@@ -40,7 +40,7 @@ public function group() {
4040
4141 /** @return string */
4242 public function name () {
43- return nameof ($ this ->instance ).':: ' .$ this ->method ->getName ();
43+ return nameof ($ this ->instance ).':: ' .$ this ->method ->name ();
4444 }
4545
4646 /**
@@ -52,26 +52,28 @@ public function name() {
5252 public function parameters () {
5353 if (null === $ this ->parameters ) {
5454 $ this ->parameters = [];
55- foreach ($ this ->method ->getParameters () as $ param ) {
56- if ($ annotations = $ param ->getAnnotations ()) {
57- foreach ($ annotations as $ from => $ value ) {
58- $ source = self ::$ SOURCES [$ from ] ?? self ::$ SOURCES ['default ' ];
59- }
55+ foreach ($ this ->method ->parameters () as $ param ) {
56+
57+ // Check for parameter annotations...
58+ foreach ($ param ->annotations () as $ annotation ) {
59+ $ source = self ::$ SOURCES [$ annotation ->name ()] ?? self ::$ SOURCES ['default ' ];
60+ $ name = $ annotation ->argument (0 ) ?? $ param ->name ();
6061
61- $ name = null === $ value ? $ param ->getName () : $ value ;
62- if ($ param ->isOptional ()) {
63- $ default = $ param ->getDefaultValue ();
64- $ this ->parameters [$ name ]= function ($ req , $ name ) use ($ source , $ default ) {
65- return $ source ($ req , $ name ) ?? $ default ;
62+ if ($ param ->optional ()) {
63+ $ this ->parameters [$ name ]= function ($ req , $ name ) use ($ source , $ param ) {
64+ return $ source ($ req , $ name ) ?? $ param ->default ();
6665 };
6766 } else {
6867 $ this ->parameters [$ name ]= $ source ;
6968 }
70- } else {
71- $ this ->parameters [$ param ->getName ()]= self ::$ SOURCES ['segment ' ];
69+ continue 2 ;
7270 }
71+
72+ // ...falling back to selecting the parameter from the segments
73+ $ this ->parameters [$ param ->name ()]= self ::$ SOURCES ['segment ' ];
7374 }
7475 }
76+
7577 return $ this ->parameters ;
7678 }
7779
@@ -80,7 +82,7 @@ public function parameters() {
8082 *
8183 * @param var[] $args
8284 * @return web.frontend.View
83- * @throws lang.reflect.TargetInvocationException
85+ * @throws lang.reflection.TargetException
8486 */
8587 public function invoke ($ args ) {
8688 $ result = $ this ->method ->invoke ($ this ->instance , $ args );
0 commit comments