-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
ConsoleExtension.php
212 lines (177 loc) · 6.21 KB
/
ConsoleExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php declare(strict_types = 1);
namespace Contributte\Console\DI;
use Contributte\Console\Application;
use Contributte\Console\CommandLoader\ContainerCommandLoader;
use Contributte\Console\Exception\Logical\InvalidArgumentException;
use Contributte\DI\Helper\ExtensionDefinitionsHelper;
use Nette\DI\CompilerExtension;
use Nette\DI\Definitions\Definition;
use Nette\DI\Definitions\ServiceDefinition;
use Nette\DI\Definitions\Statement;
use Nette\DI\MissingServiceException;
use Nette\DI\ServiceCreationException;
use Nette\Http\Request;
use Nette\Http\UrlScript;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use Nette\Utils\Arrays;
use stdClass;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* @property-read stdClass $config
*/
class ConsoleExtension extends CompilerExtension
{
public const COMMAND_TAG = 'console.command';
/** @var bool */
private $cliMode;
public function __construct(bool $cliMode = false)
{
if (func_num_args() <= 0) {
throw new InvalidArgumentException(sprintf('Provide CLI mode, e.q. %s(%%consoleMode%%).', self::class));
}
$this->cliMode = $cliMode;
}
public function getConfigSchema(): Schema
{
return Expect::structure([
'url' => Expect::anyOf(Expect::string(), Expect::null())->dynamic(),
'name' => Expect::string()->dynamic(),
'version' => Expect::anyOf(Expect::string(), Expect::int(), Expect::float()),
'catchExceptions' => Expect::bool(),
'autoExit' => Expect::bool(),
'helperSet' => Expect::anyOf(Expect::string(), Expect::type(Statement::class)),
'helpers' => Expect::arrayOf(
Expect::anyOf(Expect::string(), Expect::array(), Expect::type(Statement::class))
),
'lazy' => Expect::bool(true),
]);
}
/**
* Register services
*/
public function loadConfiguration(): void
{
// Skip if isn't CLI
if ($this->cliMode !== true) {
return;
}
$builder = $this->getContainerBuilder();
$config = $this->config;
$defhelp = new ExtensionDefinitionsHelper($this->compiler);
// Register Symfony Console Application
$applicationDef = $builder->addDefinition($this->prefix('application'))
->setFactory(Application::class);
// Setup console name
if ($config->name !== null) {
$applicationDef->addSetup('setName', [$config->name]);
}
// Setup console version
if ($config->version !== null) {
$applicationDef->addSetup('setVersion', [(string) $config->version]);
}
// Catch or populate exceptions
if ($config->catchExceptions !== null) {
$applicationDef->addSetup('setCatchExceptions', [$config->catchExceptions]);
}
// Call die() or not
if ($config->autoExit !== null) {
$applicationDef->addSetup('setAutoExit', [$config->autoExit]);
}
// Register given or default HelperSet
if ($config->helperSet !== null) {
$applicationDef->addSetup('setHelperSet', [
$defhelp->getDefinitionFromConfig($config->helperSet, $this->prefix('helperSet')),
]);
}
// Register extra helpers
foreach ($config->helpers as $helperName => $helperConfig) {
$helperPrefix = $this->prefix('helper.' . $helperName);
$helperDef = $defhelp->getDefinitionFromConfig($helperConfig, $helperPrefix);
if ($helperDef instanceof Definition) {
$helperDef->setAutowired(false);
}
$applicationDef->addSetup('?->getHelperSet()->set(?)', ['@self', $helperDef]);
}
// Commands lazy loading
if ($config->lazy) {
$builder->addDefinition($this->prefix('commandLoader'))
->setType(CommandLoaderInterface::class)
->setFactory(ContainerCommandLoader::class);
$applicationDef->addSetup('setCommandLoader', ['@' . $this->prefix('commandLoader')]);
}
// Export types
$this->compiler->addExportedType(Application::class);
}
/**
* Decorate services
*/
public function beforeCompile(): void
{
// Skip if isn't CLI
if ($this->cliMode !== true) {
return;
}
$builder = $this->getContainerBuilder();
$config = $this->config;
/** @var ServiceDefinition $applicationDef */
$applicationDef = $builder->getDefinition($this->prefix('application'));
// Setup URL for CLI
if ($config->url !== null && $builder->hasDefinition('http.request')) {
/** @var ServiceDefinition $httpDef */
$httpDef = $builder->getDefinition('http.request');
$httpDef->setFactory(Request::class, [new Statement(UrlScript::class, [$config->url])]);
}
// Register all commands (if they are not lazy-loaded)
// otherwise build a command map for command loader
$commands = $builder->findByType(Command::class);
if (!$config->lazy) {
// Iterate over all commands and add to console
foreach ($commands as $serviceName => $service) {
$applicationDef->addSetup('add', [$service]);
}
} else {
$commandMap = [];
// Iterate over all commands and build commandMap
foreach ($commands as $serviceName => $service) {
$tags = $service->getTags();
$entry = ['name' => null, 'alias' => null];
if (isset($tags[self::COMMAND_TAG])) {
// Parse tag's name attribute
if (is_string($tags[self::COMMAND_TAG])) {
$entry['name'] = $tags[self::COMMAND_TAG];
} elseif (is_array($tags[self::COMMAND_TAG])) {
$entry['name'] = Arrays::get($tags[self::COMMAND_TAG], 'name', null);
}
} else {
// Parse it from static property
$entry['name'] = call_user_func([$service->getType(), 'getDefaultName']);
}
// Validate command name
if (!isset($entry['name'])) {
throw new ServiceCreationException(
sprintf(
'Command "%s" missing tag "%s[name]" or variable "$defaultName".',
$service->getType(),
self::COMMAND_TAG
)
);
}
// Append service to command map
$commandMap[$entry['name']] = $serviceName;
}
/** @var ServiceDefinition $commandLoaderDef */
$commandLoaderDef = $builder->getDefinition($this->prefix('commandLoader'));
$commandLoaderDef->getFactory()->arguments = ['@container', $commandMap];
}
// Register event dispatcher, if available
try {
$dispatcherDef = $builder->getDefinitionByType(EventDispatcherInterface::class);
$applicationDef->addSetup('setDispatcher', [$dispatcherDef]);
} catch (MissingServiceException $e) {
// Event dispatcher is not installed, ignore
}
}
}