This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Components/HttpKernel/Profiler/Profiler.php

253 lines
6.5 KiB
PHP
Raw Normal View History

<?php
2010-06-16 13:19:46 +01:00
namespace Symfony\Components\HttpKernel\Profiler;
use Symfony\Components\HttpKernel\Response;
2010-06-16 13:19:46 +01:00
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
2010-06-16 13:19:46 +01:00
* @subpackage Components_HttpKernel
* @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)
{
$this->profilerStorage = $profilerStorage;
$this->logger = $logger;
2010-06-16 13:19:46 +01:00
$this->collectors = array();
}
2010-06-16 14:45:20 +01:00
/**
* Clones the Profiler instance.
*/
public function __clone()
{
$this->profilerStorage = clone $this->profilerStorage;
}
2010-06-16 14:45:20 +01:00
/**
* 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)
{
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 14:45:20 +01:00
/**
* 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;
}
2010-06-16 14:45:20 +01:00
/**
* 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.');
}
}
}
2010-06-16 14:45:20 +01:00
/**
* 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.');
}
}
}
2010-06-16 14:45:20 +01:00
/**
* Gets the profiler storage.
*
* @return Symfony\Components\HttpKernel\Profiler\ProfilerStorage A ProfilerStorage instance
*/
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.
*
* @return Symfony\Components\HttpKernel\Response A Response instance
*/
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
*/
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.
*
* @param Symfony\Components\HttpKernel\Profiler\DataCollector\DataCollectorInterface $collector A DataCollectorInterface instance
*/
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
*/
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
*
* @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];
}
/**
2010-06-16 14:45:20 +01:00
* Returns true if the named collector exists.
*
2010-06-16 14:45:20 +01:00
* @param string $name The collector name
*
2010-06-16 14:45:20 +01:00
* @param Boolean true if the collector exists, false otherwise
*/
public function offsetExists($name)
{
return $this->hasCollector($name);
}
/**
2010-06-16 14:45:20 +01:00
* Gets a collector.
*
2010-06-16 14:45:20 +01:00
* @param string $name The collector name
*
2010-06-16 14:45:20 +01:00
* @throws \InvalidArgumentException if the collector does not exist
*/
public function offsetGet($name)
{
return $this->getCollector($name);
}
/**
2010-06-16 14:45:20 +01:00
* Unimplemented.
*
2010-06-16 14:45:20 +01:00
* @param string $name The collector name
* @param string|array $value The collector
*
2010-06-16 14:45:20 +01:00
* @throws \LogicException
*/
public function offsetSet($name, $value)
{
2010-06-16 14:45:20 +01:00
throw new \LogicException('A Collector cannot be set.');
}
/**
* Unimplemented.
*
2010-06-16 14:45:20 +01:00
* @param string $name The collector name
*
* @throws \LogicException
*/
public function offsetUnset($name)
{
2010-06-16 14:45:20 +01:00
throw new \LogicException('A Collector cannot be removed.');
}
}