|
10 | 10 | */ |
11 | 11 | class CommandFactory { |
12 | 12 |
|
13 | | - public static function create( $name, $class, $parent ) { |
14 | | - $reflection = new \ReflectionClass( $class ); |
15 | | - |
16 | | - if ( $reflection->hasMethod( '__invoke' ) ) { |
17 | | - $command = self::create_subcommand( $parent, $name, $reflection->name, |
18 | | - $reflection->getMethod( '__invoke' ) ); |
| 13 | + /** |
| 14 | + * Creates a new composite command or subcommand |
| 15 | + * |
| 16 | + * @param [string] $name Name of command to create |
| 17 | + * @param [string] $class Name of class command belongs to |
| 18 | + * @param [RootCommand] $parent Parent command |
| 19 | + * @return [mixed] $command Either CompositeCommand or Subcommand |
| 20 | + */ |
| 21 | + public static function create($name, $class, $parent) { |
| 22 | + $reflection = new \ReflectionClass($class); |
| 23 | + |
| 24 | + if ($reflection->hasMethod('__invoke')) { |
| 25 | + $command = self::createSubcommand( |
| 26 | + $parent, |
| 27 | + $name, |
| 28 | + $reflection->name, |
| 29 | + $reflection->getMethod('__invoke') |
| 30 | + ); |
19 | 31 | } else { |
20 | | - $command = self::create_composite_command( $parent, $name, $reflection ); |
| 32 | + $command = self::createCompositeCommand($parent, $name, $reflection); |
21 | 33 | } |
22 | 34 |
|
23 | 35 | return $command; |
24 | 36 | } |
25 | 37 |
|
26 | | - private static function create_subcommand( $parent, $name, $class_name, $method ) { |
27 | | - $docparser = new DocParser( $method->getDocComment() ); |
28 | | - |
29 | | - if ( !$name ) |
30 | | - $name = $docparser->getTag( 'subcommand' ); |
31 | | - |
32 | | - if ( !$name ) |
33 | | - $name = $method->name; |
34 | | - |
35 | | - $method_name = $method->name; |
36 | | - |
37 | | - $when_invoked = function ( $args, $assoc_args ) use ( $class_name, $method_name ) { |
38 | | - call_user_func( array( new $class_name, $method_name ), $args, $assoc_args ); |
39 | | - }; |
40 | | - |
41 | | - return new Subcommand( $parent, $name, $docparser, $when_invoked ); |
42 | | - } |
43 | | - |
44 | | - private static function create_composite_command( $parent, $name, $reflection ) { |
45 | | - $docparser = new DocParser( $reflection->getDocComment() ); |
46 | | - |
47 | | - $container = new CompositeCommand( $parent, $name, $docparser ); |
48 | | - |
49 | | - foreach ( $reflection->getMethods() as $method ) { |
50 | | - if ( !self::is_good_method( $method ) ) |
| 38 | + /** |
| 39 | + * Creates a new composite command |
| 40 | + * |
| 41 | + * @param [RootCommand] $parent Parent command |
| 42 | + * @param [string] $name Name of command to create |
| 43 | + * @param [\ReflectionClass] $reflection Object with name of class to call |
| 44 | + * @return [CompositeCommand] $container |
| 45 | + */ |
| 46 | + private static function createCompositeCommand($parent, $name, $reflection) { |
| 47 | + $docparser = new DocParser($reflection->getDocComment()); |
| 48 | + $container = new CompositeCommand($parent, $name, $docparser); |
| 49 | + |
| 50 | + foreach ($reflection->getMethods() as $method) { |
| 51 | + if (!self::isGoodMethod($method)) { |
51 | 52 | continue; |
52 | | - |
53 | | - $subcommand = self::create_subcommand( $container, false, $reflection->name, $method ); |
54 | | - |
55 | | - $subcommand_name = $subcommand->get_name(); |
56 | | - |
57 | | - $container->add_subcommand( $subcommand_name, $subcommand ); |
| 53 | + } |
| 54 | + $subcommand = self::createSubcommand( |
| 55 | + $container, |
| 56 | + false, |
| 57 | + $reflection->name, |
| 58 | + $method |
| 59 | + ); |
| 60 | + $subcommand_name = $subcommand->getName(); |
| 61 | + $container->addSubcommand($subcommand_name, $subcommand); |
58 | 62 | } |
59 | 63 |
|
60 | 64 | return $container; |
61 | 65 | } |
62 | 66 |
|
63 | | - private static function is_good_method( $method ) { |
64 | | - return $method->isPublic() && !$method->isConstructor() && !$method->isStatic(); |
| 67 | + /** |
| 68 | + * Creates a new subcommand |
| 69 | + * |
| 70 | + * @param [RootCommand] $parent Parent command |
| 71 | + * @param [string] $name Name of command to create |
| 72 | + * @param [string] $class_name Name of class command belongs to |
| 73 | + * @param [string] $method Name of function to invoke in class |
| 74 | + * @return [Subcommand] $subcommand |
| 75 | + */ |
| 76 | + private static function createSubcommand( |
| 77 | + $parent, |
| 78 | + $name, |
| 79 | + $class_name, |
| 80 | + $method |
| 81 | + ) { |
| 82 | + $docparser = new DocParser($method->getDocComment()); |
| 83 | + if (!$name) { |
| 84 | + $name = $docparser->getTag('subcommand'); |
| 85 | + if (!$name) { |
| 86 | + $name = $method->name; |
| 87 | + } |
| 88 | + } |
| 89 | + $method_name = $method->name; |
| 90 | + $when_invoked = |
| 91 | + function ($args, $assoc_args) use ($class_name, $method_name) { |
| 92 | + call_user_func( |
| 93 | + array(new $class_name, $method_name), |
| 94 | + $args, |
| 95 | + $assoc_args |
| 96 | + ); |
| 97 | + }; |
| 98 | + |
| 99 | + $subcommand = new Subcommand($parent, $name, $docparser, $when_invoked); |
| 100 | + return $subcommand; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Decides if the given method is a good one |
| 105 | + * |
| 106 | + * @param [string] $method Name of method to be called |
| 107 | + * @return [boolean] $is_good_method |
| 108 | + */ |
| 109 | + private static function isGoodMethod($method) { |
| 110 | + $is_good_method = $method->isPublic() |
| 111 | + && !$method->isConstructor() |
| 112 | + && !$method->isStatic(); |
| 113 | + return $is_good_method; |
65 | 114 | } |
| 115 | + |
66 | 116 | } |
67 | 117 |
|
0 commit comments