Skip to content

Commit cfb1e92

Browse files
mabarMilan Felix Šulc
authored andcommitted
ConsoleExtension: More flexible helperSet and helpers loading, fix collision between helper and helperSet definitions
1 parent b1d1cc4 commit cfb1e92

2 files changed

Lines changed: 24 additions & 21 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": "^7.2",
20-
"nette/di": "~3.0.0",
20+
"contributte/di": "^0.4.0",
2121
"symfony/console": "^4.2.9"
2222
},
2323
"require-dev": {

src/DI/ConsoleExtension.php

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use Contributte\Console\Application;
66
use Contributte\Console\CommandLoader\ContainerCommandLoader;
77
use Contributte\Console\Exception\Logical\InvalidArgumentException;
8+
use Contributte\DI\Helper\ExtensionDefinitionsHelper;
89
use Nette\DI\CompilerExtension;
10+
use Nette\DI\Definitions\Definition;
911
use Nette\DI\Definitions\ServiceDefinition;
1012
use Nette\DI\Definitions\Statement;
1113
use Nette\DI\MissingServiceException;
@@ -15,7 +17,6 @@
1517
use Nette\Schema\Expect;
1618
use Nette\Schema\Schema;
1719
use Nette\Utils\Arrays;
18-
use Nette\Utils\Strings;
1920
use stdClass;
2021
use Symfony\Component\Console\Command\Command;
2122
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
@@ -49,8 +50,10 @@ public function getConfigSchema(): Schema
4950
'version' => Expect::anyOf(Expect::string(), Expect::int(), Expect::float()),
5051
'catchExceptions' => Expect::bool(),
5152
'autoExit' => Expect::bool(),
52-
'helperSet' => Expect::string(),
53-
'helpers' => Expect::listOf('string'),
53+
'helperSet' => Expect::anyOf(Expect::string(), Expect::array(), Expect::type(Statement::class)),
54+
'helpers' => Expect::arrayOf(
55+
Expect::anyOf(Expect::string(), Expect::array(), Expect::type(Statement::class))
56+
),
5457
'lazy' => Expect::bool(true),
5558
]);
5659
}
@@ -67,6 +70,7 @@ public function loadConfiguration(): void
6770

6871
$builder = $this->getContainerBuilder();
6972
$config = $this->config;
73+
$definitionsHelper = new ExtensionDefinitionsHelper($this->compiler);
7074

7175
$applicationDef = $builder->addDefinition($this->prefix('application'))
7276
->setFactory(Application::class);
@@ -88,27 +92,26 @@ public function loadConfiguration(): void
8892
}
8993

9094
if ($config->helperSet !== null) {
91-
if (Strings::startsWith($config->helperSet, '@')) {
92-
// Add already defined service
93-
$applicationDef->addSetup('setHelperSet', [$config->helperSet]);
94-
} else {
95-
// Parse service definition
96-
$helperSetDef = $builder->addDefinition($this->prefix('helperSet'))
97-
->setFactory($config->helperSet);
98-
$applicationDef->addSetup('setHelperSet', [$helperSetDef]);
95+
$helperSetConfig = $config->helperSet;
96+
$helperSetPrefix = $this->prefix('helperSet');
97+
$helperSetDef = $definitionsHelper->getDefinitionFromConfig($helperSetConfig, $helperSetPrefix);
98+
99+
if ($helperSetDef instanceof Definition) {
100+
$helperSetDef->setAutowired(false);
99101
}
102+
103+
$applicationDef->addSetup('setHelperSet', [$helperSetDef]);
100104
}
101105

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]));
106+
foreach ($config->helpers as $helperName => $helperConfig) {
107+
$helperPrefix = $this->prefix('helper.' . $helperName);
108+
$helperDef = $definitionsHelper->getDefinitionFromConfig($helperConfig, $helperPrefix);
109+
110+
if ($helperDef instanceof Definition) {
111+
$helperDef->setAutowired(false);
111112
}
113+
114+
$applicationDef->addSetup('?->getHelperSet()->set(?)', ['@self', $helperDef]);
112115
}
113116

114117
if ($config->lazy) {

0 commit comments

Comments
 (0)