Skip to content

Commit ebcac95

Browse files
committed
Bugfix: nullable helperSet
1 parent 1df5f53 commit ebcac95

3 files changed

Lines changed: 39 additions & 16 deletions

File tree

.docs/README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ console:
3131
autoExit: true / false
3232
url: https://contributte.com
3333
lazy: false
34-
helperSet: @customHelperSet
3534
```
3635

3736
In SAPI (CLI) mode there is no http request and thus no URL address. This is an inconvenience you have to solve by yourself - via the `console.url` option.
@@ -41,7 +40,7 @@ console:
4140
url: https://contributte.org
4241
```
4342

44-
### HelperSet
43+
### Helpers
4544

4645
You could also define you custom `helperSet` just in case. There are 2 possible approaches. You can register your
4746
`App\Model\MyCustomHelperSet` as a service under the `services` section or provide it directly to the extension config `helperSet`.
@@ -63,8 +62,7 @@ console:
6362
helperSet: App\Model\MyCustomHelperSet
6463
```
6564

66-
By default, helperSet contains 5 helpers - 4 defined in `Symfony\Component\Console\Application` and 1 defined
67-
by the extension itself. You can add more helpers, if needed.
65+
By default, helperSet contains 4 helpers defined in `Symfony\Component\Console\Application`. You can add more helpers, if need them.
6866

6967
```yaml
7068
console:

src/DI/ConsoleExtension.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Nette\Http\UrlScript;
1717
use Nette\Schema\Expect;
1818
use Nette\Schema\Schema;
19+
use Nette\Schema\ValidationException;
1920
use Nette\Utils\Arrays;
2021
use stdClass;
2122
use Symfony\Component\Console\Command\Command;
@@ -50,7 +51,14 @@ public function getConfigSchema(): Schema
5051
'version' => Expect::anyOf(Expect::string(), Expect::int(), Expect::float()),
5152
'catchExceptions' => Expect::bool(),
5253
'autoExit' => Expect::bool(),
53-
'helperSet' => Expect::anyOf(Expect::string(), Expect::array(), Expect::type(Statement::class)),
54+
'helperSet' => Expect::anyOf(Expect::string(), Expect::type(Statement::class))
55+
->assert(function ($helperSet) {
56+
if ($helperSet === null) {
57+
throw new ValidationException('helperSet cannot be null');
58+
}
59+
60+
return true;
61+
}),
5462
'helpers' => Expect::arrayOf(
5563
Expect::anyOf(Expect::string(), Expect::array(), Expect::type(Statement::class))
5664
),
@@ -70,42 +78,43 @@ public function loadConfiguration(): void
7078

7179
$builder = $this->getContainerBuilder();
7280
$config = $this->config;
73-
$definitionsHelper = new ExtensionDefinitionsHelper($this->compiler);
81+
$defhelp = new ExtensionDefinitionsHelper($this->compiler);
7482

83+
// Register Symfony Console Application
7584
$applicationDef = $builder->addDefinition($this->prefix('application'))
7685
->setFactory(Application::class);
7786

87+
// Setup console name
7888
if ($config->name !== null) {
7989
$applicationDef->addSetup('setName', [$config->name]);
8090
}
8191

92+
// Setup console version
8293
if ($config->version !== null) {
8394
$applicationDef->addSetup('setVersion', [(string) $config->version]);
8495
}
8596

97+
// Catch or populate exceptions
8698
if ($config->catchExceptions !== null) {
8799
$applicationDef->addSetup('setCatchExceptions', [$config->catchExceptions]);
88100
}
89101

102+
// Call die() or not
90103
if ($config->autoExit !== null) {
91104
$applicationDef->addSetup('setAutoExit', [$config->autoExit]);
92105
}
93106

107+
// Register given or default HelperSet
94108
if ($config->helperSet !== null) {
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);
101-
}
102-
103-
$applicationDef->addSetup('setHelperSet', [$helperSetDef]);
109+
$applicationDef->addSetup('setHelperSet', [
110+
$defhelp->getDefinitionFromConfig($config->helperSet, $this->prefix('helperSet')),
111+
]);
104112
}
105113

114+
// Register extra helpers
106115
foreach ($config->helpers as $helperName => $helperConfig) {
107116
$helperPrefix = $this->prefix('helper.' . $helperName);
108-
$helperDef = $definitionsHelper->getDefinitionFromConfig($helperConfig, $helperPrefix);
117+
$helperDef = $defhelp->getDefinitionFromConfig($helperConfig, $helperPrefix);
109118

110119
if ($helperDef instanceof Definition) {
111120
$helperDef->setAutowired(false);
@@ -114,6 +123,7 @@ public function loadConfiguration(): void
114123
$applicationDef->addSetup('?->getHelperSet()->set(?)', ['@self', $helperDef]);
115124
}
116125

126+
// Commands lazy loading
117127
if ($config->lazy) {
118128
$builder->addDefinition($this->prefix('commandLoader'))
119129
->setType(CommandLoaderInterface::class)

tests/cases/Unit/DI/ConsoleExtension.HelperSet.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use Contributte\Console\DI\ConsoleExtension;
88
use Nette\DI\Compiler;
99
use Nette\DI\Container;
1010
use Nette\DI\ContainerLoader;
11+
use Nette\DI\InvalidConfigurationException;
1112
use Symfony\Component\Console\Application;
1213
use Tester\Assert;
1314
use Tester\FileMock;
@@ -87,3 +88,17 @@ test(function (): void {
8788
// 1 foo helper
8889
Assert::count(5, $container->getByType(Application::class)->getHelperSet()->getIterator());
8990
});
91+
92+
// Null helperSet
93+
test(function (): void {
94+
Assert::exception(function (): void {
95+
$loader = new ContainerLoader(TEMP_DIR, true);
96+
$loader->load(function (Compiler $compiler): void {
97+
$compiler->addExtension('console', new ConsoleExtension(true));
98+
$compiler->loadConfig(FileMock::create('
99+
console:
100+
helperSet: null
101+
', 'neon'));
102+
}, [getmypid(), 5]);
103+
}, InvalidConfigurationException::class, "The option 'console › helperSet' expects to be string|Nette\DI\Definitions\Statement, null given.");
104+
});

0 commit comments

Comments
 (0)