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

288 lines
7.1 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.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@symfony.com>
*/
class Profiler
{
/**
* @var ProfilerStorageInterface
*/
private $storage;
/**
* @var DataCollectorInterface[]
*/
private $collectors = array();
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var Boolean
*/
private $enabled = true;
/**
* 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 14:45:20 +01:00
/**
* Disables the profiler.
2010-06-16 14:45:20 +01:00
*/
public function disable()
{
$this->enabled = false;
}
/**
* Enables the profiler.
*/
public function enable()
{
$this->enabled = true;
}
2010-06-16 14:45:20 +01:00
/**
2011-05-30 17:54:40 +01:00
* Loads the Profile 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
*
2011-05-30 17:54:40 +01:00
* @return Profile A Profile instance
2010-06-16 14:45:20 +01:00
*/
2011-05-30 17:54:40 +01:00
public function loadProfileFromResponse(Response $response)
{
2010-06-16 14:45:20 +01:00
if (!$token = $response->headers->get('X-Debug-Token')) {
2011-05-30 17:54:40 +01:00
return false;
2010-06-16 14:45:20 +01:00
}
2011-05-30 17:54:40 +01:00
return $this->loadProfile($token);
}
2010-06-16 14:45:20 +01:00
/**
2011-05-30 17:54:40 +01:00
* Loads the Profile for the given token.
2010-06-16 14:45:20 +01:00
*
* @param string $token A token
*
2011-05-30 17:54:40 +01:00
* @return Profile A Profile instance
2010-06-16 14:45:20 +01:00
*/
2011-05-30 17:54:40 +01:00
public function loadProfile($token)
{
2011-05-30 17:54:40 +01:00
return $this->storage->read($token);
}
2011-05-30 17:54:40 +01:00
/**
* Saves a Profile.
*
* @param Profile $profile A Profile instance
2011-05-30 17:54:40 +01:00
*
* @return Boolean
*/
public function saveProfile(Profile $profile)
{
if (!($ret = $this->storage->write($profile)) && null !== $this->logger) {
$this->logger->warning('Unable to store the profiler information.');
2011-05-30 17:54:40 +01:00
}
return $ret;
}
/**
* Purges all data from the storage.
*/
public function purge()
{
$this->storage->purge();
}
/**
* Exports the current profiler data.
*
* @param Profile $profile A Profile instance
*
* @return string The exported data
*/
2011-05-30 17:54:40 +01:00
public function export(Profile $profile)
{
2011-05-30 17:54:40 +01:00
return base64_encode(serialize($profile));
}
/**
* Imports data into the profiler storage.
*
* @param string $data A data string as exported by the export() method
*
2011-05-30 17:54:40 +01:00
* @return Profile A Profile instance
*/
public function import($data)
{
2011-05-30 17:54:40 +01:00
$profile = unserialize(base64_decode($data));
2011-05-30 17:54:40 +01:00
if ($this->storage->read($profile->getToken())) {
return false;
}
2011-05-30 17:54:40 +01:00
$this->saveProfile($profile);
2011-05-30 17:54:40 +01:00
return $profile;
}
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
* @param string $method The request method
2012-12-16 08:41:40 +00:00
* @param string $start The start date to search from
* @param string $end The end date to search to
*
* @return array An array of tokens
2012-12-16 08:41:40 +00:00
*
* @see http://fr2.php.net/manual/en/datetime.formats.php for the supported date/time formats
2010-06-16 14:45:20 +01:00
*/
public function find($ip, $url, $limit, $method, $start, $end)
2010-06-16 13:19:46 +01:00
{
2012-12-16 08:41:40 +00:00
if ('' != $start && null !== $start) {
$start = new \DateTime($start);
$start = $start->getTimestamp();
2012-12-16 08:41:40 +00:00
} else {
$start = null;
}
2012-12-16 08:41:40 +00:00
if ('' != $end && null !== $end) {
$end = new \DateTime($end);
$end = $end->getTimestamp();
2012-12-16 08:41:40 +00:00
} else {
$end = null;
}
return $this->storage->find($ip, $url, $limit, $method, $start, $end);
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
2011-05-30 17:54:40 +01:00
*
* @return Profile|null A Profile instance or null if the profiler is disabled
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;
}
2011-05-30 17:54:40 +01:00
$profile = new Profile(uniqid());
$profile->setTime(time());
$profile->setUrl($request->getUri());
$profile->setIp($request->getClientIp());
$profile->setMethod($request->getMethod());
2011-05-30 17:54:40 +01:00
$response->headers->set('X-Debug-Token', $profile->getToken());
foreach ($this->collectors as $collector) {
2011-05-30 17:54:40 +01:00
$collector->collect($request, $response, $exception);
2011-05-30 17:54:40 +01:00
// forces collectors to become "read/only" (they loose their object dependencies)
$profile->addCollector(unserialize(serialize($collector)));
}
2011-05-30 17:54:40 +01:00
return $profile;
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 DataCollectorInterface[] $collectors An array of collectors
2010-06-16 14:45:20 +01:00
*/
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
*
* @return Boolean
2010-06-16 14:45:20 +01:00
*/
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];
}
}