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\Profiler;
14 use Psr\Log\LoggerInterface;
15 use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException;
16 use Symfony\Component\HttpFoundation\Request;
17 use Symfony\Component\HttpFoundation\Response;
18 use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
19 use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
24 * @author Fabien Potencier <fabien@symfony.com>
31 * @var DataCollectorInterface[]
33 private $collectors = array();
36 private $initiallyEnabled = true;
37 private $enabled = true;
40 * @param bool $enable The initial enabled state
42 public function __construct(ProfilerStorageInterface $storage, LoggerInterface $logger = null, $enable = true)
44 $this->storage = $storage;
45 $this->logger = $logger;
46 $this->initiallyEnabled = $this->enabled = (bool) $enable;
50 * Disables the profiler.
52 public function disable()
54 $this->enabled = false;
58 * Enables the profiler.
60 public function enable()
62 $this->enabled = true;
66 * Loads the Profile for the given Response.
68 * @return Profile|false A Profile instance
70 public function loadProfileFromResponse(Response $response)
72 if (!$token = $response->headers->get('X-Debug-Token')) {
76 return $this->loadProfile($token);
80 * Loads the Profile for the given token.
82 * @param string $token A token
84 * @return Profile A Profile instance
86 public function loadProfile($token)
88 return $this->storage->read($token);
96 public function saveProfile(Profile $profile)
99 foreach ($profile->getCollectors() as $collector) {
100 if ($collector instanceof LateDataCollectorInterface) {
101 $collector->lateCollect();
105 if (!($ret = $this->storage->write($profile)) && null !== $this->logger) {
106 $this->logger->warning('Unable to store the profiler information.', array('configured_storage' => \get_class($this->storage)));
113 * Purges all data from the storage.
115 public function purge()
117 $this->storage->purge();
121 * Finds profiler tokens for the given criteria.
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
131 * @return array An array of tokens
133 * @see http://php.net/manual/en/datetime.formats.php for the supported date/time formats
135 public function find($ip, $url, $limit, $method, $start, $end, $statusCode = null)
137 return $this->storage->find($ip, $url, $limit, $method, $this->getTimestamp($start), $this->getTimestamp($end), $statusCode);
141 * Collects data for the given Response.
143 * @return Profile|null A Profile instance or null if the profiler is disabled
145 public function collect(Request $request, Response $response, \Exception $exception = null)
147 if (false === $this->enabled) {
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());
157 $profile->setIp($request->getClientIp());
158 } catch (ConflictingHeadersException $e) {
159 $profile->setIp('Unknown');
162 $response->headers->set('X-Debug-Token', $profile->getToken());
164 foreach ($this->collectors as $collector) {
165 $collector->collect($request, $response, $exception);
167 // we need to clone for sub-requests
168 $profile->addCollector(clone $collector);
174 public function reset()
176 foreach ($this->collectors as $collector) {
177 if (!method_exists($collector, 'reset')) {
183 $this->enabled = $this->initiallyEnabled;
187 * Gets the Collectors associated with this profiler.
189 * @return array An array of collectors
191 public function all()
193 return $this->collectors;
197 * Sets the Collectors associated with this profiler.
199 * @param DataCollectorInterface[] $collectors An array of collectors
201 public function set(array $collectors = array())
203 $this->collectors = array();
204 foreach ($collectors as $collector) {
205 $this->add($collector);
212 public function add(DataCollectorInterface $collector)
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);
218 $this->collectors[$collector->getName()] = $collector;
222 * Returns true if a Collector for the given name exists.
224 * @param string $name A collector name
228 public function has($name)
230 return isset($this->collectors[$name]);
234 * Gets a Collector by name.
236 * @param string $name A collector name
238 * @return DataCollectorInterface A DataCollectorInterface instance
240 * @throws \InvalidArgumentException if the collector does not exist
242 public function get($name)
244 if (!isset($this->collectors[$name])) {
245 throw new \InvalidArgumentException(sprintf('Collector "%s" does not exist.', $name));
248 return $this->collectors[$name];
251 private function getTimestamp($value)
253 if (null === $value || '' == $value) {
258 $value = new \DateTime(is_numeric($value) ? '@'.$value : $value);
259 } catch (\Exception $e) {
263 return $value->getTimestamp();