Augmentation vers version 3.3.0
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
|
||||
namespace Symfony\Component\EventDispatcher;
|
||||
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
@@ -20,6 +21,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
* @author Jordan Alliot <jordan.alliot@gmail.com>
|
||||
*
|
||||
* @deprecated since 3.3, to be removed in 4.0. Use EventDispatcher with closure factories instead.
|
||||
*/
|
||||
class ContainerAwareEventDispatcher extends EventDispatcher
|
||||
{
|
||||
@@ -28,16 +31,24 @@ class ContainerAwareEventDispatcher extends EventDispatcher
|
||||
/**
|
||||
* The service IDs of the event listeners and subscribers.
|
||||
*/
|
||||
private $listenerIds = array();
|
||||
private $listenerIds = [];
|
||||
|
||||
/**
|
||||
* The services registered as listeners.
|
||||
*/
|
||||
private $listeners = array();
|
||||
private $listeners = [];
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
|
||||
$class = \get_class($this);
|
||||
if ($this instanceof \PHPUnit_Framework_MockObject_MockObject || $this instanceof MockObject || $this instanceof \Prophecy\Doubler\DoubleInterface) {
|
||||
$class = get_parent_class($class);
|
||||
}
|
||||
if (__CLASS__ !== $class) {
|
||||
@trigger_error(sprintf('The %s class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead.', __CLASS__), E_USER_DEPRECATED);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,11 +65,13 @@ class ContainerAwareEventDispatcher extends EventDispatcher
|
||||
*/
|
||||
public function addListenerService($eventName, $callback, $priority = 0)
|
||||
{
|
||||
@trigger_error(sprintf('The %s class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead.', __CLASS__), E_USER_DEPRECATED);
|
||||
|
||||
if (!\is_array($callback) || 2 !== \count($callback)) {
|
||||
throw new \InvalidArgumentException('Expected an array("service", "method") argument');
|
||||
throw new \InvalidArgumentException('Expected an ["service", "method"] argument');
|
||||
}
|
||||
|
||||
$this->listenerIds[$eventName][] = array($callback[0], $callback[1], $priority);
|
||||
$this->listenerIds[$eventName][] = [$callback[0], $callback[1], $priority];
|
||||
}
|
||||
|
||||
public function removeListener($eventName, $listener)
|
||||
@@ -66,10 +79,9 @@ class ContainerAwareEventDispatcher extends EventDispatcher
|
||||
$this->lazyLoad($eventName);
|
||||
|
||||
if (isset($this->listenerIds[$eventName])) {
|
||||
foreach ($this->listenerIds[$eventName] as $i => $args) {
|
||||
list($serviceId, $method) = $args;
|
||||
foreach ($this->listenerIds[$eventName] as $i => list($serviceId, $method)) {
|
||||
$key = $serviceId.'.'.$method;
|
||||
if (isset($this->listeners[$eventName][$key]) && $listener === array($this->listeners[$eventName][$key], $method)) {
|
||||
if (isset($this->listeners[$eventName][$key]) && $listener === [$this->listeners[$eventName][$key], $method]) {
|
||||
unset($this->listeners[$eventName][$key]);
|
||||
if (empty($this->listeners[$eventName])) {
|
||||
unset($this->listeners[$eventName]);
|
||||
@@ -135,14 +147,16 @@ class ContainerAwareEventDispatcher extends EventDispatcher
|
||||
*/
|
||||
public function addSubscriberService($serviceId, $class)
|
||||
{
|
||||
@trigger_error(sprintf('The %s class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead.', __CLASS__), E_USER_DEPRECATED);
|
||||
|
||||
foreach ($class::getSubscribedEvents() as $eventName => $params) {
|
||||
if (\is_string($params)) {
|
||||
$this->listenerIds[$eventName][] = array($serviceId, $params, 0);
|
||||
$this->listenerIds[$eventName][] = [$serviceId, $params, 0];
|
||||
} elseif (\is_string($params[0])) {
|
||||
$this->listenerIds[$eventName][] = array($serviceId, $params[0], isset($params[1]) ? $params[1] : 0);
|
||||
$this->listenerIds[$eventName][] = [$serviceId, $params[0], isset($params[1]) ? $params[1] : 0];
|
||||
} else {
|
||||
foreach ($params as $listener) {
|
||||
$this->listenerIds[$eventName][] = array($serviceId, $listener[0], isset($listener[1]) ? $listener[1] : 0);
|
||||
$this->listenerIds[$eventName][] = [$serviceId, $listener[0], isset($listener[1]) ? $listener[1] : 0];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -150,6 +164,8 @@ class ContainerAwareEventDispatcher extends EventDispatcher
|
||||
|
||||
public function getContainer()
|
||||
{
|
||||
@trigger_error('The '.__METHOD__.'() method is deprecated since Symfony 3.3 as its class will be removed in 4.0. Inject the container or the services you need in your listeners/subscribers instead.', E_USER_DEPRECATED);
|
||||
|
||||
return $this->container;
|
||||
}
|
||||
|
||||
@@ -164,16 +180,15 @@ class ContainerAwareEventDispatcher extends EventDispatcher
|
||||
protected function lazyLoad($eventName)
|
||||
{
|
||||
if (isset($this->listenerIds[$eventName])) {
|
||||
foreach ($this->listenerIds[$eventName] as $args) {
|
||||
list($serviceId, $method, $priority) = $args;
|
||||
foreach ($this->listenerIds[$eventName] as list($serviceId, $method, $priority)) {
|
||||
$listener = $this->container->get($serviceId);
|
||||
|
||||
$key = $serviceId.'.'.$method;
|
||||
if (!isset($this->listeners[$eventName][$key])) {
|
||||
$this->addListener($eventName, array($listener, $method), $priority);
|
||||
$this->addListener($eventName, [$listener, $method], $priority);
|
||||
} elseif ($this->listeners[$eventName][$key] !== $listener) {
|
||||
parent::removeListener($eventName, array($this->listeners[$eventName][$key], $method));
|
||||
$this->addListener($eventName, array($listener, $method), $priority);
|
||||
parent::removeListener($eventName, [$this->listeners[$eventName][$key], $method]);
|
||||
$this->addListener($eventName, [$listener, $method], $priority);
|
||||
}
|
||||
|
||||
$this->listeners[$eventName][$key] = $listener;
|
||||
|
||||
Reference in New Issue
Block a user