Nete 3.0: Operator? is deprecated in config files
- Marek Bartoš
- Nette Blogger | 1275
Most simple cases should work without ?, for rest should be code eg. extracted to new class or moved into an extension.
It was helpful syntax but supporting programming in config files which is bad
Last edited by Mabar (2019-11-19 13:34)
- Pavel Janda
- Member | 977
@Mabar I would not say that usage of PHP in neon is strictly “bad”. I would say there is a scale of what one may consider “useful” and what someone else may consider “I like PHP extension better”.
For those who are looking for easiest way:
Nette 2.4 code (neon config):
application.application:
setup:
- "$service->onError[] = ?"([@Some\Logger, logApplicationError])
As far as my knowledge goes this code does not have an equivalent in 3.0. A PHP extension may be used instead:
<?php
declare(strict_types=1);
namespace App\DI;
use Nette\Application\Application;
use Nette\DI\CompilerExtension;
use Nette\DI\Definitions\FactoryDefinition;
use Nette\DI\Definitions\ServiceDefinition;
use Nette\DI\Definitions\Statement;
use Some\Logger;
/**
* @property-read array $config
*/
final class NativeEventsExtension extends CompilerExtension
{
public function loadConfiguration(): void
{
$builder = $this->getContainerBuilder();
$application = $builder->getByType(Application::class);
if ($application === null) {
throw new \UnexpectedValueException;
}
$applicationDefinition = $builder->getDefinition($application);
if ($applicationDefinition instanceof ServiceDefinition) {
/**
* OK
*/
} elseif ($applicationDefinition instanceof FactoryDefinition) {
$applicationDefinition = $applicationDefinition->getResultDefinition();
} else {
throw new \UnexpectedValueException;
}
$builder->addDefinition($this->prefix('logger'))
->setFactory(Logger::class);
$applicationDefinition->addSetup(
new Statement(
'$service->onError[] = [?, ?]',
['@' . Logger::class, 'logApplicationError']
)
);
}
}
Do you know now why I consider the 2.4 more useful?
EDIT:
It looks there is a new syntax for 3.0:
application.application:
setup:
- '$service->onError[]' = [@Some\Logger, logApplicationError]
Last edited by Pavel Janda (2019-11-20 14:16)
- Marek Bartoš
- Nette Blogger | 1275
Btw, you imho don't need these
if($applicationDefinition instanceof ServiceDefinition)
conditions.
Application should always be a ServiceDefinition so
assert($applicationDefinition instanceof ServiceDefinition)
shoudl
be enough
Last edited by Mabar (2019-11-20 12:50)
- David Grudl
- Nette Core | 8228
Statements with a question mark are very difficult to understand. The aim is to replace its usage with a more readable syntax.
Your example can be replaced this way
application.application:
setup:
- '$service->onError[]' = [@Some\Logger, logApplicationError]
- Pavel Janda
- Member | 977
@Mabar Those are there because of phpstan..
@DavidGrudl But that is awesome! Can we please put it somewhere in the docs? Thanks a lot
- Marek Bartoš
- Nette Blogger | 1275
@Mabar Those are there because of phpstan..
Even with max level and extensions, assert is enough
https://github.com/…xtension.php#L35