/usr/share/php/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php is in php-symfony-http-kernel 2.3.21+dfsg-4+deb8u3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 | <?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpKernel\Debug;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\HttpKernel\KernelEvents;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Profiler\Profile;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcherInterface;
/**
* Collects some data about event listeners.
*
* This event dispatcher delegates the dispatching to another one.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class TraceableEventDispatcher implements EventDispatcherInterface, TraceableEventDispatcherInterface
{
private $logger;
private $called;
private $stopwatch;
private $profiler;
private $dispatcher;
private $wrappedListeners;
private $firstCalledEvent;
private $lastEventId = 0;
/**
* Constructor.
*
* @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
* @param Stopwatch $stopwatch A Stopwatch instance
* @param LoggerInterface $logger A LoggerInterface instance
*/
public function __construct(EventDispatcherInterface $dispatcher, Stopwatch $stopwatch, LoggerInterface $logger = null)
{
$this->dispatcher = $dispatcher;
$this->stopwatch = $stopwatch;
$this->logger = $logger;
$this->called = array();
$this->wrappedListeners = array();
$this->firstCalledEvent = array();
}
/**
* Sets the profiler.
*
* @param Profiler|null $profiler A Profiler instance
*/
public function setProfiler(Profiler $profiler = null)
{
$this->profiler = $profiler;
}
/**
* {@inheritdoc}
*/
public function addListener($eventName, $listener, $priority = 0)
{
$this->dispatcher->addListener($eventName, $listener, $priority);
}
/**
* {@inheritdoc}
*/
public function addSubscriber(EventSubscriberInterface $subscriber)
{
$this->dispatcher->addSubscriber($subscriber);
}
/**
* {@inheritdoc}
*/
public function removeListener($eventName, $listener)
{
return $this->dispatcher->removeListener($eventName, $listener);
}
/**
* {@inheritdoc}
*/
public function removeSubscriber(EventSubscriberInterface $subscriber)
{
return $this->dispatcher->removeSubscriber($subscriber);
}
/**
* {@inheritdoc}
*/
public function getListeners($eventName = null)
{
return $this->dispatcher->getListeners($eventName);
}
/**
* {@inheritdoc}
*/
public function hasListeners($eventName = null)
{
return $this->dispatcher->hasListeners($eventName);
}
/**
* {@inheritdoc}
*/
public function dispatch($eventName, Event $event = null)
{
if (null === $event) {
$event = new Event();
}
$eventId = ++$this->lastEventId;
$this->preDispatch($eventName, $eventId, $event);
$e = $this->stopwatch->start($eventName, 'section');
$this->firstCalledEvent[$eventName] = $this->stopwatch->start($eventName.'.loading', 'event_listener_loading');
if (!$this->dispatcher->hasListeners($eventName)) {
$this->firstCalledEvent[$eventName]->stop();
}
$this->dispatcher->dispatch($eventName, $event);
unset($this->firstCalledEvent[$eventName]);
$e->stop();
$this->postDispatch($eventName, $eventId, $event);
return $event;
}
/**
* {@inheritdoc}
*/
public function getCalledListeners()
{
return $this->called;
}
/**
* {@inheritdoc}
*/
public function getNotCalledListeners()
{
try {
$allListeners = $this->getListeners();
} catch (\Exception $e) {
if (null !== $this->logger) {
$this->logger->info(sprintf('An exception was thrown while getting the uncalled listeners (%s)', $e->getMessage()), array('exception' => $e));
}
// unable to retrieve the uncalled listeners
return array();
}
$notCalled = array();
foreach ($allListeners as $name => $listeners) {
foreach ($listeners as $listener) {
$info = $this->getListenerInfo($listener, null, $name);
if (!isset($this->called[$name.'.'.$info['pretty']])) {
$notCalled[$name.'.'.$info['pretty']] = $info;
}
}
}
return $notCalled;
}
/**
* Proxies all method calls to the original event dispatcher.
*
* @param string $method The method name
* @param array $arguments The method arguments
*
* @return mixed
*/
public function __call($method, $arguments)
{
return call_user_func_array(array($this->dispatcher, $method), $arguments);
}
/**
* This is a private method and must not be used.
*
* This method is public because it is used in a closure.
* Whenever Symfony will require PHP 5.4, this could be changed
* to a proper private method.
*/
public function logSkippedListeners($eventName, $eventId, Event $event, $listener)
{
if (null === $this->logger) {
return;
}
$info = $this->getListenerInfo($listener, $eventId, $eventName);
$this->logger->debug(sprintf('Listener "%s" stopped propagation of the event "%s".', $info['pretty'], $eventName));
$skippedListeners = $this->getListeners($eventName);
$skipped = false;
foreach ($skippedListeners as $skippedListener) {
$skippedListener = $this->unwrapListener($skippedListener, $eventId);
if ($skipped) {
$info = $this->getListenerInfo($skippedListener, $eventId, $eventName);
$this->logger->debug(sprintf('Listener "%s" was not called for event "%s".', $info['pretty'], $eventName));
}
if ($skippedListener === $listener) {
$skipped = true;
}
}
}
/**
* This is a private method.
*
* This method is public because it is used in a closure.
* Whenever Symfony will require PHP 5.4, this could be changed
* to a proper private method.
*/
public function preListenerCall($eventName, $eventId, $listener)
{
// is it the first called listener?
if (isset($this->firstCalledEvent[$eventName])) {
$this->firstCalledEvent[$eventName]->stop();
unset($this->firstCalledEvent[$eventName]);
}
$info = $this->getListenerInfo($listener, $eventId, $eventName);
if (null !== $this->logger) {
$this->logger->debug(sprintf('Notified event "%s" to listener "%s".', $eventName, $info['pretty']));
}
$this->called[$eventName.'.'.$info['pretty']] = $info;
return $this->stopwatch->start(isset($info['class']) ? $info['class'] : $info['type'], 'event_listener');
}
/**
* Returns information about the listener
*
* @param object $listener The listener
* @param int|null $eventId The event id
* @param string $eventName The event name
*
* @return array Information about the listener
*/
private function getListenerInfo($listener, $eventId, $eventName)
{
$listener = $this->unwrapListener($listener, $eventId);
$info = array(
'event' => $eventName,
);
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],
);
}
return $info;
}
/**
* Updates the stopwatch data in the profile hierarchy.
*
* @param string $token Profile token
* @param bool $updateChildren Whether to update the children altogether
*/
private function updateProfiles($token, $updateChildren)
{
if (!$this->profiler || !$profile = $this->profiler->loadProfile($token)) {
return;
}
$this->saveInfoInProfile($profile, $updateChildren);
}
/**
* Update the profiles with the timing and events information and saves them.
*
* @param Profile $profile The root profile
* @param bool $updateChildren Whether to update the children altogether
*/
private function saveInfoInProfile(Profile $profile, $updateChildren)
{
try {
$collector = $profile->getCollector('memory');
$collector->updateMemoryUsage();
} catch (\InvalidArgumentException $e) {
}
try {
$collector = $profile->getCollector('time');
$collector->setEvents($this->stopwatch->getSectionEvents($profile->getToken()));
} catch (\InvalidArgumentException $e) {
}
try {
$collector = $profile->getCollector('events');
$collector->setCalledListeners($this->getCalledListeners());
$collector->setNotCalledListeners($this->getNotCalledListeners());
} catch (\InvalidArgumentException $e) {
}
$this->profiler->saveProfile($profile);
if ($updateChildren) {
foreach ($profile->getChildren() as $child) {
$this->saveInfoInProfile($child, true);
}
}
}
private function preDispatch($eventName, $eventId, Event $event)
{
// wrap all listeners before they are called
$this->wrappedListeners[$eventId] = new \SplObjectStorage();
$listeners = $this->dispatcher->getListeners($eventName);
foreach ($listeners as $listener) {
$this->dispatcher->removeListener($eventName, $listener);
$wrapped = $this->wrapListener($eventName, $eventId, $listener);
$this->wrappedListeners[$eventId][$wrapped] = $listener;
$this->dispatcher->addListener($eventName, $wrapped);
}
switch ($eventName) {
case KernelEvents::REQUEST:
$this->stopwatch->openSection();
break;
case KernelEvents::VIEW:
case KernelEvents::RESPONSE:
// stop only if a controller has been executed
if ($this->stopwatch->isStarted('controller')) {
$this->stopwatch->stop('controller');
}
break;
case KernelEvents::TERMINATE:
$token = $event->getResponse()->headers->get('X-Debug-Token');
// There is a very special case when using builtin AppCache class as kernel wrapper, in the case
// of an ESI request leading to a `stale` response [B] inside a `fresh` cached response [A].
// In this case, `$token` contains the [B] debug token, but the open `stopwatch` section ID
// is equal to the [A] debug token. Trying to reopen section with the [B] token throws an exception
// which must be caught.
try {
$this->stopwatch->openSection($token);
} catch (\LogicException $e) {
}
break;
}
}
private function postDispatch($eventName, $eventId, Event $event)
{
switch ($eventName) {
case KernelEvents::CONTROLLER:
$this->stopwatch->start('controller', 'section');
break;
case KernelEvents::RESPONSE:
$token = $event->getResponse()->headers->get('X-Debug-Token');
$this->stopwatch->stopSection($token);
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
// The profiles can only be updated once they have been created
// that is after the 'kernel.response' event of the main request
$this->updateProfiles($token, true);
}
break;
case KernelEvents::TERMINATE:
$token = $event->getResponse()->headers->get('X-Debug-Token');
// In the special case described in the `preDispatch` method above, the `$token` section
// does not exist, then closing it throws an exception which must be caught.
try {
$this->stopwatch->stopSection($token);
} catch (\LogicException $e) {
}
// The children profiles have been updated by the previous 'kernel.response'
// event. Only the root profile need to be updated with the 'kernel.terminate'
// timing information.
$this->updateProfiles($token, false);
break;
}
foreach ($this->wrappedListeners[$eventId] as $wrapped) {
$this->dispatcher->removeListener($eventName, $wrapped);
$this->dispatcher->addListener($eventName, $this->wrappedListeners[$eventId][$wrapped]);
}
unset($this->wrappedListeners[$eventId]);
}
private function wrapListener($eventName, $eventId, $listener)
{
$self = $this;
return function (Event $event) use ($self, $eventName, $eventId, $listener) {
$e = $self->preListenerCall($eventName, $eventId, $listener);
call_user_func($listener, $event);
$e->stop();
if ($event->isPropagationStopped()) {
$self->logSkippedListeners($eventName, $eventId, $event, $listener);
}
};
}
private function unwrapListener($listener, $eventId)
{
// get the original listener
if (is_object($listener)) {
if (null === $eventId) {
foreach (array_keys($this->wrappedListeners) as $eventId) {
if (isset($this->wrappedListeners[$eventId][$listener])) {
return $this->wrappedListeners[$eventId][$listener];
}
}
} elseif (isset($this->wrappedListeners[$eventId][$listener])) {
return $this->wrappedListeners[$eventId][$listener];
}
}
return $listener;
}
}
|