8
8
use SebastianBergmann \Environment \Runtime ;
9
9
10
10
/**
11
- * Class CommandRouter
11
+ * Parses $argv (or passed array) and attempts to find a command
12
+ * which is suitable for what was typed on the cli. It then executed the callable
13
+ * associated with that command definition.
14
+ *
12
15
* @package PhpSchool\PhpWorkshop
13
16
* @author Aydin Hassan <[email protected] >
14
17
*/
@@ -26,14 +29,20 @@ class CommandRouter
26
29
private $ defaultCommand ;
27
30
28
31
/**
29
- * @var \Interop\Container\ ContainerInterface
32
+ * @var ContainerInterface
30
33
*/
31
34
private $ container ;
32
35
33
36
/**
34
- * @param CommandDefinition[] $commands
35
- * @param $default
36
- * @param \Interop\Container\ContainerInterface $container
37
+ * Accepts an array of CommandDefinition's which represent the application. Also takes a default
38
+ * (name of one of the commands) which will be used if the workshop was invoked with no arguments.
39
+ *
40
+ * Also accepts an instance of the container so it can look for services in there which may by defined
41
+ * as the callable for one of the command definitions.
42
+ *
43
+ * @param CommandDefinition[] $commands An array of command definitions
44
+ * @param string $default The default command to use (if the workshop was invoked with no arguments)
45
+ * @param ContainerInterface $container An instance of the container
37
46
*/
38
47
public function __construct (array $ commands , $ default , ContainerInterface $ container )
39
48
{
@@ -61,6 +70,18 @@ private function addCommand(CommandDefinition $c)
61
70
}
62
71
63
72
/**
73
+ * Attempts to route the command. Parses $argv (or a given array), extracting the command name and
74
+ * arguments. Using the command name, the command definition is looked up.
75
+ *
76
+ * The number of arguments are validated against the required arguments for the command
77
+ * (specified by the definition)
78
+ *
79
+ * We get the callable from the command definition, or if it is the name of a service, we lookup the service
80
+ * in the container and validate that it is a callable.
81
+ *
82
+ * Finally, the callable is invoked with the arguments passed from the cli. The return value of
83
+ * callable is returned (if it is an integer, if not zero (success) is returned).
84
+ *
64
85
* @param array $args
65
86
* @return int
66
87
* @throws CliRouteNotExists
0 commit comments