Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
ProfilerListener.php
001 <?php
002
003 /*
004 * This file is part of the Symfony package.
005 *
006 * (c) Fabien Potencier <fabien@symfony.com>
007 *
008 * For the full copyright and license information, please view the LICENSE
009 * file that was distributed with this source code.
010 */
011
012 namespace Symfony\Component\HttpKernel\EventListener;
013
014 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
015 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
016 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
017 use Symfony\Component\HttpKernel\Event\PostResponseEvent;
018 use Symfony\Component\HttpKernel\KernelEvents;
019 use Symfony\Component\HttpKernel\Profiler\Profiler;
020 use Symfony\Component\HttpFoundation\RequestMatcherInterface;
021 use Symfony\Component\HttpFoundation\RequestStack;
022 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
023
024 /**
025 * ProfilerListener collects data for the current request by listening to the kernel events.
026 *
027 * @author Fabien Potencier <fabien@symfony.com>
028 */
029 class ProfilerListener implements EventSubscriberInterface
030 {
031 protected $profiler;
032 protected $matcher;
033 protected $onlyException;
034 protected $onlyMasterRequests;
035 protected $exception;
036 protected $requests = array();
037 protected $profiles;
038 protected $requestStack;
039 protected $parents;
040
041 /**
042 * Constructor.
043 *
044 * @param Profiler $profiler A Profiler instance
045 * @param RequestStack $requestStack A RequestStack instance
046 * @param RequestMatcherInterface|null $matcher A RequestMatcher instance
047 * @param bool $onlyException true if the profiler only collects data when an exception occurs, false otherwise
048 * @param bool $onlyMasterRequests true if the profiler only collects data when the request is a master request, false otherwise
049 */
050 public function __construct(Profiler $profiler, $requestStack = null, $matcher = null, $onlyException = false, $onlyMasterRequests = false)
051 {
052 if ($requestStack instanceof RequestMatcherInterface || (null !== $matcher && !$matcher instanceof RequestMatcherInterface) || $onlyMasterRequests instanceof RequestStack) {
053 $tmp = $onlyMasterRequests;
054 $onlyMasterRequests = $onlyException;
055 $onlyException = $matcher;
056 $matcher = $requestStack;
057 $requestStack = func_num_args() < 5 ? null : $tmp;
058
059 @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);
060 } elseif (!$requestStack instanceof RequestStack) {
061 @trigger_error('The '.__METHOD__.' method now requires a RequestStack instance as '.__CLASS__.'::onKernelRequest method will be removed in 3.0.', E_USER_DEPRECATED);
062 }
063
064 if (null !== $requestStack && !$requestStack instanceof RequestStack) {
065 throw new \InvalidArgumentException('RequestStack instance expected.');
066 }
067 if (null !== $matcher && !$matcher instanceof RequestMatcherInterface) {
068 throw new \InvalidArgumentException('Matcher must implement RequestMatcherInterface.');
069 }
070
071 $this->profiler = $profiler;
072 $this->matcher = $matcher;
073 $this->onlyException = (bool) $onlyException;
074 $this->onlyMasterRequests = (bool) $onlyMasterRequests;
075 $this->profiles = new \SplObjectStorage();
076 $this->parents = new \SplObjectStorage();
077 $this->requestStack = $requestStack;
078 }
079
080 /**
081 * Handles the onKernelException event.
082 *
083 * @param GetResponseForExceptionEvent $event A GetResponseForExceptionEvent instance
084 */
085 public function onKernelException(GetResponseForExceptionEvent $event)
086 {
087 if ($this->onlyMasterRequests && !$event->isMasterRequest()) {
088 return;
089 }
090
091 $this->exception = $event->getException();
092 }
093
094 /**
095 * @deprecated since version 2.4, to be removed in 3.0.
096 */
097 public function onKernelRequest(GetResponseEvent $event)
098 {
099 if (null === $this->requestStack) {
100 $this->requests[] = $event->getRequest();
101 }
102 }
103
104 /**
105 * Handles the onKernelResponse event.
106 *
107 * @param FilterResponseEvent $event A FilterResponseEvent instance
108 */
109 public function onKernelResponse(FilterResponseEvent $event)
110 {
111 $master = $event->isMasterRequest();
112 if ($this->onlyMasterRequests && !$master) {
113 return;
114 }
115
116 if ($this->onlyException && null === $this->exception) {
117 return;
118 }
119
120 $request = $event->getRequest();
121 $exception = $this->exception;
122 $this->exception = null;
123
124 if (null !== $this->matcher && !$this->matcher->matches($request)) {
125 return;
126 }
127
128 if (!$profile = $this->profiler->collect($request, $event->getResponse(), $exception)) {
129 return;
130 }
131
132 $this->profiles[$request] = $profile;
133
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);
139
140 $this->parents[$request] = end($this->requests);
141 }
142 }
143
144 public function onKernelTerminate(PostResponseEvent $event)
145 {
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]);
152 }
153 }
154 }
155
156 // save profiles
157 foreach ($this->profiles as $request) {
158 $this->profiler->saveProfile($this->profiles[$request]);
159 }
160
161 $this->profiles = new \SplObjectStorage();
162 $this->parents = new \SplObjectStorage();
163 $this->requests = array();
164 }
165
166 public static function getSubscribedEvents()
167 {
168 return array(
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),
175 );
176 }
177 }
178