@@ -31,41 +31,47 @@ function get(string $command, string $text) : Request|false {
3131 return new Request ($ args , $ cmd );
3232 }
3333
34- function loadMethodsByAttributes (object $ obj ) {
35- //TODO
34+ function loadMethods (object $ obj ) {
35+ $ objRef = new \ReflectionObject ($ obj );
36+ foreach ($ objRef ->getMethods (\ReflectionMethod::IS_PUBLIC ) as $ rf ) {
37+ $ this ->attrAddCmd ($ rf , [$ obj , $ rf ->name ]);
38+ }
39+ }
40+
41+ protected function attrAddCmd ($ rf , $ f ) {
42+ $ cmdAttr = $ rf ->getAttributes (attributes \Cmd::class);
43+ if (count ($ cmdAttr ) == 0 )
44+ return ;
45+ $ cmdAttr = $ cmdAttr [0 ]->newInstance ();
46+ $ syntaxAttr = $ rf ->getAttributes (attributes \Syntax::class);
47+ $ syntax = '' ;
48+ $ callWrapper = null ;
49+ $ callWrapperPre = [];
50+ $ callWrapperPost = [];
51+ if (isset ($ syntaxAttr [0 ])) {
52+ $ sa = $ syntaxAttr [0 ]->newInstance ();
53+ $ syntax = $ sa ->syntax ;
54+ }
55+ $ callWrapAttr = $ rf ->getAttributes (attributes \CallWrap::class);
56+ if (isset ($ callWrapAttr [0 ])) {
57+ $ cw = $ callWrapAttr [0 ]->newInstance ();
58+ $ callWrapper = $ cw ->caller ;
59+ $ callWrapperPre = $ cw ->preArgs ;
60+ $ callWrapperPost = $ cw ->postArgs ;
61+ }
62+ foreach ($ cmdAttr ->args as $ command ) {
63+ if ($ callWrapper != null )
64+ $ this ->cmds [$ command ] = new Cmd ($ command , $ callWrapper , [...$ callWrapperPre , $ f ], $ callWrapperPost , $ syntax );
65+ else
66+ $ this ->cmds [$ command ] = new Cmd ($ command , $ f , $ callWrapperPre , $ callWrapperPost , $ syntax );
67+ }
3668 }
3769
38- function loadFuncsByAttributes () {
70+ function loadFuncs () {
3971 $ funcs = get_defined_functions (true )["user " ];
4072 foreach ($ funcs as $ f ) {
4173 $ rf = new \ReflectionFunction ($ f );
42- //all commands should at least have Cmd attribute
43- $ cmdAttr = $ rf ->getAttributes (attributes \Cmd::class);
44- if (count ($ cmdAttr ) == 0 )
45- continue ;
46- $ cmdAttr = $ cmdAttr [0 ]->newInstance ();
47- $ syntaxAttr = $ rf ->getAttributes (attributes \Syntax::class);
48- $ syntax = '' ;
49- $ callWrapper = null ;
50- $ callWrapperPre = [];
51- $ callWrapperPost = [];
52- if (isset ($ syntaxAttr [0 ])) {
53- $ sa = $ syntaxAttr [0 ]->newInstance ();
54- $ syntax = $ sa ->syntax ;
55- }
56- $ callWrapAttr = $ rf ->getAttributes (attributes \CallWrap::class);
57- if (isset ($ callWrapAttr [0 ])) {
58- $ cw = $ callWrapAttr [0 ]->newInstance ();
59- $ callWrapper = $ cw ->caller ;
60- $ callWrapperPre = $ cw ->preArgs ;
61- $ callWrapperPost = $ cw ->postArgs ;
62- }
63- foreach ($ cmdAttr ->args as $ command ) {
64- if ($ callWrapper != null )
65- $ this ->cmds [$ command ] = new Cmd ($ command , $ callWrapper , [...$ callWrapperPre , $ f ], $ callWrapperPost , $ syntax );
66- else
67- $ this ->cmds [$ command ] = new Cmd ($ command , $ f , $ callWrapperPre , $ callWrapperPost , $ syntax );
68- }
74+ $ this ->attrAddCmd ($ rf , $ f );
6975 }
7076 }
7177
0 commit comments