Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

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.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

Profiler.php

Zuletzt modifiziert: 09.10.2024, 12:58 - Dateigröße: 7.02 KiB


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