Skip to content
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

Move events group name to component params (#61) #62

Merged
merged 3 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 2.0.1 under development

- no changes in this release.
- Enh #61: Move `events` configuration group name to parameters (@olegbaturin)

## 2.0.0 February 16, 2023

Expand Down
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,28 @@ composer require yiisoft/yii-event

### DI configuration

You can see a config example in the [config directory](config):
You can find default configuration in the [config directory](config):

- [common.php](config/common.php) contains the configuration for the [PSR-14](https://www.php-fig.org/psr/psr-14/) interfaces.
- [console.php](config/console.php) and [web.php](config/web.php) contains the configuration for the `ListenerCollectionFactory`.
- [di.php](config/di.php) contains the configuration for the [PSR-14](https://www.php-fig.org/psr/psr-14/) interfaces.
- [di-web.php](config/di-web.php) and [di-console.php](config/di-consle.php) contains the configuration for the `Yiisoft\EventDispatcher\Provider\ListenerCollection`.
- [params-web.php](config/params-web.php) and [params-console.php](config/params-consle.php) contains parameters for `web` and `console` configurations.

All these configs will be used automatically in projects with the [yiisoft/config](https://github.com/yiisoft/config).
All these settings will be used automatically in projects with the [yiisoft/config](https://github.com/yiisoft/config).

If you have custom `events` configuration group name, for example `events-api`, redefine it in the `eventsConfigGroup` params key.

params.php
```php
<?php

declare(strict_types=1);

return [
'yiisoft/yii-event' => [
'eventsConfigGroup' => 'events-api',
],
]
```

### Event configuration example

Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
"merge-plan-file": "../tests/environment/.merge-plan.php"
},
"config-plugin": {
"params-web": "params-web.php",
"params-console": "params-console.php",
"di": "di.php",
"di-web": "di-web.php",
"di-console": "di-console.php",
Expand Down
3 changes: 2 additions & 1 deletion config/di-console.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
use Yiisoft\Yii\Event\ListenerCollectionFactory as Factory;

/** @var \Yiisoft\Config\Config $config */
/** @var array $params */

return [
ListenerCollection::class => static fn (Factory $factory) => $factory->create($config->get('events-console')),
ListenerCollection::class => static fn (Factory $factory) => $factory->create($config->get($params['yiisoft/yii-event']['eventsConfigGroup'])),
];
3 changes: 2 additions & 1 deletion config/di-web.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
use Yiisoft\Yii\Event\ListenerCollectionFactory as Factory;

/** @var \Yiisoft\Config\Config $config */
/** @var array $params */

return [
ListenerCollection::class => static fn (Factory $factory) => $factory->create($config->get('events-web')),
ListenerCollection::class => static fn (Factory $factory) => $factory->create($config->get($params['yiisoft/yii-event']['eventsConfigGroup'])),
];
9 changes: 9 additions & 0 deletions config/params-console.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

return [
'yiisoft/yii-event' => [
'eventsConfigGroup' => 'events-console',
],
];
9 changes: 9 additions & 0 deletions config/params-web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

return [
'yiisoft/yii-event' => [
'eventsConfigGroup' => 'events-web',
],
];
6 changes: 3 additions & 3 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private function createContainer(?string $postfix = null): Container
{
return new Container(
ContainerConfig::create()->withDefinitions(
$this->createConfig()->get('di' . ($postfix !== null ? '-' . $postfix : ''))
$this->createConfig($postfix)->get('di' . ($postfix !== null ? '-' . $postfix : ''))
+
[
EventDispatcherInterface::class => new SimpleEventDispatcher(),
Expand All @@ -60,13 +60,13 @@ private function createContainer(?string $postfix = null): Container
);
}

private function createConfig(): Config
private function createConfig(?string $postfix = null): Config
{
return new Config(
new ConfigPaths(dirname(__DIR__), 'config'),
null,
[],
null,
$postfix !== null ? 'params' . '-' . $postfix : null,
'../tests/environment/.merge-plan.php'
);
}
Expand Down