TYPO3 v15 dev-main snapshot ()
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\EventDispatcher;
|
||||
|
||||
use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
use Psr\EventDispatcher\ListenerProviderInterface;
|
||||
use Psr\EventDispatcher\StoppableEventInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\AsAlias;
|
||||
use TYPO3\CMS\Core\SingletonInterface;
|
||||
|
||||
/**
|
||||
* Base PSR-14 event dispatcher which has only one listener provider, given at runtime
|
||||
* Is a singleton instance in order to be published once.
|
||||
*/
|
||||
#[AsAlias(EventDispatcherInterface::class, public: true)]
|
||||
class EventDispatcher implements EventDispatcherInterface, SingletonInterface
|
||||
{
|
||||
public function __construct(
|
||||
protected readonly ListenerProviderInterface $listenerProvider
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @template T of object
|
||||
* @param T $event
|
||||
* @return T
|
||||
*/
|
||||
public function dispatch(object $event): object
|
||||
{
|
||||
// If the event is already stopped, nothing to do here.
|
||||
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
|
||||
return $event;
|
||||
}
|
||||
foreach ($this->listenerProvider->getListenersForEvent($event) as $listener) {
|
||||
$listener($event);
|
||||
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $event;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\EventDispatcher;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\EventDispatcher\ListenerProviderInterface;
|
||||
|
||||
/**
|
||||
* Provides Listeners configured with the symfony service
|
||||
* tag 'event.listener'.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class ListenerProvider implements ListenerProviderInterface
|
||||
{
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
protected $container;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $listeners = [];
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Not part of the public API, used in the generated service factor for this class,
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function addListener(string $event, string $service, ?string $method = null, ?string $identifier = null): void
|
||||
{
|
||||
$this->listeners[$event][$identifier ?? $service] = [
|
||||
'service' => $service,
|
||||
'method' => $method,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Not part of the public API, only used for debugging purposes
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function getAllListenerDefinitions(): array
|
||||
{
|
||||
return $this->listeners;
|
||||
}
|
||||
|
||||
public function getListenersForEvent(object $event): iterable
|
||||
{
|
||||
$eventClasses = [get_class($event)];
|
||||
$classParents = class_parents($event);
|
||||
$classInterfaces = class_implements($event);
|
||||
if (!empty($classParents)) {
|
||||
array_push($eventClasses, ...array_values($classParents));
|
||||
}
|
||||
if (!empty($classInterfaces)) {
|
||||
array_push($eventClasses, ...array_values($classInterfaces));
|
||||
}
|
||||
foreach ($eventClasses as $className) {
|
||||
if (isset($this->listeners[$className])) {
|
||||
foreach ($this->listeners[$className] as $listener) {
|
||||
yield $this->getCallable($listener['service'], $listener['method']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
protected function getCallable(string $service, ?string $method = null): callable
|
||||
{
|
||||
$target = $this->container->get($service);
|
||||
if ($method !== null) {
|
||||
// Dispatch to configured method name instead of __invoke()
|
||||
$target = [ $target, $method ];
|
||||
}
|
||||
|
||||
if (!is_callable($target)) {
|
||||
throw new \InvalidArgumentException(
|
||||
sprintf('Event listener "%s%s%s" is not callable"', $service, ($method !== null ? '::' : ''), $method),
|
||||
1549988537
|
||||
);
|
||||
}
|
||||
|
||||
return $target;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\EventDispatcher;
|
||||
|
||||
use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
/**
|
||||
* An event dispatcher that does nothing. Useful for testing.
|
||||
*
|
||||
* Also used in scopes like the install tool where classes have dependencies
|
||||
* to EventDispatcherInterface but should not actively dispatch events.
|
||||
*/
|
||||
final class NoopEventDispatcher implements EventDispatcherInterface
|
||||
{
|
||||
public function dispatch(object $event): object
|
||||
{
|
||||
return $event;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user