Skip to content

Commit 09e87b3

Browse files
authored
add documentation for new event listeneres DI support (#8310)
1 parent 982fe37 commit 09e87b3

4 files changed

Lines changed: 120 additions & 11 deletions

File tree

docs/en/appendices/5-4-migration-guide.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,19 @@ version is reported as `unknown`), the header is omitted.
4343
Events being registered in either `Application::events()` or `Plugin::events()`
4444
now work in both web and CLI contexts. It is therefore highly recommended to
4545
move your event listeners from the `config/bootstrap.php` file to the
46-
`events()` method in your `Application` or `Plugin` class.
46+
`eventListeners()` method in your `Application` or `Plugin` class. Use the
47+
`events()` method when you need custom registration logic or anonymous
48+
listeners.
4749
See [Application and Plugin Events](../core-libraries/events#registering-event-listeners) for more details.
4850

51+
`Application::eventListeners()` and `Plugin::eventListeners()` were added to
52+
register event listener classes declaratively. These listeners are resolved
53+
through the application's dependency injection container, so they can use
54+
constructor-injected dependencies.
55+
56+
`EventAwareApplicationInterface::pluginEvents()` has been deprecated. Plugin
57+
events are now registered while each plugin is bootstrapped.
58+
4959
### I18n
5060

5161
- `Number::parseFloat()` now returns `null` instead of `0.0` when parsing

docs/en/core-libraries/events.md

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,16 +298,86 @@ As you can see in the above code, the `on()` function will accept instances
298298
of the `EventListener` interface. Internally, the event manager will use
299299
`implementedEvents()` to attach the correct callbacks.
300300

301-
::: info Added in version 5.1.0
302-
The `events` hook was added to the `BaseApplication` as well as the `BasePlugin` class
301+
::: info Added in version 5.4.0
302+
The `eventListeners` hook was added to `BaseApplication` and `BasePlugin`.
303303
:::
304304

305-
As of CakePHP 5.1 it is recommended to register event listeners by adding them via the `events` hook in your application or plugin class:
305+
As of CakePHP 5.4, applications and plugins can register listener classes with
306+
the `eventListeners()` hook. Listener classes are resolved through the
307+
application's dependency injection container before they are attached to the
308+
global event manager. This lets listeners declare constructor dependencies:
306309

307310
```php
308311
namespace App;
309312

310313
use App\Event\UserStatistic;
314+
use Cake\Http\BaseApplication;
315+
316+
class Application extends BaseApplication
317+
{
318+
// The rest of your Application class
319+
320+
/**
321+
* @return list<class-string<\Cake\Event\EventListenerInterface>>
322+
*/
323+
public function eventListeners(): array
324+
{
325+
return [
326+
UserStatistic::class,
327+
];
328+
}
329+
}
330+
```
331+
332+
Plugins can define event listeners the same way in their plugin class:
333+
334+
```php
335+
namespace ContactManager;
336+
337+
use Cake\Core\BasePlugin;
338+
use ContactManager\Event\UserStatistic;
339+
340+
class ContactManagerPlugin extends BasePlugin
341+
{
342+
/**
343+
* @return list<class-string<\Cake\Event\EventListenerInterface>>
344+
*/
345+
public function eventListeners(): array
346+
{
347+
return [
348+
UserStatistic::class,
349+
];
350+
}
351+
}
352+
```
353+
354+
If your listener has constructor dependencies, register the listener and its
355+
dependencies in `Application::services()` or `Plugin::services()`:
356+
357+
```php
358+
use App\Event\UserStatistic;
359+
use App\Service\StatisticsClient;
360+
use Cake\Core\ContainerInterface;
361+
362+
public function services(ContainerInterface $container): void
363+
{
364+
$container->addShared(StatisticsClient::class);
365+
$container->addShared(UserStatistic::class)
366+
->addArgument(StatisticsClient::class);
367+
}
368+
```
369+
370+
::: info Added in version 5.1.0
371+
The `events` hook was added to the `BaseApplication` as well as the `BasePlugin` class.
372+
:::
373+
374+
Use the `events()` hook in your application or plugin class when you need
375+
imperative registration logic, or want to register anonymous functions:
376+
377+
```php
378+
namespace App;
379+
380+
use Cake\Event\EventInterface;
311381
use Cake\Event\EventManagerInterface;
312382
use Cake\Http\BaseApplication;
313383

@@ -317,8 +387,9 @@ class Application extends BaseApplication
317387

318388
public function events(EventManagerInterface $eventManager): EventManagerInterface
319389
{
320-
$statistics = new UserStatistic();
321-
$eventManager->on($statistics);
390+
$eventManager->on('Order.afterPlace', function (EventInterface $event): void {
391+
// Code to update statistics
392+
});
322393

323394
return $eventManager;
324395
}

docs/en/development/application.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@ methods:
1616

1717
- `bootstrap` Used to load [configuration files](../development/configuration), define constants and other global functions.
1818
By default, this will include **config/bootstrap.php**. This is the ideal place
19-
to load [Plugins](../plugins) and global [event listeners](../core-libraries/events).
19+
to load [Plugins](../plugins) and application configuration.
2020
- `routes` Used to load [routes](../development/routing). By default, this
2121
will include **config/routes.php**.
2222
- `middleware` Used to add [middleware](../controllers/middleware) to your application.
2323
- `console` Used to add [console commands](../console-commands) to your
2424
application. By default, this will automatically discover console commands in
2525
your application and all plugins.
26+
- `eventListeners` Used to register global [event listener](../core-libraries/events)
27+
classes with the application's event manager.
28+
- `events` Used to register global [events](../core-libraries/events) that
29+
require custom registration logic.
2630

2731
## Bootstrapping your Application
2832

@@ -49,8 +53,7 @@ sections there are better ways you add custom logic to your application.
4953

5054
In addition to the **config/bootstrap.php** file which should be used to
5155
configure low-level concerns of your application, you can also use the
52-
`Application::bootstrap()` hook method to load/initialize plugins, and attach
53-
global event listeners:
56+
`Application::bootstrap()` hook method to load/initialize plugins:
5457

5558
```php
5659
// in src/Application.php
@@ -84,6 +87,6 @@ class Application extends BaseApplication
8487
}
8588
```
8689

87-
Loading plugins and events in `Application::bootstrap()` makes
88-
[Integration Testing](../development/testing#integration-testing) easier as events and routes will be re-processed on
90+
Loading plugins in `Application::bootstrap()` makes
91+
[Integration Testing](../development/testing#integration-testing) easier as routes will be re-processed on
8992
each test method.

docs/en/plugins.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ appropriate parts of your application. The hooks are:
115115
collection.
116116
- `services` Used to register application container services. This is a good
117117
opportunity to setup additional objects that need access to the container.
118+
- `eventListeners` Used to register global event listener classes with the
119+
application's event manager.
120+
- `events` Used to register global events that require custom registration
121+
logic.
118122

119123
By default, all plugins hooks are enabled. You can disable hooks by using the
120124
related options of the `plugin load` command:
@@ -311,6 +315,7 @@ use Cake\Core\BasePlugin;
311315
use Cake\Core\ContainerInterface;
312316
use Cake\Core\PluginApplicationInterface;
313317
use Cake\Console\CommandCollection;
318+
use Cake\Event\EventManagerInterface;
314319
use Cake\Http\MiddlewareQueue;
315320
use Cake\Routing\RouteBuilder;
316321

@@ -369,6 +374,26 @@ class ContactManagerPlugin extends BasePlugin
369374
{
370375
// Add your services here
371376
}
377+
378+
/**
379+
* @return list<class-string<\Cake\Event\EventListenerInterface>>
380+
*/
381+
public function eventListeners(): array
382+
{
383+
return [
384+
// Add your event listeners here.
385+
];
386+
}
387+
388+
/**
389+
* @inheritDoc
390+
*/
391+
public function events(EventManagerInterface $eventManager): EventManagerInterface
392+
{
393+
// Add custom event registration logic here.
394+
395+
return $eventManager;
396+
}
372397
}
373398
```
374399

0 commit comments

Comments
 (0)