123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- namespace Symfony\Components\HttpKernel\Profiler;
- use Symfony\Components\HttpKernel\Response;
- use Symfony\Components\HttpKernel\Profiler\ProfilerStorage;
- use Symfony\Components\HttpKernel\Profiler\DataCollector\DataCollectorInterface;
- use Symfony\Components\HttpKernel\LoggerInterface;
- /*
- * This file is part of the Symfony framework.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- /**
- * Profiler.
- *
- * @package Symfony
- * @subpackage Components_HttpKernel
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- */
- class Profiler implements \ArrayAccess
- {
- protected $profilerStorage;
- protected $collectors;
- protected $response;
- protected $logger;
- public function __construct(ProfilerStorage $profilerStorage, LoggerInterface $logger = null)
- {
- $this->profilerStorage = $profilerStorage;
- $this->logger = $logger;
- $this->collectors = array();
- }
- /**
- * Clones the Profiler instance.
- */
- public function __clone()
- {
- $this->profilerStorage = clone $this->profilerStorage;
- }
- /**
- * Returns a new Profiler for the given Response.
- *
- * @param Symfony\Components\HttpKernel\Response $response A Response instance
- *
- * @return Symfony\Components\HttpKernel\Profiler\Profiler A new Profiler instance
- */
- public function load(Response $response)
- {
- if (!$token = $response->headers->get('X-Debug-Token')) {
- return null;
- }
- return $this->getProfilerForToken($token);
- }
- /**
- * Returns a new Profiler for the given token.
- *
- * @param string $token A token
- *
- * @return Symfony\Components\HttpKernel\Profiler\Profiler A new Profiler instance
- */
- public function getProfilerForToken($token)
- {
- $profiler = clone $this;
- $profiler->profilerStorage->setToken($token);
- $profiler->loadCollectorData();
- return $profiler;
- }
- /**
- * Collects data for the given Response.
- *
- * @param Symfony\Components\HttpKernel\Response $response A Response instance
- */
- public function collect(Response $response)
- {
- $this->response = $response;
- $this->response->headers->set('X-Debug-Token', $this->profilerStorage->getToken());
- $data = array();
- foreach ($this->collectors as $name => $collector) {
- $collector->collect();
- $data[$name] = $collector->getData();
- }
- try {
- $this->profilerStorage->write($data);
- $this->profilerStorage->purge();
- } catch (\Exception $e) {
- if (null !== $this->logger) {
- $this->logger->err('Unable to store the profiler information.');
- }
- }
- }
- /**
- * Loads the data stored in the storage for all collectors.
- */
- public function loadCollectorData()
- {
- try {
- foreach ($this->collectors as $name => $collector) {
- $collector->setData($this->profilerStorage->getData($name));
- }
- } catch (\Exception $e) {
- if (null !== $this->logger) {
- $this->logger->err('Unable to read the profiler information.');
- }
- }
- }
- /**
- * Gets the profiler storage.
- *
- * @return Symfony\Components\HttpKernel\Profiler\ProfilerStorage A ProfilerStorage instance
- */
- public function getProfilerStorage()
- {
- return $this->profilerStorage;
- }
- /**
- * Gets the Response.
- *
- * @return Symfony\Components\HttpKernel\Response A Response instance
- */
- public function getResponse()
- {
- return $this->response;
- }
- /**
- * Gets the Collectors associated with this profiler.
- *
- * @return array An array of collectors
- */
- public function getCollectors()
- {
- return $this->collectors;
- }
- /**
- * Sets the Collectors associated with this profiler.
- *
- * @param array $collectors An array of collectors
- */
- public function setCollectors(array $collectors = array())
- {
- $this->collectors = array();
- foreach ($collectors as $name => $collector) {
- $this->addCollector($collector);
- }
- }
- /**
- * Adds a Collector.
- *
- * @param Symfony\Components\HttpKernel\Profiler\DataCollector\DataCollectorInterface $collector A DataCollectorInterface instance
- */
- public function addCollector(DataCollectorInterface $collector)
- {
- $this->collectors[$collector->getName()] = $collector;
- }
- /**
- * Returns true if a Collector for the given name exists.
- *
- * @param string $name A collector name
- */
- public function hasCollector($name)
- {
- return isset($this->collectors[$name]);
- }
- /**
- * Gets a Collector by name.
- *
- * @param string $name A collector name
- *
- * @return Symfony\Components\HttpKernel\Profiler\DataCollector\DataCollectorInterface A DataCollectorInterface instance
- *
- * @throws \InvalidArgumentException if the collector does not exist
- */
- public function getCollector($name)
- {
- if (!isset($this->collectors[$name])) {
- throw new \InvalidArgumentException(sprintf('Collector "%s" does not exist.', $name));
- }
- return $this->collectors[$name];
- }
- /**
- * Returns true if the named collector exists.
- *
- * @param string $name The collector name
- *
- * @param Boolean true if the collector exists, false otherwise
- */
- public function offsetExists($name)
- {
- return $this->hasCollector($name);
- }
- /**
- * Gets a collector.
- *
- * @param string $name The collector name
- *
- * @throws \InvalidArgumentException if the collector does not exist
- */
- public function offsetGet($name)
- {
- return $this->getCollector($name);
- }
- /**
- * Unimplemented.
- *
- * @param string $name The collector name
- * @param string|array $value The collector
- *
- * @throws \LogicException
- */
- public function offsetSet($name, $value)
- {
- throw new \LogicException('A Collector cannot be set.');
- }
- /**
- * Unimplemented.
- *
- * @param string $name The collector name
- *
- * @throws \LogicException
- */
- public function offsetUnset($name)
- {
- throw new \LogicException('A Collector cannot be removed.');
- }
- }
|