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/Component/HttpKernel/Profiler/Profiler.php

330 lines
7.9 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpKernel\Profiler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface;
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
/**
* Profiler.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class Profiler
{
protected $storage;
protected $collectors;
protected $logger;
2010-08-22 21:35:44 +01:00
protected $enabled;
protected $token;
protected $parent;
protected $data;
protected $ip;
protected $url;
protected $time;
protected $empty;
/**
* Constructor.
*
* @param ProfilerStorageInterface $storage A ProfilerStorageInterface instance
* @param LoggerInterface $logger A LoggerInterface instance
*/
public function __construct(ProfilerStorageInterface $storage, LoggerInterface $logger = null)
{
$this->storage = $storage;
$this->logger = $logger;
2010-06-16 13:19:46 +01:00
$this->collectors = array();
2010-08-22 21:35:44 +01:00
$this->enabled = true;
$this->empty = true;
}
2010-06-16 14:45:20 +01:00
/**
* Disables the profiler.
2010-06-16 14:45:20 +01:00
*/
public function disable()
{
$this->enabled = false;
}
2010-06-16 14:45:20 +01:00
/**
* Loads a Profiler for the given Response.
2010-06-16 14:45:20 +01:00
*
* @param Response $response A Response instance
2010-06-16 14:45:20 +01:00
*
* @return Profiler A new Profiler instance
2010-06-16 14:45:20 +01:00
*/
public function loadFromResponse(Response $response)
{
2010-06-16 14:45:20 +01:00
if (!$token = $response->headers->get('X-Debug-Token')) {
return null;
}
return $this->loadFromToken($token);
}
2010-06-16 14:45:20 +01:00
/**
* Loads a Profiler for the given token.
2010-06-16 14:45:20 +01:00
*
* @param string $token A token
*
* @return Profiler A new Profiler instance
2010-06-16 14:45:20 +01:00
*/
public function loadFromToken($token)
{
$profiler = new self($this->storage, $this->logger);
$profiler->setToken($token);
return $profiler;
}
/**
* Purges all data from the storage.
*/
public function purge()
{
$this->storage->purge();
}
/**
* Exports the current profiler data.
*
* @return string The exported data
*/
public function export()
{
$data = base64_encode(serialize(array($this->token, $this->parent, $this->collectors, $this->ip, $this->url, $this->time)));
return $data;
}
/**
* Imports data into the profiler storage.
*
* @param string $data A data string as exported by the export() method
*
* @return string The token associated with the imported data
*/
public function import($data)
{
list($token, $parent, $collectors, $ip, $url, $time) = unserialize(base64_decode($data));
if (false !== $this->storage->read($token)) {
return false;
}
$data = base64_encode(serialize($collectors));
$this->storage->write($token, $parent, $data, $ip, $url, $time);
return $token;
}
/**
* Sets the token.
*
* @param string $token The token
*/
public function setToken($token)
2010-08-22 21:35:44 +01:00
{
$this->token = $token;
if (false !== $items = $this->storage->read($token)) {
list($data, $this->ip, $this->url, $this->time) = $items;
$this->set(unserialize(base64_decode($data)));
$this->empty = false;
} else {
$this->empty = true;
}
2010-08-22 21:35:44 +01:00
}
2010-06-16 14:45:20 +01:00
/**
* Gets the token.
2010-06-16 14:45:20 +01:00
*
* @return string The token
2010-06-16 14:45:20 +01:00
*/
public function getToken()
{
if (null === $this->token) {
$this->token = uniqid();
2010-08-22 21:35:44 +01:00
}
return $this->token;
}
/**
* Checks if the profiler is empty.
*
* @return Boolean Whether the profiler is empty or not
*/
public function isEmpty()
{
return $this->empty;
}
/**
* Returns the parent token.
*
* @return string The parent token
*/
public function getParent()
{
return $this->parent;
}
/**
* Returns the IP.
*
* @return string The IP
*/
public function getIp()
{
return $this->ip;
}
/**
* Returns the URL.
*
* @return string The URL
*/
public function getUrl()
{
return $this->url;
}
2010-06-16 14:45:20 +01:00
/**
* Returns the time.
*
* @return string The time
2010-06-16 14:45:20 +01:00
*/
public function getTime()
{
return $this->time;
}
2010-06-16 14:45:20 +01:00
/**
* Finds profiler tokens for the given criteria.
2010-06-16 14:45:20 +01:00
*
* @param string $ip The IP
* @param string $url The URL
* @param string $limit The maximum number of tokens to return
*
* @return array An array of tokens
2010-06-16 14:45:20 +01:00
*/
public function find($ip, $url, $limit)
2010-06-16 13:19:46 +01:00
{
return $this->storage->find($ip, $url, $limit);
2010-06-16 13:19:46 +01:00
}
2010-06-16 14:45:20 +01:00
/**
* Collects data for the given Response.
2010-06-16 14:45:20 +01:00
*
* @param Request $request A Request instance
* @param Response $response A Response instance
* @param \Exception $exception An exception instance if the request threw one
2010-06-16 14:45:20 +01:00
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
2010-06-16 13:19:46 +01:00
{
if (false === $this->enabled) {
return;
}
$response->headers->set('X-Debug-Token', $this->getToken());
foreach ($this->collectors as $collector) {
$collector->collect($request, $response, $exception);
}
$this->parent = '';
$this->ip = $request->server->get('REMOTE_ADDR');
$this->url = $request->getUri();
$this->time = time();
$data = base64_encode(serialize($this->collectors));
if (true === $this->storage->write($this->token, $this->parent, $data, $this->ip, $this->url, $this->time)) {
2011-01-28 17:31:55 +00:00
$this->empty = false;
} elseif (null !== $this->logger) {
if (null !== $exception) {
$this->logger->err(sprintf('Unable to store the profiler information (%s).', $exception->getMessage()));
} else {
$this->logger->err('Unable to store the profiler information (%s).');
}
}
2010-06-16 13:19:46 +01:00
}
2010-06-16 14:45:20 +01:00
/**
* Gets the Collectors associated with this profiler.
*
* @return array An array of collectors
*/
2010-09-01 15:53:28 +01:00
public function all()
{
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-09-01 15:53:28 +01:00
public function set(array $collectors = array())
2010-06-16 13:19:46 +01:00
{
$this->collectors = array();
foreach ($collectors as $collector) {
2010-09-01 15:53:28 +01:00
$this->add($collector);
2010-06-16 13:19:46 +01:00
}
}
2010-06-16 14:45:20 +01:00
/**
* Adds a Collector.
*
* @param DataCollectorInterface $collector A DataCollectorInterface instance
2010-06-16 14:45:20 +01:00
*/
2010-09-01 15:53:28 +01:00
public function add(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-09-01 15:53:28 +01:00
public function has($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 DataCollectorInterface A DataCollectorInterface instance
2010-06-16 14:45:20 +01:00
*
* @throws \InvalidArgumentException if the collector does not exist
*/
2010-09-01 15:53:28 +01:00
public function get($name)
{
if (!isset($this->collectors[$name])) {
throw new \InvalidArgumentException(sprintf('Collector "%s" does not exist.', $name));
}
return $this->collectors[$name];
}
}