Skip to content

Commit bee4e81

Browse files
mabarMilan Felix Šulc
authored andcommitted
ConsoleExtension: Simplify code
1 parent 1fbcb5b commit bee4e81

1 file changed

Lines changed: 31 additions & 37 deletions

File tree

src/DI/ConsoleExtension.php

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -66,63 +66,57 @@ public function loadConfiguration(): void
6666
}
6767

6868
$builder = $this->getContainerBuilder();
69-
$config = (array) $this->config;
69+
$config = $this->config;
7070

71-
$application = $builder->addDefinition($this->prefix('application'))
71+
$applicationDef = $builder->addDefinition($this->prefix('application'))
7272
->setFactory(Application::class);
7373

74-
if ($config['name'] !== null) {
75-
$application->addSetup('setName', [$config['name']]);
74+
if ($config->name !== null) {
75+
$applicationDef->addSetup('setName', [$config->name]);
7676
}
7777

78-
if ($config['version'] !== null) {
79-
$application->addSetup('setVersion', [$config['version']]);
78+
if ($config->version !== null) {
79+
$applicationDef->addSetup('setVersion', [$config->version]);
8080
}
8181

82-
if ($config['catchExceptions'] !== null) {
83-
$application->addSetup('setCatchExceptions', [(bool) $config['catchExceptions']]);
82+
if ($config->catchExceptions !== null) {
83+
$applicationDef->addSetup('setCatchExceptions', [$config->catchExceptions]);
8484
}
8585

86-
if ($config['autoExit'] !== null) {
87-
$application->addSetup('setAutoExit', [(bool) $config['autoExit']]);
86+
if ($config->autoExit !== null) {
87+
$applicationDef->addSetup('setAutoExit', [$config->autoExit]);
8888
}
8989

90-
if ($config['helperSet'] !== null) {
91-
if (is_string($config['helperSet']) && Strings::startsWith($config['helperSet'], '@')) {
90+
if ($config->helperSet !== null) {
91+
if (Strings::startsWith($config->helperSet, '@')) {
9292
// Add already defined service
93-
$application->addSetup('setHelperSet', [$config['helperSet']]);
94-
} elseif (is_string($config['helperSet'])) {
93+
$applicationDef->addSetup('setHelperSet', [$config->helperSet]);
94+
} else {
9595
// Parse service definition
9696
$helperSetDef = $builder->addDefinition($this->prefix('helperSet'))
97-
->setFactory($config['helperSet']);
98-
$application->addSetup('setHelperSet', [$helperSetDef]);
99-
} else {
100-
throw new ServiceCreationException(sprintf('Unsupported definition of helperSet'));
97+
->setFactory($config->helperSet);
98+
$applicationDef->addSetup('setHelperSet', [$helperSetDef]);
10199
}
102100
}
103101

104-
if ($config['helpers']) {
105-
foreach ($config['helpers'] as $n => $helper) {
106-
if (is_string($helper) && Strings::startsWith($helper, '@')) {
107-
// Add already defined service
108-
$application->addSetup(new Statement('$service->getHelperSet()->set(?)', [$helper]));
109-
} elseif (is_string($helper)) {
110-
// Parse service definition
111-
$helperDef = $builder->addDefinition($this->prefix('helperSet'))
112-
->setFactory($helper);
113-
$application->addSetup(new Statement('$service->getHelperSet()->set(?)', [$helperDef]));
114-
} else {
115-
throw new ServiceCreationException(sprintf('Unsupported definition of helper'));
116-
}
102+
foreach ($config->helpers as $helperConfig) {
103+
if (Strings::startsWith($helperConfig, '@')) {
104+
// Add already defined service
105+
$applicationDef->addSetup(new Statement('?->getHelperSet()->set(?)', ['@self', $helperConfig]));
106+
} else {
107+
// Parse service definition
108+
$helperDef = $builder->addDefinition($this->prefix('helperSet'))
109+
->setFactory($helperConfig);
110+
$applicationDef->addSetup(new Statement('?->getHelperSet()->set(?)', ['@self', $helperDef]));
117111
}
118112
}
119113

120-
if ($config['lazy'] === true) {
114+
if ($config->lazy) {
121115
$builder->addDefinition($this->prefix('commandLoader'))
122116
->setType(CommandLoaderInterface::class)
123117
->setFactory(ContainerCommandLoader::class);
124118

125-
$application->addSetup('setCommandLoader', ['@' . $this->prefix('commandLoader')]);
119+
$applicationDef->addSetup('setCommandLoader', ['@' . $this->prefix('commandLoader')]);
126120
}
127121
}
128122

@@ -137,23 +131,23 @@ public function beforeCompile(): void
137131
}
138132

139133
$builder = $this->getContainerBuilder();
140-
$config = (array) $this->config;
134+
$config = $this->config;
141135

142136
/** @var ServiceDefinition $applicationDef */
143137
$applicationDef = $builder->getDefinition($this->prefix('application'));
144138

145139
// Setup URL for CLI
146-
if ($builder->hasDefinition('http.request') && $config['url'] !== null) {
140+
if ($config->url !== null && $builder->hasDefinition('http.request')) {
147141
/** @var ServiceDefinition $httpDef */
148142
$httpDef = $builder->getDefinition('http.request');
149-
$httpDef->setFactory(Request::class, [new Statement(UrlScript::class, [$config['url']])]);
143+
$httpDef->setFactory(Request::class, [new Statement(UrlScript::class, [$config->url])]);
150144
}
151145

152146
// Register all commands (if they are not lazy-loaded)
153147
// otherwise build a command map for command loader
154148
$commands = $builder->findByType(Command::class);
155149

156-
if ($config['lazy'] === false) {
150+
if (!$config->lazy) {
157151
// Iterate over all commands and add to console
158152
foreach ($commands as $serviceName => $service) {
159153
$applicationDef->addSetup('add', [$service]);

0 commit comments

Comments
 (0)