Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
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 |
Profiler.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\Profiler;
013
014 use Psr\Log\LoggerInterface;
015 use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException;
016 use Symfony\Component\HttpFoundation\Request;
017 use Symfony\Component\HttpFoundation\Response;
018 use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
019 use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
020
021 /**
022 * Profiler.
023 *
024 * @author Fabien Potencier <fabien@symfony.com>
025 */
026 class Profiler
027 {
028 private $storage;
029
030 /**
031 * @var DataCollectorInterface[]
032 */
033 private $collectors = [];
034
035 private $logger;
036 private $initiallyEnabled = true;
037 private $enabled = true;
038
039 /**
040 * @param bool $enable The initial enabled state
041 */
042 public function __construct(ProfilerStorageInterface $storage, LoggerInterface $logger = null, $enable = true)
043 {
044 $this->storage = $storage;
045 $this->logger = $logger;
046 $this->initiallyEnabled = $this->enabled = (bool) $enable;
047 }
048
049 /**
050 * Disables the profiler.
051 */
052 public function disable()
053 {
054 $this->enabled = false;
055 }
056
057 /**
058 * Enables the profiler.
059 */
060 public function enable()
061 {
062 $this->enabled = true;
063 }
064
065 /**
066 * Loads the Profile for the given Response.
067 *
068 * @return Profile|null A Profile instance
069 */
070 public function loadProfileFromResponse(Response $response)
071 {
072 if (!$token = $response->headers->get('X-Debug-Token')) {
073 return null;
074 }
075
076 return $this->loadProfile($token);
077 }
078
079 /**
080 * Loads the Profile for the given token.
081 *
082 * @param string $token A token
083 *
084 * @return Profile|null A Profile instance
085 */
086 public function loadProfile($token)
087 {
088 return $this->storage->read($token);
089 }
090
091 /**
092 * Saves a Profile.
093 *
094 * @return bool
095 */
096 public function saveProfile(Profile $profile)
097 {
098 // late collect
099 foreach ($profile->getCollectors() as $collector) {
100 if ($collector instanceof LateDataCollectorInterface) {
101 $collector->lateCollect();
102 }
103 }
104
105 if (!($ret = $this->storage->write($profile)) && null !== $this->logger) {
106 $this->logger->warning('Unable to store the profiler information.', ['configured_storage' => \get_class($this->storage)]);
107 }
108
109 return $ret;
110 }
111
112 /**
113 * Purges all data from the storage.
114 */
115 public function purge()
116 {
117 $this->storage->purge();
118 }
119
120 /**
121 * Finds profiler tokens for the given criteria.
122 *
123 * @param string $ip The IP
124 * @param string $url The URL
125 * @param string $limit The maximum number of tokens to return
126 * @param string $method The request method
127 * @param string $start The start date to search from
128 * @param string $end The end date to search to
129 * @param string $statusCode The request status code
130 *
131 * @return array An array of tokens
132 *
133 * @see https://php.net/datetime.formats for the supported date/time formats
134 */
135 public function find($ip, $url, $limit, $method, $start, $end, $statusCode = null)
136 {
137 return $this->storage->find($ip, $url, $limit, $method, $this->getTimestamp($start), $this->getTimestamp($end), $statusCode);
138 }
139
140 /**
141 * Collects data for the given Response.
142 *
143 * @return Profile|null A Profile instance or null if the profiler is disabled
144 */
145 public function collect(Request $request, Response $response, \Exception $exception = null)
146 {
147 if (false === $this->enabled) {
148 return null;
149 }
150
151 $profile = new Profile(substr(hash('sha256', uniqid(mt_rand(), true)), 0, 6));
152 $profile->setTime(time());
153 $profile->setUrl($request->getUri());
154 $profile->setMethod($request->getMethod());
155 $profile->setStatusCode($response->getStatusCode());
156 try {
157 $profile->setIp($request->getClientIp());
158 } catch (ConflictingHeadersException $e) {
159 $profile->setIp('Unknown');
160 }
161
162 $response->headers->set('X-Debug-Token', $profile->getToken());
163
164 foreach ($this->collectors as $collector) {
165 $collector->collect($request, $response, $exception);
166
167 // we need to clone for sub-requests
168 $profile->addCollector(clone $collector);
169 }
170
171 return $profile;
172 }
173
174 public function reset()
175 {
176 foreach ($this->collectors as $collector) {
177 if (!method_exists($collector, 'reset')) {
178 continue;
179 }
180
181 $collector->reset();
182 }
183 $this->enabled = $this->initiallyEnabled;
184 }
185
186 /**
187 * Gets the Collectors associated with this profiler.
188 *
189 * @return array An array of collectors
190 */
191 public function all()
192 {
193 return $this->collectors;
194 }
195
196 /**
197 * Sets the Collectors associated with this profiler.
198 *
199 * @param DataCollectorInterface[] $collectors An array of collectors
200 */
201 public function set(array $collectors = [])
202 {
203 $this->collectors = [];
204 foreach ($collectors as $collector) {
205 $this->add($collector);
206 }
207 }
208
209 /**
210 * Adds a Collector.
211 */
212 public function add(DataCollectorInterface $collector)
213 {
214 if (!method_exists($collector, 'reset')) {
215 @trigger_error(sprintf('Implementing "%s" without the "reset()" method is deprecated since Symfony 3.4 and will be unsupported in 4.0 for class "%s".', DataCollectorInterface::class, \get_class($collector)), \E_USER_DEPRECATED);
216 }
217
218 $this->collectors[$collector->getName()] = $collector;
219 }
220
221 /**
222 * Returns true if a Collector for the given name exists.
223 *
224 * @param string $name A collector name
225 *
226 * @return bool
227 */
228 public function has($name)
229 {
230 return isset($this->collectors[$name]);
231 }
232
233 /**
234 * Gets a Collector by name.
235 *
236 * @param string $name A collector name
237 *
238 * @return DataCollectorInterface A DataCollectorInterface instance
239 *
240 * @throws \InvalidArgumentException if the collector does not exist
241 */
242 public function get($name)
243 {
244 if (!isset($this->collectors[$name])) {
245 throw new \InvalidArgumentException(sprintf('Collector "%s" does not exist.', $name));
246 }
247
248 return $this->collectors[$name];
249 }
250
251 /**
252 * @return int|null
253 */
254 private function getTimestamp($value)
255 {
256 if (null === $value || '' == $value) {
257 return null;
258 }
259
260 try {
261 $value = new \DateTime(is_numeric($value) ? '@'.$value : $value);
262 } catch (\Exception $e) {
263 return null;
264 }
265
266 return $value->getTimestamp();
267 }
268 }
269