1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* spiral |
4
|
|
|
* |
5
|
|
|
* @author Wolfy-J |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Spiral\Console; |
9
|
|
|
|
10
|
|
|
use Spiral\Console\Traits\HelpersTrait; |
11
|
|
|
use Interop\Container\ContainerInterface; |
12
|
|
|
use Spiral\Core\ResolverInterface; |
13
|
|
|
use Spiral\Core\Traits\SharedTrait; |
14
|
|
|
use Symfony\Component\Console\Command\Command as SymfonyCommand; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Basic application command class. Implements method injections and simplified access to |
20
|
|
|
* container bindings. |
21
|
|
|
*/ |
22
|
|
|
abstract class Command extends SymfonyCommand |
23
|
|
|
{ |
24
|
|
|
use SharedTrait, HelpersTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Command name. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
const NAME = ''; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Short command description. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
const DESCRIPTION = ''; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Command options specified in Symphony format. For more complex definitions redefine |
42
|
|
|
* getOptions() method. |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
const OPTIONS = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Command arguments specified in Symphony format. For more complex definitions redefine |
50
|
|
|
* getArguments() method. |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
const ARGUMENTS = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var ContainerInterface |
58
|
|
|
*/ |
59
|
|
|
protected $container = null; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param ContainerInterface $container |
63
|
|
|
*/ |
64
|
|
|
public function __construct(ContainerInterface $container) |
65
|
|
|
{ |
66
|
|
|
$this->container = $container; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Configuring command. |
70
|
|
|
*/ |
71
|
|
|
parent::__construct(static::NAME); |
72
|
|
|
$this->setDescription(static::DESCRIPTION); |
73
|
|
|
|
74
|
|
|
foreach ($this->defineOptions() as $option) { |
75
|
|
|
call_user_func_array([$this, 'addOption'], $option); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
foreach ($this->defineArguments() as $argument) { |
79
|
|
|
call_user_func_array([$this, 'addArgument'], $argument); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function run(InputInterface $input, OutputInterface $output) |
87
|
|
|
{ |
88
|
|
|
try { |
89
|
|
|
list($this->input, $this->output) = [$input, $output]; |
90
|
|
|
|
91
|
|
|
return parent::run($input, $output); |
92
|
|
|
} finally { |
93
|
|
|
//Scope end |
94
|
|
|
$this->input = $this->output = null; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
* |
101
|
|
|
* Pass execution to "perform" method using container to resolve method dependencies. |
102
|
|
|
*/ |
103
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
104
|
|
|
{ |
105
|
|
|
$reflection = new \ReflectionMethod($this, 'perform'); |
106
|
|
|
$reflection->setAccessible(true); |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @var ResolverInterface $resolver |
110
|
|
|
*/ |
111
|
|
|
$resolver = $this->container->get(ResolverInterface::class); |
112
|
|
|
|
113
|
|
|
//Executing perform method with method injection |
114
|
|
|
return $reflection->invokeArgs( |
115
|
|
|
$this, |
116
|
|
|
$resolver->resolveArguments($reflection, compact('input', 'output')) |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Define command options. |
122
|
|
|
* |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
protected function defineOptions(): array |
126
|
|
|
{ |
127
|
|
|
return static::OPTIONS; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Define command arguments. |
132
|
|
|
* |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
protected function defineArguments(): array |
136
|
|
|
{ |
137
|
|
|
return static::ARGUMENTS; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return ContainerInterface |
142
|
|
|
*/ |
143
|
|
|
protected function iocContainer() |
144
|
|
|
{ |
145
|
|
|
//We have to always be executed in a container scope |
146
|
|
|
return $this->container; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Check if verbosity level of output is higher or equal to VERBOSITY_VERBOSE. |
151
|
|
|
* |
152
|
|
|
* @return bool |
153
|
|
|
*/ |
154
|
|
|
protected function isVerbosity(): bool |
155
|
|
|
{ |
156
|
|
|
return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE; |
157
|
|
|
} |
158
|
|
|
} |