Skip to content

Commit 9621e09

Browse files
committed
Command* docs
1 parent 107f243 commit 9621e09

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

src/CommandDefinition.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
namespace PhpSchool\PhpWorkshop;
44

55
/**
6-
* Class CommandDefinition
6+
* Represents a command in the workshop framework. Simply consists of a
7+
* command name, required arguments and either a service name of callable to
8+
* execute when the command is run.
9+
*
710
* @package PhpSchool\PhpWorkshop
811
* @author Aydin Hassan <[email protected]>
912
*/
@@ -15,7 +18,7 @@ class CommandDefinition
1518
private $name;
1619

1720
/**
18-
* @var array
21+
* @var string[]
1922
*/
2023
private $args;
2124

@@ -25,11 +28,9 @@ class CommandDefinition
2528
private $commandCallable;
2629

2730
/**
28-
* CommandDefinition constructor.
29-
*
30-
* @param string $name
31-
* @param array $args
32-
* @param string|callable $commandCallable
31+
* @param string $name The name of the command (this is how the student would invoke the command from the cli)
32+
* @param string[] $args A list of required arguments. This must be an array of strings.
33+
* @param string|callable $commandCallable The name of a callable container entry or an actual PHP callable.
3334
*/
3435
public function __construct($name, array $args, $commandCallable)
3536
{
@@ -39,6 +40,8 @@ public function __construct($name, array $args, $commandCallable)
3940
}
4041

4142
/**
43+
* Get the name of the command
44+
*
4245
* @return string
4346
*/
4447
public function getName()
@@ -47,6 +50,8 @@ public function getName()
4750
}
4851

4952
/**
53+
* Get the list of required arguments
54+
*
5055
* @return array
5156
*/
5257
public function getRequiredArgs()
@@ -55,6 +60,8 @@ public function getRequiredArgs()
5560
}
5661

5762
/**
63+
* Get the callable associated with this command
64+
*
5865
* @return string|callable
5966
*/
6067
public function getCommandCallable()

src/CommandRouter.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
use SebastianBergmann\Environment\Runtime;
99

1010
/**
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+
*
1215
* @package PhpSchool\PhpWorkshop
1316
* @author Aydin Hassan <[email protected]>
1417
*/
@@ -26,14 +29,20 @@ class CommandRouter
2629
private $defaultCommand;
2730

2831
/**
29-
* @var \Interop\Container\ContainerInterface
32+
* @var ContainerInterface
3033
*/
3134
private $container;
3235

3336
/**
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
3746
*/
3847
public function __construct(array $commands, $default, ContainerInterface $container)
3948
{
@@ -61,6 +70,18 @@ private function addCommand(CommandDefinition $c)
6170
}
6271

6372
/**
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+
*
6485
* @param array $args
6586
* @return int
6687
* @throws CliRouteNotExists

0 commit comments

Comments
 (0)