1
|
|
|
<?php |
2
|
|
|
namespace Consolidation\AnnotatedCommand; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Prepare parameter list for execurion. Handle injection of any |
6
|
|
|
* special values (e.g. $input and $output) into the parameter list. |
7
|
|
|
*/ |
8
|
|
|
class ParameterInjection implements ParameterInjector |
9
|
|
|
{ |
10
|
|
|
public function __construct() |
11
|
|
|
{ |
12
|
|
|
$this->register('Symfony\Component\Console\Input\InputInterface', $this); |
13
|
|
|
$this->register('Symfony\Component\Console\Output\OutputInterface', $this); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function register($interfaceName, ParameterInjector $injector) |
17
|
|
|
{ |
18
|
|
|
$this->injectors[$interfaceName] = $injector; |
|
|
|
|
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function args($commandData) |
22
|
|
|
{ |
23
|
|
|
return array_merge( |
24
|
|
|
$commandData->injectedInstances(), |
25
|
|
|
$commandData->getArgsAndOptions() |
26
|
|
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function injectIntoCommandData($commandData, $injectedClasses) |
30
|
|
|
{ |
31
|
|
|
foreach ($injectedClasses as $injectedClass) { |
32
|
|
|
$injectedInstance = $this->getInstanceToInject($commandData, $injectedClass); |
33
|
|
|
$commandData->injectInstance($injectedInstance); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function getInstanceToInject(CommandData $commandData, $interfaceName) |
38
|
|
|
{ |
39
|
|
|
if (!isset($this->injectors[$interfaceName])) { |
40
|
|
|
return null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $this->injectors[$interfaceName]->get($commandData, $interfaceName); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function get(CommandData $commandData, $interfaceName) |
47
|
|
|
{ |
48
|
|
|
switch ($interfaceName) { |
49
|
|
|
case 'Symfony\Component\Console\Input\InputInterface': |
50
|
|
|
return $commandData->input(); |
51
|
|
|
case 'Symfony\Component\Console\Output\OutputInterface': |
52
|
|
|
return $commandData->output(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return null; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: