4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\EventListener;
14 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
15 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
16 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
17 use Symfony\Component\HttpKernel\Event\PostResponseEvent;
18 use Symfony\Component\HttpKernel\KernelEvents;
19 use Symfony\Component\HttpKernel\Profiler\Profiler;
20 use Symfony\Component\HttpFoundation\RequestMatcherInterface;
21 use Symfony\Component\HttpFoundation\RequestStack;
22 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
25 * ProfilerListener collects data for the current request by listening to the kernel events.
27 * @author Fabien Potencier <fabien@symfony.com>
29 class ProfilerListener implements EventSubscriberInterface
33 protected $onlyException;
34 protected $onlyMasterRequests;
36 protected $requests = array();
38 protected $requestStack;
44 * @param Profiler $profiler A Profiler instance
45 * @param RequestStack $requestStack A RequestStack instance
46 * @param RequestMatcherInterface|null $matcher A RequestMatcher instance
47 * @param bool $onlyException true if the profiler only collects data when an exception occurs, false otherwise
48 * @param bool $onlyMasterRequests true if the profiler only collects data when the request is a master request, false otherwise
50 public function __construct(Profiler $profiler, $requestStack = null, $matcher = null, $onlyException = false, $onlyMasterRequests = false)
52 if ($requestStack instanceof RequestMatcherInterface || (null !== $matcher && !$matcher instanceof RequestMatcherInterface) || $onlyMasterRequests instanceof RequestStack) {
53 $tmp = $onlyMasterRequests;
54 $onlyMasterRequests = $onlyException;
55 $onlyException = $matcher;
56 $matcher = $requestStack;
57 $requestStack = func_num_args() < 5 ? null : $tmp;
59 @trigger_error('The '.__METHOD__.' method now requires a RequestStack to be given as second argument as '.__CLASS__.'::onKernelRequest method will be removed in 3.0.', E_USER_DEPRECATED);
60 } elseif (!$requestStack instanceof RequestStack) {
61 @trigger_error('The '.__METHOD__.' method now requires a RequestStack instance as '.__CLASS__.'::onKernelRequest method will be removed in 3.0.', E_USER_DEPRECATED);
64 if (null !== $requestStack && !$requestStack instanceof RequestStack) {
65 throw new \InvalidArgumentException('RequestStack instance expected.');
67 if (null !== $matcher && !$matcher instanceof RequestMatcherInterface) {
68 throw new \InvalidArgumentException('Matcher must implement RequestMatcherInterface.');
71 $this->profiler = $profiler;
72 $this->matcher = $matcher;
73 $this->onlyException = (bool) $onlyException;
74 $this->onlyMasterRequests = (bool) $onlyMasterRequests;
75 $this->profiles = new \SplObjectStorage();
76 $this->parents = new \SplObjectStorage();
77 $this->requestStack = $requestStack;
81 * Handles the onKernelException event.
83 * @param GetResponseForExceptionEvent $event A GetResponseForExceptionEvent instance
85 public function onKernelException(GetResponseForExceptionEvent $event)
87 if ($this->onlyMasterRequests && !$event->isMasterRequest()) {
91 $this->exception = $event->getException();
95 * @deprecated since version 2.4, to be removed in 3.0.
97 public function onKernelRequest(GetResponseEvent $event)
99 if (null === $this->requestStack) {
100 $this->requests[] = $event->getRequest();
105 * Handles the onKernelResponse event.
107 * @param FilterResponseEvent $event A FilterResponseEvent instance
109 public function onKernelResponse(FilterResponseEvent $event)
111 $master = $event->isMasterRequest();
112 if ($this->onlyMasterRequests && !$master) {
116 if ($this->onlyException && null === $this->exception) {
120 $request = $event->getRequest();
121 $exception = $this->exception;
122 $this->exception = null;
124 if (null !== $this->matcher && !$this->matcher->matches($request)) {
128 if (!$profile = $this->profiler->collect($request, $event->getResponse(), $exception)) {
132 $this->profiles[$request] = $profile;
134 if (null !== $this->requestStack) {
135 $this->parents[$request] = $this->requestStack->getParentRequest();
136 } elseif (!$master) {
137 // to be removed when requestStack is required
138 array_pop($this->requests);
140 $this->parents[$request] = end($this->requests);
144 public function onKernelTerminate(PostResponseEvent $event)
146 // attach children to parents
147 foreach ($this->profiles as $request) {
148 // isset call should be removed when requestStack is required
149 if (isset($this->parents[$request]) && null !== $parentRequest = $this->parents[$request]) {
150 if (isset($this->profiles[$parentRequest])) {
151 $this->profiles[$parentRequest]->addChild($this->profiles[$request]);
157 foreach ($this->profiles as $request) {
158 $this->profiler->saveProfile($this->profiles[$request]);
161 $this->profiles = new \SplObjectStorage();
162 $this->parents = new \SplObjectStorage();
163 $this->requests = array();
166 public static function getSubscribedEvents()
169 // kernel.request must be registered as early as possible to not break
170 // when an exception is thrown in any other kernel.request listener
171 KernelEvents::REQUEST => array('onKernelRequest', 1024),
172 KernelEvents::RESPONSE => array('onKernelResponse', -100),
173 KernelEvents::EXCEPTION => 'onKernelException',
174 KernelEvents::TERMINATE => array('onKernelTerminate', -1024),