Skip to content

Commit

Permalink
[Form][EventDispatcher] Fix VarDumper usage related to perf regression
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Sep 20, 2016
1 parent 84229f8 commit 294868e
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 244 deletions.
Expand Up @@ -15,8 +15,6 @@
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\VarDumper\Caster\ClassStub;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Psr\Log\LoggerInterface;

/**
Expand All @@ -34,7 +32,6 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
private $called;
private $dispatcher;
private $wrappedListeners;
private $cloner;

/**
* Constructor.
Expand All @@ -50,9 +47,6 @@ public function __construct(EventDispatcherInterface $dispatcher, Stopwatch $sto
$this->logger = $logger;
$this->called = array();
$this->wrappedListeners = array();
if (class_exists(ClassStub::class)) {
$this->cloner = new VarCloner();
}
}

/**
Expand Down Expand Up @@ -159,8 +153,7 @@ public function getCalledListeners()
$called = array();
foreach ($this->called as $eventName => $listeners) {
foreach ($listeners as $listener) {
$info = $this->getListenerInfo($listener->getWrappedListener(), $eventName);
$called[$eventName.'.'.$info['pretty']] = $info;
$called[$eventName.'.'.$listener->getPretty()] = $listener->getInfo($eventName);
}
}

Expand Down Expand Up @@ -198,8 +191,10 @@ public function getNotCalledListeners()
}

if (!$called) {
$info = $this->getListenerInfo($listener, $eventName);
$notCalled[$eventName.'.'.$info['pretty']] = $info;
if (!$listener instanceof WrappedListener) {
$listener = new WrappedListener($listener, null, $this->stopwatch, $this);
}
$notCalled[$eventName.'.'.$listener->getPretty()] = $listener->getInfo($eventName);
}
}
}
Expand Down Expand Up @@ -245,12 +240,11 @@ protected function postDispatch($eventName, Event $event)
private function preProcess($eventName)
{
foreach ($this->dispatcher->getListeners($eventName) as $listener) {
$info = $this->getListenerInfo($listener, $eventName);
$name = isset($info['class']) ? $info['class'] : $info['type'];
$wrappedListener = new WrappedListener($listener, $name, $this->stopwatch, $this);
$priority = $this->getListenerPriority($eventName, $listener);
$wrappedListener = new WrappedListener($listener, null, $this->stopwatch, $this);
$this->wrappedListeners[$eventName][] = $wrappedListener;
$this->dispatcher->removeListener($eventName, $listener);
$this->dispatcher->addListener($eventName, $wrappedListener, $info['priority']);
$this->dispatcher->addListener($eventName, $wrappedListener, $priority);
}
}

Expand All @@ -267,10 +261,13 @@ private function postProcess($eventName)
$this->dispatcher->removeListener($eventName, $listener);
$this->dispatcher->addListener($eventName, $listener->getWrappedListener(), $priority);

$info = $this->getListenerInfo($listener->getWrappedListener(), $eventName);
if (null !== $this->logger) {
$context = array('event' => $eventName, 'listener' => $listener->getPretty());
}

if ($listener->wasCalled()) {
if (null !== $this->logger) {
$this->logger->debug('Notified event "{event}" to listener "{listener}".', array('event' => $eventName, 'listener' => $info['pretty']));
$this->logger->debug('Notified event "{event}" to listener "{listener}".', $context);
}

if (!isset($this->called[$eventName])) {
Expand All @@ -281,83 +278,19 @@ private function postProcess($eventName)
}

if (null !== $this->logger && $skipped) {
$this->logger->debug('Listener "{listener}" was not called for event "{event}".', array('listener' => $info['pretty'], 'event' => $eventName));
$this->logger->debug('Listener "{listener}" was not called for event "{event}".', $context);
}

if ($listener->stoppedPropagation()) {
if (null !== $this->logger) {
$this->logger->debug('Listener "{listener}" stopped propagation of the event "{event}".', array('listener' => $info['pretty'], 'event' => $eventName));
$this->logger->debug('Listener "{listener}" stopped propagation of the event "{event}".', $context);
}

$skipped = true;
}
}
}

/**
* Returns information about the listener.
*
* @param object $listener The listener
* @param string $eventName The event name
*
* @return array Information about the listener
*/
private function getListenerInfo($listener, $eventName)
{
$info = array(
'event' => $eventName,
'priority' => $this->getListenerPriority($eventName, $listener),
);
if ($listener instanceof \Closure) {
$info += array(
'type' => 'Closure',
'pretty' => 'closure',
);
} elseif (is_string($listener)) {
try {
$r = new \ReflectionFunction($listener);
$file = $r->getFileName();
$line = $r->getStartLine();
} catch (\ReflectionException $e) {
$file = null;
$line = null;
}
$info += array(
'type' => 'Function',
'function' => $listener,
'file' => $file,
'line' => $line,
'pretty' => $listener,
);
} elseif (is_array($listener) || (is_object($listener) && is_callable($listener))) {
if (!is_array($listener)) {
$listener = array($listener, '__invoke');
}
$class = is_object($listener[0]) ? get_class($listener[0]) : $listener[0];
try {
$r = new \ReflectionMethod($class, $listener[1]);
$file = $r->getFileName();
$line = $r->getStartLine();
} catch (\ReflectionException $e) {
$file = null;
$line = null;
}
$info += array(
'type' => 'Method',
'class' => $class,
'method' => $listener[1],
'file' => $file,
'line' => $line,
'pretty' => $class.'::'.$listener[1],
);
}
if (null !== $this->cloner) {
$info['data'] = $this->cloner->cloneVar(array(new ClassStub($info['pretty'].'()', $listener)))->seek(0);
}

return $info;
}

