2010-06-16 09:38:41 +01:00
|
|
|
<?php
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Component\HttpKernel\Profiler;
|
2010-06-16 09:38:41 +01:00
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
use Symfony\Component\HttpKernel\Profiler\ProfilerStorage;
|
|
|
|
use Symfony\Component\HttpKernel\Profiler\DataCollector\DataCollectorInterface;
|
|
|
|
use Symfony\Component\HttpKernel\Log\LoggerInterface;
|
2010-06-16 09:38:41 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*/
|
|
|
|
class Profiler implements \ArrayAccess
|
|
|
|
{
|
|
|
|
protected $profilerStorage;
|
|
|
|
protected $collectors;
|
|
|
|
protected $response;
|
|
|
|
protected $logger;
|
|
|
|
|
2010-06-16 13:19:46 +01:00
|
|
|
public function __construct(ProfilerStorage $profilerStorage, LoggerInterface $logger = null)
|
2010-06-16 09:38:41 +01:00
|
|
|
{
|
|
|
|
$this->profilerStorage = $profilerStorage;
|
|
|
|
$this->logger = $logger;
|
2010-06-16 13:19:46 +01:00
|
|
|
$this->collectors = array();
|
2010-06-16 09:38:41 +01:00
|
|
|
}
|
|
|
|
|
2010-06-16 14:45:20 +01:00
|
|
|
/**
|
|
|
|
* Clones the Profiler instance.
|
|
|
|
*/
|
2010-06-16 09:38:41 +01:00
|
|
|
public function __clone()
|
|
|
|
{
|
|
|
|
$this->profilerStorage = clone $this->profilerStorage;
|
|
|
|
}
|
|
|
|
|
2010-06-16 14:45:20 +01:00
|
|
|
/**
|
|
|
|
* Returns a new Profiler for the given Response.
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @param Response $response A Response instance
|
2010-06-16 14:45:20 +01:00
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Profiler A new Profiler instance
|
2010-06-16 14:45:20 +01:00
|
|
|
*/
|
2010-06-16 09:38:41 +01:00
|
|
|
public function load(Response $response)
|
|
|
|
{
|
2010-06-16 14:45:20 +01:00
|
|
|
if (!$token = $response->headers->get('X-Debug-Token')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getProfilerForToken($token);
|
2010-06-16 09:38:41 +01:00
|
|
|
}
|
|
|
|
|
2010-06-16 14:45:20 +01:00
|
|
|
/**
|
|
|
|
* Returns a new Profiler for the given token.
|
|
|
|
*
|
|
|
|
* @param string $token A token
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Profiler A new Profiler instance
|
2010-06-16 14:45:20 +01:00
|
|
|
*/
|
2010-06-16 09:38:41 +01:00
|
|
|
public function getProfilerForToken($token)
|
|
|
|
{
|
|
|
|
$profiler = clone $this;
|
|
|
|
$profiler->profilerStorage->setToken($token);
|
|
|
|
$profiler->loadCollectorData();
|
|
|
|
|
|
|
|
return $profiler;
|
|
|
|
}
|
|
|
|
|
2010-06-16 14:45:20 +01:00
|
|
|
/**
|
|
|
|
* Collects data for the given Response.
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @param Response $response A Response instance
|
2010-06-16 14:45:20 +01:00
|
|
|
*/
|
2010-06-16 09:38:41 +01:00
|
|
|
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.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-16 14:45:20 +01:00
|
|
|
/**
|
|
|
|
* Loads the data stored in the storage for all collectors.
|
|
|
|
*/
|
2010-06-16 09:38:41 +01:00
|
|
|
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.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-16 14:45:20 +01:00
|
|
|
/**
|
|
|
|
* Gets the profiler storage.
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return ProfilerStorage A ProfilerStorage instance
|
2010-06-16 14:45:20 +01:00
|
|
|
*/
|
2010-06-16 13:19:46 +01:00
|
|
|
public function getProfilerStorage()
|
|
|
|
{
|
|
|
|
return $this->profilerStorage;
|
|
|
|
}
|
|
|
|
|
2010-06-16 14:45:20 +01:00
|
|
|
/**
|
|
|
|
* Gets the Response.
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Response A Response instance
|
2010-06-16 14:45:20 +01:00
|
|
|
*/
|
2010-06-16 13:19:46 +01:00
|
|
|
public function getResponse()
|
|
|
|
{
|
|
|
|
return $this->response;
|
|
|
|
}
|
|
|
|
|
2010-06-16 14:45:20 +01:00
|
|
|
/**
|
|
|
|
* Gets the Collectors associated with this profiler.
|
|
|
|
*
|
|
|
|
* @return array An array of collectors
|
|
|
|
*/
|
2010-06-16 09:38:41 +01:00
|
|
|
public function getCollectors()
|
|
|
|
{
|
|
|
|
return $this->collectors;
|
|
|
|
}
|
|
|
|
|
2010-06-16 14:45:20 +01:00
|
|
|
/**
|
|
|
|
* Sets the Collectors associated with this profiler.
|
|
|
|
*
|
|
|
|
* @param array $collectors An array of collectors
|
|
|
|
*/
|
2010-06-16 13:19:46 +01:00
|
|
|
public function setCollectors(array $collectors = array())
|
|
|
|
{
|
|
|
|
$this->collectors = array();
|
|
|
|
foreach ($collectors as $name => $collector) {
|
2010-06-16 14:45:20 +01:00
|
|
|
$this->addCollector($collector);
|
2010-06-16 13:19:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-16 14:45:20 +01:00
|
|
|
/**
|
|
|
|
* Adds a Collector.
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @param DataCollectorInterface $collector A DataCollectorInterface instance
|
2010-06-16 14:45:20 +01:00
|
|
|
*/
|
|
|
|
public function addCollector(DataCollectorInterface $collector)
|
2010-06-16 13:19:46 +01:00
|
|
|
{
|
2010-06-16 14:45:20 +01:00
|
|
|
$this->collectors[$collector->getName()] = $collector;
|
2010-06-16 13:19:46 +01:00
|
|
|
}
|
|
|
|
|
2010-06-16 14:45:20 +01:00
|
|
|
/**
|
|
|
|
* Returns true if a Collector for the given name exists.
|
|
|
|
*
|
|
|
|
* @param string $name A collector name
|
|
|
|
*/
|
2010-06-16 09:38:41 +01:00
|
|
|
public function hasCollector($name)
|
|
|
|
{
|
|
|
|
return isset($this->collectors[$name]);
|
|
|
|
}
|
|
|
|
|
2010-06-16 14:45:20 +01:00
|
|
|
/**
|
|
|
|
* Gets a Collector by name.
|
|
|
|
*
|
|
|
|
* @param string $name A collector name
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return DataCollectorInterface A DataCollectorInterface instance
|
2010-06-16 14:45:20 +01:00
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException if the collector does not exist
|
|
|
|
*/
|
2010-06-16 09:38:41 +01:00
|
|
|
public function getCollector($name)
|
|
|
|
{
|
|
|
|
if (!isset($this->collectors[$name])) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('Collector "%s" does not exist.', $name));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->collectors[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-16 14:45:20 +01:00
|
|
|
* Returns true if the named collector exists.
|
2010-06-16 09:38:41 +01:00
|
|
|
*
|
2010-06-16 14:45:20 +01:00
|
|
|
* @param string $name The collector name
|
2010-06-16 09:38:41 +01:00
|
|
|
*
|
2010-07-01 19:17:03 +01:00
|
|
|
* @return Boolean true if the collector exists, false otherwise
|
2010-06-16 09:38:41 +01:00
|
|
|
*/
|
|
|
|
public function offsetExists($name)
|
|
|
|
{
|
|
|
|
return $this->hasCollector($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-16 14:45:20 +01:00
|
|
|
* Gets a collector.
|
2010-06-16 09:38:41 +01:00
|
|
|
*
|
2010-06-16 14:45:20 +01:00
|
|
|
* @param string $name The collector name
|
2010-06-16 09:38:41 +01:00
|
|
|
*
|
2010-06-16 14:45:20 +01:00
|
|
|
* @throws \InvalidArgumentException if the collector does not exist
|
2010-06-16 09:38:41 +01:00
|
|
|
*/
|
|
|
|
public function offsetGet($name)
|
|
|
|
{
|
|
|
|
return $this->getCollector($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-16 14:45:20 +01:00
|
|
|
* Unimplemented.
|
2010-06-16 09:38:41 +01:00
|
|
|
*
|
2010-06-16 14:45:20 +01:00
|
|
|
* @param string $name The collector name
|
|
|
|
* @param string|array $value The collector
|
2010-06-16 09:38:41 +01:00
|
|
|
*
|
2010-06-16 14:45:20 +01:00
|
|
|
* @throws \LogicException
|
2010-06-16 09:38:41 +01:00
|
|
|
*/
|
|
|
|
public function offsetSet($name, $value)
|
|
|
|
{
|
2010-06-16 14:45:20 +01:00
|
|
|
throw new \LogicException('A Collector cannot be set.');
|
2010-06-16 09:38:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unimplemented.
|
|
|
|
*
|
2010-06-16 14:45:20 +01:00
|
|
|
* @param string $name The collector name
|
|
|
|
*
|
|
|
|
* @throws \LogicException
|
2010-06-16 09:38:41 +01:00
|
|
|
*/
|
|
|
|
public function offsetUnset($name)
|
|
|
|
{
|
2010-06-16 14:45:20 +01:00
|
|
|
throw new \LogicException('A Collector cannot be removed.');
|
2010-06-16 09:38:41 +01:00
|
|
|
}
|
|
|
|
}
|