Skip to content

Commit

Permalink
ConsoleExtension: More flexible helperSet and helpers loading, fix co…
Browse files Browse the repository at this point in the history
…llision between helper and helperSet definitions
  • Loading branch information
mabar authored and Milan Felix Šulc committed Jun 28, 2019
1 parent b1d1cc4 commit cfb1e92
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
],
"require": {
"php": "^7.2",
"nette/di": "~3.0.0",
"contributte/di": "^0.4.0",
"symfony/console": "^4.2.9"
},
"require-dev": {
Expand Down
43 changes: 23 additions & 20 deletions src/DI/ConsoleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
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;
Expand All @@ -15,7 +17,6 @@
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use Nette\Utils\Arrays;
use Nette\Utils\Strings;
use stdClass;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
Expand Down Expand Up @@ -49,8 +50,10 @@ public function getConfigSchema(): Schema
'version' => Expect::anyOf(Expect::string(), Expect::int(), Expect::float()),
'catchExceptions' => Expect::bool(),
'autoExit' => Expect::bool(),
'helperSet' => Expect::string(),
'helpers' => Expect::listOf('string'),
'helperSet' => Expect::anyOf(Expect::string(), Expect::array(), Expect::type(Statement::class)),
'helpers' => Expect::arrayOf(
Expect::anyOf(Expect::string(), Expect::array(), Expect::type(Statement::class))
),
'lazy' => Expect::bool(true),
]);
}
Expand All @@ -67,6 +70,7 @@ public function loadConfiguration(): void

$builder = $this->getContainerBuilder();
$config = $this->config;
$definitionsHelper = new ExtensionDefinitionsHelper($this->compiler);

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

if ($config->helperSet !== null) {
if (Strings::startsWith($config->helperSet, '@')) {
// Add already defined service
$applicationDef->addSetup('setHelperSet', [$config->helperSet]);
} else {
// Parse service definition
$helperSetDef = $builder->addDefinition($this->prefix('helperSet'))
->setFactory($config->helperSet);
$applicationDef->addSetup('setHelperSet', [$helperSetDef]);
$helperSetConfig = $config->helperSet;
$helperSetPrefix = $this->prefix('helperSet');
$helperSetDef = $definitionsHelper->getDefinitionFromConfig($helperSetConfig, $helperSetPrefix);

if ($helperSetDef instanceof Definition) {
$helperSetDef->setAutowired(false);
}

$applicationDef->addSetup('setHelperSet', [$helperSetDef]);
}

foreach ($config->helpers as $helperConfig) {
if (Strings::startsWith($helperConfig, '@')) {
// Add already defined service
$applicationDef->addSetup(new Statement('?->getHelperSet()->set(?)', ['@self', $helperConfig]));
} else {
// Parse service definition
$helperDef = $builder->addDefinition($this->prefix('helperSet'))
->setFactory($helperConfig);
$applicationDef->addSetup(new Statement('?->getHelperSet()->set(?)', ['@self', $helperDef]));
foreach ($config->helpers as $helperName => $helperConfig) {
$helperPrefix = $this->prefix('helper.' . $helperName);
$helperDef = $definitionsHelper->getDefinitionFromConfig($helperConfig, $helperPrefix);

if ($helperDef instanceof Definition) {
$helperDef->setAutowired(false);
}

$applicationDef->addSetup('?->getHelperSet()->set(?)', ['@self', $helperDef]);
}

if ($config->lazy) {
Expand Down

0 comments on commit cfb1e92

Please sign in to comment.