Skip to content

[HttpKernel][EventDispatcher] Wrap some log contexts with ClassStub #19829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\VarDumper\Caster\ClassStub;
use Psr\Log\LoggerInterface;

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

/**
* Constructor.
Expand All @@ -47,6 +49,7 @@ public function __construct(EventDispatcherInterface $dispatcher, Stopwatch $sto
$this->logger = $logger;
$this->called = array();
$this->wrappedListeners = array();
$this->useClassStub = class_exists(ClassStub::class);
}

/**
Expand Down Expand Up @@ -261,10 +264,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 = $this->getListenerInfo($listener->getWrappedListener(), $eventName);
$context = array('event' => $eventName, 'listener' => $this->useClassStub ? new ClassStub($context['pretty'], $listener->getWrappedListener()) : $context['pretty']);
}
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 @@ -275,12 +281,12 @@ 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Symfony\Component\Routing\RequestContextAwareInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\VarDumper\Caster\ClassStub;

/**
* Initializes the context from the request and sets request attributes based on a matching route.
Expand Down Expand Up @@ -63,6 +64,7 @@ public function __construct($matcher, RequestStack $requestStack, RequestContext
$this->context = $context ?: $matcher->getContext();
$this->requestStack = $requestStack;
$this->logger = $logger;
$this->useClassStub = class_exists(ClassStub::class);
}

private function setCurrentRequest(Request $request = null)
Expand Down Expand Up @@ -102,8 +104,12 @@ public function onKernelRequest(GetResponseEvent $event)
} else {
$parameters = $this->matcher->match($request->getPathInfo());
}
$request->attributes->add($parameters);

if (null !== $this->logger) {
if (isset($parameters['_controller']) && $this->useClassStub) {
$parameters['_controller'] = ClassStub::wrapCallable($parameters['_controller']);
}
$this->logger->info('Matched route "{route}".', array(
'route' => isset($parameters['_route']) ? $parameters['_route'] : 'n/a',
'route_parameters' => $parameters,
Expand All @@ -112,7 +118,6 @@ public function onKernelRequest(GetResponseEvent $event)
));
}

$request->attributes->add($parameters);
unset($parameters['_route'], $parameters['_controller']);
$request->attributes->set('_route_params', $parameters);
} catch (ResourceNotFoundException $e) {
Expand Down