private function sortListenersByPriority($a, $b)
{
if (is_int($a['priority']) && !is_int($b['priority'])) {
Expand Down
45 changes: 45 additions & 0 deletions src/Symfony/Component/EventDispatcher/Debug/WrappedListener.php
Expand Up @@ -14,6 +14,8 @@
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\VarDumper\Caster\ClassStub;
use Symfony\Component\VarDumper\Cloner\VarCloner;

/**
* @author Fabien Potencier <fabien@symfony.com>
Expand All @@ -26,6 +28,10 @@ class WrappedListener
private $stoppedPropagation;
private $stopwatch;
private $dispatcher;
private $pretty;
private $data;

private static $cloner;

public function __construct($listener, $name, Stopwatch $stopwatch, EventDispatcherInterface $dispatcher = null)
{
Expand All @@ -35,6 +41,26 @@ public function __construct($listener, $name, Stopwatch $stopwatch, EventDispatc
$this->dispatcher = $dispatcher;
$this->called = false;
$this->stoppedPropagation = false;

if (is_array($listener)) {
$this->name = is_object($listener[0]) ? get_class($listener[0]) : $listener[0];
$this->pretty = $this->name.'::'.$listener[1];
} elseif ($listener instanceof \Closure) {
$this->pretty = $this->name = 'closure';
} elseif (is_string($listener)) {
$this->pretty = $this->name = $listener;
} else {
$this->name = get_class($listener);
$this->pretty = $this->name.'::__invoke';
}

if (null !== $name) {
$this->name = $name;
}

if (null === self::$cloner) {
self::$cloner = class_exists(ClassStub::class) ? new VarCloner() : false;
}
}

public function getWrappedListener()
Expand All @@ -52,6 +78,25 @@ public function stoppedPropagation()
return $this->stoppedPropagation;
}

public function getPretty()
{
return $this->pretty;
}

public function getInfo($eventName)
{
if (null === $this->data) {
$this->data = false !== self::$cloner ? self::$cloner->cloneVar(array(new ClassStub($this->pretty.'()', $this->listener)))->seek(0) : $this->pretty;
}

return array(
'event' => $eventName,
'priority' => null !== $this->dispatcher ? $this->dispatcher->getListenerPriority($eventName, $this->listener) : null,
'pretty' => $this->pretty,
'data' => $this->data,
);
}

public function __invoke(Event $event, $eventName, EventDispatcherInterface $dispatcher)
{
$this->called = true;
Expand Down
Expand Up @@ -96,15 +96,17 @@ public function testGetCalledListeners()
$tdispatcher->addListener('foo', $listener = function () {});

$listeners = $tdispatcher->getNotCalledListeners();
$this->assertArrayHasKey('data', $listeners['foo.closure']);
unset($listeners['foo.closure']['data']);
$this->assertEquals(array(), $tdispatcher->getCalledListeners());
$this->assertEquals(array('foo.closure' => array('event' => 'foo', 'type' => 'Closure', 'pretty' => 'closure', 'priority' => 0)), $listeners);
$this->assertEquals(array('foo.closure' => array('event' => 'foo', 'pretty' => 'closure', 'priority' => 0)), $listeners);

$tdispatcher->dispatch('foo');

$listeners = $tdispatcher->getCalledListeners();
$this->assertArrayHasKey('data', $listeners['foo.closure']);
unset($listeners['foo.closure']['data']);
$this->assertEquals(array('foo.closure' => array('event' => 'foo', 'type' => 'Closure', 'pretty' => 'closure', 'priority' => null)), $listeners);
$this->assertEquals(array('foo.closure' => array('event' => 'foo', 'pretty' => 'closure', 'priority' => null)), $listeners);
$this->assertEquals(array(), $tdispatcher->getNotCalledListeners());
}

Expand Down

0 comments on commit 294868e

Please sign in to comment.