|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Console; |
|
13
|
|
|
|
|
14
|
|
|
use Psr\Container\ContainerInterface; |
|
15
|
|
|
use Spiral\Console\Traits\HelpersTrait; |
|
16
|
|
|
use Spiral\Core\Container; |
|
17
|
|
|
use Spiral\Core\Exception\ScopeException; |
|
18
|
|
|
use Spiral\Core\ResolverInterface; |
|
19
|
|
|
use Symfony\Component\Console\Command\Command as SymfonyCommand; |
|
20
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Provides automatic command configuration and access to global container scope. |
|
25
|
|
|
*/ |
|
26
|
|
|
abstract class Command extends SymfonyCommand |
|
27
|
|
|
{ |
|
28
|
|
|
use HelpersTrait; |
|
29
|
|
|
|
|
30
|
|
|
// Command name. |
|
31
|
|
|
protected const NAME = ''; |
|
32
|
|
|
|
|
33
|
|
|
// Short command description. |
|
34
|
|
|
protected const DESCRIPTION = ''; |
|
35
|
|
|
|
|
36
|
|
|
// Command options specified in Symphony format. For more complex definitions redefine |
|
37
|
|
|
// getOptions() method. |
|
38
|
|
|
protected const OPTIONS = []; |
|
39
|
|
|
|
|
40
|
|
|
// Command arguments specified in Symphony format. For more complex definitions redefine |
|
41
|
|
|
// getArguments() method. |
|
42
|
|
|
protected const ARGUMENTS = []; |
|
43
|
|
|
|
|
44
|
|
|
/** @var Container */ |
|
45
|
|
|
protected $container; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param ContainerInterface $container |
|
49
|
|
|
*/ |
|
50
|
|
|
public function setContainer(ContainerInterface $container): void |
|
51
|
|
|
{ |
|
52
|
|
|
$this->container = $container; |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
* |
|
58
|
|
|
* Pass execution to "perform" method using container to resolve method dependencies. |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
61
|
|
|
{ |
|
62
|
|
|
if ($this->container === null) { |
|
63
|
|
|
throw new ScopeException('Container is not set'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$reflection = new \ReflectionMethod($this, 'perform'); |
|
67
|
|
|
$reflection->setAccessible(true); |
|
68
|
|
|
|
|
69
|
|
|
/** @var ResolverInterface $resolver */ |
|
70
|
|
|
$resolver = $this->container->get(ResolverInterface::class); |
|
71
|
|
|
|
|
72
|
|
|
try { |
|
73
|
|
|
list($this->input, $this->output) = [$input, $output]; |
|
74
|
|
|
|
|
75
|
|
|
//Executing perform method with method injection |
|
76
|
|
|
return (int)$reflection->invokeArgs($this, $resolver->resolveArguments( |
|
77
|
|
|
$reflection, |
|
78
|
|
|
compact('input', 'output') |
|
79
|
|
|
)); |
|
80
|
|
|
} finally { |
|
81
|
|
|
[$this->input, $this->output] = [null, null]; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Configures the command. |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function configure(): void |
|
89
|
|
|
{ |
|
90
|
|
|
$this->setName(static::NAME); |
|
91
|
|
|
$this->setDescription(static::DESCRIPTION); |
|
92
|
|
|
|
|
93
|
|
|
foreach ($this->defineOptions() as $option) { |
|
94
|
|
|
call_user_func_array([$this, 'addOption'], $option); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
foreach ($this->defineArguments() as $argument) { |
|
98
|
|
|
call_user_func_array([$this, 'addArgument'], $argument); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Define command options. |
|
104
|
|
|
* |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function defineOptions(): array |
|
108
|
|
|
{ |
|
109
|
|
|
return static::OPTIONS; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Define command arguments. |
|
114
|
|
|
* |
|
115
|
|
|
* @return array |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function defineArguments(): array |
|
118
|
|
|
{ |
|
119
|
|
|
return static::ARGUMENTS; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.