-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to use $params in anonymous functions? #31
Comments
Please, show what do you want in practice, cause your example doesn't look practical. |
I am trying to access parameters in an anonymous function. Such as logger configuration(DI), I want to make FileTarget's levels to be mergeable via 'logger' => function (yii\di\Container $container) {
$logger = new Yiisoft\Log\Logger([
$container->get(Yiisoft\Log\FileTarget::class),
]);
return $logger;
},
Yiisoft\Log\FileTarget::class => function (yii\di\Container $container) use($params) {
/** @var yii\base\Aliases $aliases */
$aliases = $container->get('aliases');
$target = new Yiisoft\Log\FileTarget(
$aliases->get($params['console.logger.target.file.logFile']),
$container->get(Yiisoft\Log\FileRotator::class)
);
$target->levels = $params['logger.target.file.levels'];
//...
return $target;
},
Yiisoft\Log\FileRotator::class => [
'__class' => Yiisoft\Log\FileRotator::class,
], params.php return [
'logger.target.file.levels' => ['warning', 'error'],
]; params-local.php return [
'logger.target.file.levels' => ['info', 'debug'],
]; |
You can get params from the app object similarly you get aliases:
But it looks like a good case to improve Yii DI for these objects could be configured with arrays only without closures. Something like this: 'logger' => [
'__class' => Logger::class,
'__construct' => [
[Reference::to('file-target')],
],
],
'file-target' => [
'__class' => Yiisoft\Log\FileTarget::class,
'__construct' => [
Reference::aliasTo('@logger/logFile'),
Reference::to(Yiisoft\Log\FileRotator::class),
],
'levels' => $params['logger.target.file.levels'],
], I'll look into it on a weekend. |
Your solution look good to me:) EDIT: |
Hi, I found another issue about exporting I've read the code of It may can be fixed by two ways:
Reproduce:
|
Dump the |
I have no idea so far, it maybe impossible :( |
That's actually a problem not about anonymous functions but about |
Initialization of In return [
'app' => function () use ($params) {
return [
'name' => $params['app.name']
];
},
'appName' => $params['app.name'],
]; Closing this issue. Please open a new one in case of any problems. |
Composer.json
params.php
web.php
index.php
Got:
BTW, the generated web.php missing
use
language structureThe text was updated successfully, but these errors were encountered: