[ Index ] |
PHP Cross Reference of phpBB-3.2.11-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12 namespace Symfony\Component\HttpKernel\EventListener; 13 14 use Symfony\Component\EventDispatcher\EventSubscriberInterface; 15 use Symfony\Component\HttpFoundation\RequestMatcherInterface; 16 use Symfony\Component\HttpFoundation\RequestStack; 17 use Symfony\Component\HttpKernel\Event\FilterResponseEvent; 18 use Symfony\Component\HttpKernel\Event\GetResponseEvent; 19 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; 20 use Symfony\Component\HttpKernel\Event\PostResponseEvent; 21 use Symfony\Component\HttpKernel\KernelEvents; 22 use Symfony\Component\HttpKernel\Profiler\Profiler; 23 24 /** 25 * ProfilerListener collects data for the current request by listening to the kernel events. 26 * 27 * @author Fabien Potencier <fabien@symfony.com> 28 */ 29 class ProfilerListener implements EventSubscriberInterface 30 { 31 protected $profiler; 32 protected $matcher; 33 protected $onlyException; 34 protected $onlyMasterRequests; 35 protected $exception; 36 protected $requests = array(); 37 protected $profiles; 38 protected $requestStack; 39 protected $parents; 40 41 /** 42 * @param Profiler $profiler A Profiler instance 43 * @param RequestStack $requestStack A RequestStack instance 44 * @param RequestMatcherInterface|null $matcher A RequestMatcher instance 45 * @param bool $onlyException True if the profiler only collects data when an exception occurs, false otherwise 46 * @param bool $onlyMasterRequests True if the profiler only collects data when the request is a master request, false otherwise 47 */ 48 public function __construct(Profiler $profiler, $requestStack = null, $matcher = null, $onlyException = false, $onlyMasterRequests = false) 49 { 50 if ($requestStack instanceof RequestMatcherInterface || (null !== $matcher && !$matcher instanceof RequestMatcherInterface) || $onlyMasterRequests instanceof RequestStack) { 51 $tmp = $onlyMasterRequests; 52 $onlyMasterRequests = $onlyException; 53 $onlyException = $matcher; 54 $matcher = $requestStack; 55 $requestStack = \func_num_args() < 5 ? null : $tmp; 56 57 @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); 58 } elseif (!$requestStack instanceof RequestStack) { 59 @trigger_error('The '.__METHOD__.' method now requires a RequestStack instance as '.__CLASS__.'::onKernelRequest method will be removed in 3.0.', E_USER_DEPRECATED); 60 } 61 62 if (null !== $requestStack && !$requestStack instanceof RequestStack) { 63 throw new \InvalidArgumentException('RequestStack instance expected.'); 64 } 65 if (null !== $matcher && !$matcher instanceof RequestMatcherInterface) { 66 throw new \InvalidArgumentException('Matcher must implement RequestMatcherInterface.'); 67 } 68 69 $this->profiler = $profiler; 70 $this->matcher = $matcher; 71 $this->onlyException = (bool) $onlyException; 72 $this->onlyMasterRequests = (bool) $onlyMasterRequests; 73 $this->profiles = new \SplObjectStorage(); 74 $this->parents = new \SplObjectStorage(); 75 $this->requestStack = $requestStack; 76 } 77 78 /** 79 * Handles the onKernelException event. 80 */ 81 public function onKernelException(GetResponseForExceptionEvent $event) 82 { 83 if ($this->onlyMasterRequests && !$event->isMasterRequest()) { 84 return; 85 } 86 87 $this->exception = $event->getException(); 88 } 89 90 /** 91 * @deprecated since version 2.4, to be removed in 3.0. 92 */ 93 public function onKernelRequest(GetResponseEvent $event) 94 { 95 if (null === $this->requestStack) { 96 $this->requests[] = $event->getRequest(); 97 } 98 } 99 100 /** 101 * Handles the onKernelResponse event. 102 */ 103 public function onKernelResponse(FilterResponseEvent $event) 104 { 105 $master = $event->isMasterRequest(); 106 if ($this->onlyMasterRequests && !$master) { 107 return; 108 } 109 110 if ($this->onlyException && null === $this->exception) { 111 return; 112 } 113 114 $request = $event->getRequest(); 115 $exception = $this->exception; 116 $this->exception = null; 117 118 if (null !== $this->matcher && !$this->matcher->matches($request)) { 119 return; 120 } 121 122 if (!$profile = $this->profiler->collect($request, $event->getResponse(), $exception)) { 123 return; 124 } 125 126 $this->profiles[$request] = $profile; 127 128 if (null !== $this->requestStack) { 129 $this->parents[$request] = $this->requestStack->getParentRequest(); 130 } elseif (!$master) { 131 // to be removed when requestStack is required 132 array_pop($this->requests); 133 134 $this->parents[$request] = end($this->requests); 135 } 136 } 137 138 public function onKernelTerminate(PostResponseEvent $event) 139 { 140 // attach children to parents 141 foreach ($this->profiles as $request) { 142 // isset call should be removed when requestStack is required 143 if (isset($this->parents[$request]) && null !== $parentRequest = $this->parents[$request]) { 144 if (isset($this->profiles[$parentRequest])) { 145 $this->profiles[$parentRequest]->addChild($this->profiles[$request]); 146 } 147 } 148 } 149 150 // save profiles 151 foreach ($this->profiles as $request) { 152 $this->profiler->saveProfile($this->profiles[$request]); 153 } 154 155 $this->profiles = new \SplObjectStorage(); 156 $this->parents = new \SplObjectStorage(); 157 $this->requests = array(); 158 } 159 160 public static function getSubscribedEvents() 161 { 162 return array( 163 // kernel.request must be registered as early as possible to not break 164 // when an exception is thrown in any other kernel.request listener 165 KernelEvents::REQUEST => array('onKernelRequest', 1024), 166 KernelEvents::RESPONSE => array('onKernelResponse', -100), 167 KernelEvents::EXCEPTION => 'onKernelException', 168 KernelEvents::TERMINATE => array('onKernelTerminate', -1024), 169 ); 170 } 171 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 11 20:33:01 2020 | Cross-referenced by PHPXref 0.7.1 |