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 Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpFoundation\Response;
17 use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
18 use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
19 use Psr\Log\LoggerInterface;
24 * @author Fabien Potencier <fabien@symfony.com>
29 * @var ProfilerStorageInterface
34 * @var DataCollectorInterface[]
36 private $collectors = array();
39 * @var LoggerInterface
46 private $enabled = true;
51 * @param ProfilerStorageInterface $storage A ProfilerStorageInterface instance
52 * @param LoggerInterface $logger A LoggerInterface instance
54 public function __construct(ProfilerStorageInterface $storage, LoggerInterface $logger = null)
56 $this->storage = $storage;
57 $this->logger = $logger;
61 * Disables the profiler.
63 public function disable()
65 $this->enabled = false;
69 * Enables the profiler.
71 public function enable()
73 $this->enabled = true;
77 * Loads the Profile for the given Response.
79 * @param Response $response A Response instance
81 * @return Profile|false A Profile instance
83 public function loadProfileFromResponse(Response $response)
85 if (!$token = $response->headers->get('X-Debug-Token')) {
89 return $this->loadProfile($token);
93 * Loads the Profile for the given token.
95 * @param string $token A token
97 * @return Profile A Profile instance
99 public function loadProfile($token)
101 return $this->storage->read($token);
107 * @param Profile $profile A Profile instance
111 public function saveProfile(Profile $profile)
114 foreach ($profile->getCollectors() as $collector) {
115 if ($collector instanceof LateDataCollectorInterface) {
116 $collector->lateCollect();
120 if (!($ret = $this->storage->write($profile)) && null !== $this->logger) {
121 $this->logger->warning('Unable to store the profiler information.', array('configured_storage' => get_class($this->storage)));
128 * Purges all data from the storage.
130 public function purge()
132 $this->storage->purge();
136 * Finds profiler tokens for the given criteria.
138 * @param string $ip The IP
139 * @param string $url The URL
140 * @param string $limit The maximum number of tokens to return
141 * @param string $method The request method
142 * @param string $start The start date to search from
143 * @param string $end The end date to search to
144 * @param string $statusCode The request status code
146 * @return array An array of tokens
148 * @see http://php.net/manual/en/datetime.formats.php for the supported date/time formats
150 public function find($ip, $url, $limit, $method, $start, $end, $statusCode = null)
152 return $this->storage->find($ip, $url, $limit, $method, $this->getTimestamp($start), $this->getTimestamp($end), $statusCode);
156 * Collects data for the given Response.
158 * @param Request $request A Request instance
159 * @param Response $response A Response instance
160 * @param \Exception $exception An exception instance if the request threw one
162 * @return Profile|null A Profile instance or null if the profiler is disabled
164 public function collect(Request $request, Response $response, \Exception $exception = null)
166 if (false === $this->enabled) {
170 $profile = new Profile(substr(hash('sha256', uniqid(mt_rand(), true)), 0, 6));
171 $profile->setTime(time());
172 $profile->setUrl($request->getUri());
173 $profile->setMethod($request->getMethod());
174 $profile->setStatusCode($response->getStatusCode());
176 $profile->setIp($request->getClientIp());
177 } catch (ConflictingHeadersException $e) {
178 $profile->setIp('Unknown');
181 $response->headers->set('X-Debug-Token', $profile->getToken());
183 foreach ($this->collectors as $collector) {
184 $collector->collect($request, $response, $exception);
186 // we need to clone for sub-requests
187 $profile->addCollector(clone $collector);
194 * Gets the Collectors associated with this profiler.
196 * @return array An array of collectors
198 public function all()
200 return $this->collectors;
204 * Sets the Collectors associated with this profiler.
206 * @param DataCollectorInterface[] $collectors An array of collectors
208 public function set(array $collectors = array())
210 $this->collectors = array();
211 foreach ($collectors as $collector) {
212 $this->add($collector);
219 * @param DataCollectorInterface $collector A DataCollectorInterface instance
221 public function add(DataCollectorInterface $collector)
223 $this->collectors[$collector->getName()] = $collector;
227 * Returns true if a Collector for the given name exists.
229 * @param string $name A collector name
233 public function has($name)
235 return isset($this->collectors[$name]);
239 * Gets a Collector by name.
241 * @param string $name A collector name
243 * @return DataCollectorInterface A DataCollectorInterface instance
245 * @throws \InvalidArgumentException if the collector does not exist
247 public function get($name)
249 if (!isset($this->collectors[$name])) {
250 throw new \InvalidArgumentException(sprintf('Collector "%s" does not exist.', $name));
253 return $this->collectors[$name];
256 private function getTimestamp($value)
258 if (null === $value || '' == $value) {
263 $value = new \DateTime(is_numeric($value) ? '@'.$value : $value);
264 } catch (\Exception $e) {
268 return $value->getTimestamp();