Upgrade framework
This commit is contained in:
@@ -20,23 +20,19 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
{
|
||||
/**
|
||||
* Folder where profiler data are stored.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $folder;
|
||||
private string $folder;
|
||||
|
||||
/**
|
||||
* Constructs the file storage using a "dsn-like" path.
|
||||
*
|
||||
* Example : "file:/path/to/the/storage/folder"
|
||||
*
|
||||
* @param string $dsn The DSN
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function __construct($dsn)
|
||||
public function __construct(string $dsn)
|
||||
{
|
||||
if (0 !== strpos($dsn, 'file:')) {
|
||||
if (!str_starts_with($dsn, 'file:')) {
|
||||
throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use FileStorage with an invalid dsn "%s". The expected format is "file:/path/to/the/storage/folder".', $dsn));
|
||||
}
|
||||
$this->folder = substr($dsn, 5);
|
||||
@@ -49,24 +45,24 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function find($ip, $url, $limit, $method, $start = null, $end = null, $statusCode = null)
|
||||
public function find(?string $ip, ?string $url, ?int $limit, ?string $method, int $start = null, int $end = null, string $statusCode = null): array
|
||||
{
|
||||
$file = $this->getIndexFilename();
|
||||
|
||||
if (!file_exists($file)) {
|
||||
return array();
|
||||
return [];
|
||||
}
|
||||
|
||||
$file = fopen($file, 'r');
|
||||
fseek($file, 0, SEEK_END);
|
||||
fseek($file, 0, \SEEK_END);
|
||||
|
||||
$result = array();
|
||||
while (count($result) < $limit && $line = $this->readLineFromFile($file)) {
|
||||
$result = [];
|
||||
while (\count($result) < $limit && $line = $this->readLineFromFile($file)) {
|
||||
$values = str_getcsv($line);
|
||||
list($csvToken, $csvIp, $csvMethod, $csvUrl, $csvTime, $csvParent, $csvStatusCode) = $values;
|
||||
[$csvToken, $csvIp, $csvMethod, $csvUrl, $csvTime, $csvParent, $csvStatusCode] = $values;
|
||||
$csvTime = (int) $csvTime;
|
||||
|
||||
if ($ip && false === strpos($csvIp, $ip) || $url && false === strpos($csvUrl, $url) || $method && false === strpos($csvMethod, $method) || $statusCode && false === strpos($csvStatusCode, $statusCode)) {
|
||||
if ($ip && !str_contains($csvIp, $ip) || $url && !str_contains($csvUrl, $url) || $method && !str_contains($csvMethod, $method) || $statusCode && !str_contains($csvStatusCode, $statusCode)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -78,7 +74,7 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
continue;
|
||||
}
|
||||
|
||||
$result[$csvToken] = array(
|
||||
$result[$csvToken] = [
|
||||
'token' => $csvToken,
|
||||
'ip' => $csvIp,
|
||||
'method' => $csvMethod,
|
||||
@@ -86,7 +82,7 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
'time' => $csvTime,
|
||||
'parent' => $csvParent,
|
||||
'status_code' => $csvStatusCode,
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
fclose($file);
|
||||
@@ -115,13 +111,9 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function read($token)
|
||||
public function read(string $token): ?Profile
|
||||
{
|
||||
if (!$token || !file_exists($file = $this->getFilename($token))) {
|
||||
return;
|
||||
}
|
||||
|
||||
return $this->createProfileFromData($token, unserialize(file_get_contents($file)));
|
||||
return $this->doRead($token);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,33 +121,47 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function write(Profile $profile)
|
||||
public function write(Profile $profile): bool
|
||||
{
|
||||
$file = $this->getFilename($profile->getToken());
|
||||
|
||||
$profileIndexed = is_file($file);
|
||||
if (!$profileIndexed) {
|
||||
// Create directory
|
||||
$dir = dirname($file);
|
||||
$dir = \dirname($file);
|
||||
if (!is_dir($dir) && false === @mkdir($dir, 0777, true) && !is_dir($dir)) {
|
||||
throw new \RuntimeException(sprintf('Unable to create the storage directory (%s).', $dir));
|
||||
}
|
||||
}
|
||||
|
||||
$profileToken = $profile->getToken();
|
||||
// when there are errors in sub-requests, the parent and/or children tokens
|
||||
// may equal the profile token, resulting in infinite loops
|
||||
$parentToken = $profile->getParentToken() !== $profileToken ? $profile->getParentToken() : null;
|
||||
$childrenToken = array_filter(array_map(function (Profile $p) use ($profileToken) {
|
||||
return $profileToken !== $p->getToken() ? $p->getToken() : null;
|
||||
}, $profile->getChildren()));
|
||||
|
||||
// Store profile
|
||||
$data = array(
|
||||
'token' => $profile->getToken(),
|
||||
'parent' => $profile->getParentToken(),
|
||||
'children' => array_map(function ($p) { return $p->getToken(); }, $profile->getChildren()),
|
||||
$data = [
|
||||
'token' => $profileToken,
|
||||
'parent' => $parentToken,
|
||||
'children' => $childrenToken,
|
||||
'data' => $profile->getCollectors(),
|
||||
'ip' => $profile->getIp(),
|
||||
'method' => $profile->getMethod(),
|
||||
'url' => $profile->getUrl(),
|
||||
'time' => $profile->getTime(),
|
||||
'status_code' => $profile->getStatusCode(),
|
||||
);
|
||||
];
|
||||
|
||||
if (false === file_put_contents($file, serialize($data))) {
|
||||
$data = serialize($data);
|
||||
|
||||
if (\function_exists('gzencode')) {
|
||||
$data = gzencode($data, 3);
|
||||
}
|
||||
|
||||
if (false === file_put_contents($file, $data, \LOCK_EX)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -165,7 +171,7 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
return false;
|
||||
}
|
||||
|
||||
fputcsv($file, array(
|
||||
fputcsv($file, [
|
||||
$profile->getToken(),
|
||||
$profile->getIp(),
|
||||
$profile->getMethod(),
|
||||
@@ -173,7 +179,7 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
$profile->getTime(),
|
||||
$profile->getParentToken(),
|
||||
$profile->getStatusCode(),
|
||||
));
|
||||
]);
|
||||
fclose($file);
|
||||
}
|
||||
|
||||
@@ -182,12 +188,8 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
|
||||
/**
|
||||
* Gets filename to store data, associated to the token.
|
||||
*
|
||||
* @param string $token
|
||||
*
|
||||
* @return string The profile filename
|
||||
*/
|
||||
protected function getFilename($token)
|
||||
protected function getFilename(string $token): string
|
||||
{
|
||||
// Uses 4 last characters, because first are mostly the same.
|
||||
$folderA = substr($token, -2, 2);
|
||||
@@ -198,10 +200,8 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
|
||||
/**
|
||||
* Gets the index filename.
|
||||
*
|
||||
* @return string The index filename
|
||||
*/
|
||||
protected function getIndexFilename()
|
||||
protected function getIndexFilename(): string
|
||||
{
|
||||
return $this->folder.'/index.csv';
|
||||
}
|
||||
@@ -212,16 +212,14 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
* This function automatically skips the empty lines and do not include the line return in result value.
|
||||
*
|
||||
* @param resource $file The file resource, with the pointer placed at the end of the line to read
|
||||
*
|
||||
* @return mixed A string representing the line or null if beginning of file is reached
|
||||
*/
|
||||
protected function readLineFromFile($file)
|
||||
protected function readLineFromFile($file): mixed
|
||||
{
|
||||
$line = '';
|
||||
$position = ftell($file);
|
||||
|
||||
if (0 === $position) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
while (true) {
|
||||
@@ -243,7 +241,7 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
|
||||
$position += $upTo;
|
||||
$line = substr($buffer, $upTo + 1).$line;
|
||||
fseek($file, max(0, $position), SEEK_SET);
|
||||
fseek($file, max(0, $position), \SEEK_SET);
|
||||
|
||||
if ('' !== $line) {
|
||||
break;
|
||||
@@ -253,7 +251,7 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
return '' === $line ? null : $line;
|
||||
}
|
||||
|
||||
protected function createProfileFromData($token, $data, $parent = null)
|
||||
protected function createProfileFromData(string $token, array $data, Profile $parent = null)
|
||||
{
|
||||
$profile = new Profile($token);
|
||||
$profile->setIp($data['ip']);
|
||||
@@ -272,13 +270,34 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
}
|
||||
|
||||
foreach ($data['children'] as $token) {
|
||||
if (!$token || !file_exists($file = $this->getFilename($token))) {
|
||||
continue;
|
||||
if (null !== $childProfile = $this->doRead($token, $profile)) {
|
||||
$profile->addChild($childProfile);
|
||||
}
|
||||
|
||||
$profile->addChild($this->createProfileFromData($token, unserialize(file_get_contents($file)), $profile));
|
||||
}
|
||||
|
||||
return $profile;
|
||||
}
|
||||
|
||||
private function doRead($token, Profile $profile = null): ?Profile
|
||||
{
|
||||
if (!$token || !file_exists($file = $this->getFilename($token))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$h = fopen($file, 'r');
|
||||
flock($h, \LOCK_SH);
|
||||
$data = stream_get_contents($h);
|
||||
flock($h, \LOCK_UN);
|
||||
fclose($h);
|
||||
|
||||
if (\function_exists('gzdecode')) {
|
||||
$data = @gzdecode($data) ?: $data;
|
||||
}
|
||||
|
||||
if (!$data = unserialize($data)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->createProfileFromData($token, $data, $profile);
|
||||
}
|
||||
}
|
||||
|
||||
147
vendor/symfony/http-kernel/Profiler/Profile.php
vendored
147
vendor/symfony/http-kernel/Profiler/Profile.php
vendored
@@ -20,173 +20,122 @@ use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
|
||||
*/
|
||||
class Profile
|
||||
{
|
||||
private $token;
|
||||
private string $token;
|
||||
|
||||
/**
|
||||
* @var DataCollectorInterface[]
|
||||
*/
|
||||
private $collectors = array();
|
||||
private array $collectors = [];
|
||||
|
||||
private $ip;
|
||||
private $method;
|
||||
private $url;
|
||||
private $time;
|
||||
private $statusCode;
|
||||
|
||||
/**
|
||||
* @var Profile
|
||||
*/
|
||||
private $parent;
|
||||
private ?string $ip = null;
|
||||
private ?string $method = null;
|
||||
private ?string $url = null;
|
||||
private ?int $time = null;
|
||||
private ?int $statusCode = null;
|
||||
private ?self $parent = null;
|
||||
|
||||
/**
|
||||
* @var Profile[]
|
||||
*/
|
||||
private $children = array();
|
||||
private array $children = [];
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $token The token
|
||||
*/
|
||||
public function __construct($token)
|
||||
public function __construct(string $token)
|
||||
{
|
||||
$this->token = $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the token.
|
||||
*
|
||||
* @param string $token The token
|
||||
*/
|
||||
public function setToken($token)
|
||||
public function setToken(string $token)
|
||||
{
|
||||
$this->token = $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the token.
|
||||
*
|
||||
* @return string The token
|
||||
*/
|
||||
public function getToken()
|
||||
public function getToken(): string
|
||||
{
|
||||
return $this->token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the parent token.
|
||||
*
|
||||
* @param Profile $parent
|
||||
*/
|
||||
public function setParent(Profile $parent)
|
||||
public function setParent(self $parent)
|
||||
{
|
||||
$this->parent = $parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the parent profile.
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function getParent()
|
||||
public function getParent(): ?self
|
||||
{
|
||||
return $this->parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the parent token.
|
||||
*
|
||||
* @return null|string The parent token
|
||||
*/
|
||||
public function getParentToken()
|
||||
public function getParentToken(): ?string
|
||||
{
|
||||
return $this->parent ? $this->parent->getToken() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the IP.
|
||||
*
|
||||
* @return string The IP
|
||||
*/
|
||||
public function getIp()
|
||||
public function getIp(): ?string
|
||||
{
|
||||
return $this->ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the IP.
|
||||
*
|
||||
* @param string $ip
|
||||
*/
|
||||
public function setIp($ip)
|
||||
public function setIp(?string $ip)
|
||||
{
|
||||
$this->ip = $ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the request method.
|
||||
*
|
||||
* @return string The request method
|
||||
*/
|
||||
public function getMethod()
|
||||
public function getMethod(): ?string
|
||||
{
|
||||
return $this->method;
|
||||
}
|
||||
|
||||
public function setMethod($method)
|
||||
public function setMethod(string $method)
|
||||
{
|
||||
$this->method = $method;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the URL.
|
||||
*
|
||||
* @return string The URL
|
||||
*/
|
||||
public function getUrl()
|
||||
public function getUrl(): ?string
|
||||
{
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
public function setUrl($url)
|
||||
public function setUrl(?string $url)
|
||||
{
|
||||
$this->url = $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the time.
|
||||
*
|
||||
* @return int The time
|
||||
*/
|
||||
public function getTime()
|
||||
public function getTime(): int
|
||||
{
|
||||
if (null === $this->time) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $this->time;
|
||||
return $this->time ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int The time
|
||||
*/
|
||||
public function setTime($time)
|
||||
public function setTime(int $time)
|
||||
{
|
||||
$this->time = $time;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $statusCode
|
||||
*/
|
||||
public function setStatusCode($statusCode)
|
||||
public function setStatusCode(int $statusCode)
|
||||
{
|
||||
$this->statusCode = $statusCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getStatusCode()
|
||||
public function getStatusCode(): ?int
|
||||
{
|
||||
return $this->statusCode;
|
||||
}
|
||||
@@ -196,7 +145,7 @@ class Profile
|
||||
*
|
||||
* @return self[]
|
||||
*/
|
||||
public function getChildren()
|
||||
public function getChildren(): array
|
||||
{
|
||||
return $this->children;
|
||||
}
|
||||
@@ -208,7 +157,7 @@ class Profile
|
||||
*/
|
||||
public function setChildren(array $children)
|
||||
{
|
||||
$this->children = array();
|
||||
$this->children = [];
|
||||
foreach ($children as $child) {
|
||||
$this->addChild($child);
|
||||
}
|
||||
@@ -216,25 +165,30 @@ class Profile
|
||||
|
||||
/**
|
||||
* Adds the child token.
|
||||
*
|
||||
* @param Profile $child
|
||||
*/
|
||||
public function addChild(Profile $child)
|
||||
public function addChild(self $child)
|
||||
{
|
||||
$this->children[] = $child;
|
||||
$child->setParent($this);
|
||||
}
|
||||
|
||||
public function getChildByToken(string $token): ?self
|
||||
{
|
||||
foreach ($this->children as $child) {
|
||||
if ($token === $child->getToken()) {
|
||||
return $child;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a Collector by name.
|
||||
*
|
||||
* @param string $name A collector name
|
||||
*
|
||||
* @return DataCollectorInterface A DataCollectorInterface instance
|
||||
*
|
||||
* @throws \InvalidArgumentException if the collector does not exist
|
||||
*/
|
||||
public function getCollector($name)
|
||||
public function getCollector(string $name): DataCollectorInterface
|
||||
{
|
||||
if (!isset($this->collectors[$name])) {
|
||||
throw new \InvalidArgumentException(sprintf('Collector "%s" does not exist.', $name));
|
||||
@@ -248,7 +202,7 @@ class Profile
|
||||
*
|
||||
* @return DataCollectorInterface[]
|
||||
*/
|
||||
public function getCollectors()
|
||||
public function getCollectors(): array
|
||||
{
|
||||
return $this->collectors;
|
||||
}
|
||||
@@ -260,7 +214,7 @@ class Profile
|
||||
*/
|
||||
public function setCollectors(array $collectors)
|
||||
{
|
||||
$this->collectors = array();
|
||||
$this->collectors = [];
|
||||
foreach ($collectors as $collector) {
|
||||
$this->addCollector($collector);
|
||||
}
|
||||
@@ -268,28 +222,19 @@ class Profile
|
||||
|
||||
/**
|
||||
* Adds a Collector.
|
||||
*
|
||||
* @param 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
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasCollector($name)
|
||||
public function hasCollector(string $name): bool
|
||||
{
|
||||
return isset($this->collectors[$name]);
|
||||
}
|
||||
|
||||
public function __sleep()
|
||||
public function __sleep(): array
|
||||
{
|
||||
return array('token', 'parent', 'children', 'collectors', 'ip', 'method', 'url', 'time', 'statusCode');
|
||||
return ['token', 'parent', 'children', 'collectors', 'ip', 'method', 'url', 'time', 'statusCode'];
|
||||
}
|
||||
}
|
||||
|
||||
115
vendor/symfony/http-kernel/Profiler/Profiler.php
vendored
115
vendor/symfony/http-kernel/Profiler/Profiler.php
vendored
@@ -11,50 +11,37 @@
|
||||
|
||||
namespace Symfony\Component\HttpKernel\Profiler;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
|
||||
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Contracts\Service\ResetInterface;
|
||||
|
||||
/**
|
||||
* Profiler.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class Profiler
|
||||
class Profiler implements ResetInterface
|
||||
{
|
||||
/**
|
||||
* @var ProfilerStorageInterface
|
||||
*/
|
||||
private $storage;
|
||||
|
||||
/**
|
||||
* @var DataCollectorInterface[]
|
||||
*/
|
||||
private $collectors = array();
|
||||
private array $collectors = [];
|
||||
|
||||
/**
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
private $logger;
|
||||
private bool $initiallyEnabled = true;
|
||||
private bool $enabled = true;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $enabled = true;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param ProfilerStorageInterface $storage A ProfilerStorageInterface instance
|
||||
* @param LoggerInterface $logger A LoggerInterface instance
|
||||
*/
|
||||
public function __construct(ProfilerStorageInterface $storage, LoggerInterface $logger = null)
|
||||
public function __construct(ProfilerStorageInterface $storage, LoggerInterface $logger = null, bool $enable = true)
|
||||
{
|
||||
$this->storage = $storage;
|
||||
$this->logger = $logger;
|
||||
$this->initiallyEnabled = $this->enabled = $enable;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,15 +62,11 @@ class Profiler
|
||||
|
||||
/**
|
||||
* Loads the Profile for the given Response.
|
||||
*
|
||||
* @param Response $response A Response instance
|
||||
*
|
||||
* @return Profile|false A Profile instance
|
||||
*/
|
||||
public function loadProfileFromResponse(Response $response)
|
||||
public function loadProfileFromResponse(Response $response): ?Profile
|
||||
{
|
||||
if (!$token = $response->headers->get('X-Debug-Token')) {
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->loadProfile($token);
|
||||
@@ -91,24 +74,16 @@ class Profiler
|
||||
|
||||
/**
|
||||
* Loads the Profile for the given token.
|
||||
*
|
||||
* @param string $token A token
|
||||
*
|
||||
* @return Profile A Profile instance
|
||||
*/
|
||||
public function loadProfile($token)
|
||||
public function loadProfile(string $token): ?Profile
|
||||
{
|
||||
return $this->storage->read($token);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a Profile.
|
||||
*
|
||||
* @param Profile $profile A Profile instance
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function saveProfile(Profile $profile)
|
||||
public function saveProfile(Profile $profile): bool
|
||||
{
|
||||
// late collect
|
||||
foreach ($profile->getCollectors() as $collector) {
|
||||
@@ -118,7 +93,7 @@ class Profiler
|
||||
}
|
||||
|
||||
if (!($ret = $this->storage->write($profile)) && null !== $this->logger) {
|
||||
$this->logger->warning('Unable to store the profiler information.', array('configured_storage' => get_class($this->storage)));
|
||||
$this->logger->warning('Unable to store the profiler information.', ['configured_storage' => \get_class($this->storage)]);
|
||||
}
|
||||
|
||||
return $ret;
|
||||
@@ -135,36 +110,24 @@ class Profiler
|
||||
/**
|
||||
* Finds profiler tokens for the given criteria.
|
||||
*
|
||||
* @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
|
||||
* @param string $start The start date to search from
|
||||
* @param string $end The end date to search to
|
||||
* @param string $statusCode The request status code
|
||||
* @param string|null $limit The maximum number of tokens to return
|
||||
* @param string|null $start The start date to search from
|
||||
* @param string|null $end The end date to search to
|
||||
*
|
||||
* @return array An array of tokens
|
||||
*
|
||||
* @see http://php.net/manual/en/datetime.formats.php for the supported date/time formats
|
||||
* @see https://php.net/datetime.formats for the supported date/time formats
|
||||
*/
|
||||
public function find($ip, $url, $limit, $method, $start, $end, $statusCode = null)
|
||||
public function find(?string $ip, ?string $url, ?string $limit, ?string $method, ?string $start, ?string $end, string $statusCode = null): array
|
||||
{
|
||||
return $this->storage->find($ip, $url, $limit, $method, $this->getTimestamp($start), $this->getTimestamp($end), $statusCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects data for the given Response.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
* @param Response $response A Response instance
|
||||
* @param \Exception $exception An exception instance if the request threw one
|
||||
*
|
||||
* @return Profile|null A Profile instance or null if the profiler is disabled
|
||||
*/
|
||||
public function collect(Request $request, Response $response, \Exception $exception = null)
|
||||
public function collect(Request $request, Response $response, \Throwable $exception = null): ?Profile
|
||||
{
|
||||
if (false === $this->enabled) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
$profile = new Profile(substr(hash('sha256', uniqid(mt_rand(), true)), 0, 6));
|
||||
@@ -178,6 +141,10 @@ class Profiler
|
||||
$profile->setIp('Unknown');
|
||||
}
|
||||
|
||||
if ($prevToken = $response->headers->get('X-Debug-Token')) {
|
||||
$response->headers->set('X-Previous-Debug-Token', $prevToken);
|
||||
}
|
||||
|
||||
$response->headers->set('X-Debug-Token', $profile->getToken());
|
||||
|
||||
foreach ($this->collectors as $collector) {
|
||||
@@ -190,12 +157,18 @@ class Profiler
|
||||
return $profile;
|
||||
}
|
||||
|
||||
public function reset()
|
||||
{
|
||||
foreach ($this->collectors as $collector) {
|
||||
$collector->reset();
|
||||
}
|
||||
$this->enabled = $this->initiallyEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Collectors associated with this profiler.
|
||||
*
|
||||
* @return array An array of collectors
|
||||
*/
|
||||
public function all()
|
||||
public function all(): array
|
||||
{
|
||||
return $this->collectors;
|
||||
}
|
||||
@@ -205,9 +178,9 @@ class Profiler
|
||||
*
|
||||
* @param DataCollectorInterface[] $collectors An array of collectors
|
||||
*/
|
||||
public function set(array $collectors = array())
|
||||
public function set(array $collectors = [])
|
||||
{
|
||||
$this->collectors = array();
|
||||
$this->collectors = [];
|
||||
foreach ($collectors as $collector) {
|
||||
$this->add($collector);
|
||||
}
|
||||
@@ -215,8 +188,6 @@ class Profiler
|
||||
|
||||
/**
|
||||
* Adds a Collector.
|
||||
*
|
||||
* @param DataCollectorInterface $collector A DataCollectorInterface instance
|
||||
*/
|
||||
public function add(DataCollectorInterface $collector)
|
||||
{
|
||||
@@ -227,10 +198,8 @@ class Profiler
|
||||
* Returns true if a Collector for the given name exists.
|
||||
*
|
||||
* @param string $name A collector name
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function has($name)
|
||||
public function has(string $name): bool
|
||||
{
|
||||
return isset($this->collectors[$name]);
|
||||
}
|
||||
@@ -240,11 +209,9 @@ class Profiler
|
||||
*
|
||||
* @param string $name A collector name
|
||||
*
|
||||
* @return DataCollectorInterface A DataCollectorInterface instance
|
||||
*
|
||||
* @throws \InvalidArgumentException if the collector does not exist
|
||||
*/
|
||||
public function get($name)
|
||||
public function get(string $name): DataCollectorInterface
|
||||
{
|
||||
if (!isset($this->collectors[$name])) {
|
||||
throw new \InvalidArgumentException(sprintf('Collector "%s" does not exist.', $name));
|
||||
@@ -253,16 +220,16 @@ class Profiler
|
||||
return $this->collectors[$name];
|
||||
}
|
||||
|
||||
private function getTimestamp($value)
|
||||
private function getTimestamp(?string $value): ?int
|
||||
{
|
||||
if (null === $value || '' == $value) {
|
||||
return;
|
||||
if (null === $value || '' === $value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$value = new \DateTime(is_numeric($value) ? '@'.$value : $value);
|
||||
} catch (\Exception $e) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
return $value->getTimestamp();
|
||||
|
||||
@@ -14,6 +14,14 @@ namespace Symfony\Component\HttpKernel\Profiler;
|
||||
/**
|
||||
* ProfilerStorageInterface.
|
||||
*
|
||||
* This interface exists for historical reasons. The only supported
|
||||
* implementation is FileProfilerStorage.
|
||||
*
|
||||
* As the profiler must only be used on non-production servers, the file storage
|
||||
* is more than enough and no other implementations will ever be supported.
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
interface ProfilerStorageInterface
|
||||
@@ -21,36 +29,23 @@ interface ProfilerStorageInterface
|
||||
/**
|
||||
* Finds profiler tokens for the given criteria.
|
||||
*
|
||||
* @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
|
||||
* @param int|null $start The start date to search from
|
||||
* @param int|null $end The end date to search to
|
||||
*
|
||||
* @return array An array of tokens
|
||||
* @param int|null $limit The maximum number of tokens to return
|
||||
* @param int|null $start The start date to search from
|
||||
* @param int|null $end The end date to search to
|
||||
*/
|
||||
public function find($ip, $url, $limit, $method, $start = null, $end = null);
|
||||
public function find(?string $ip, ?string $url, ?int $limit, ?string $method, int $start = null, int $end = null): array;
|
||||
|
||||
/**
|
||||
* Reads data associated with the given token.
|
||||
*
|
||||
* The method returns false if the token does not exist in the storage.
|
||||
*
|
||||
* @param string $token A token
|
||||
*
|
||||
* @return Profile The profile associated with token
|
||||
*/
|
||||
public function read($token);
|
||||
public function read(string $token): ?Profile;
|
||||
|
||||
/**
|
||||
* Saves a Profile.
|
||||
*
|
||||
* @param Profile $profile A Profile instance
|
||||
*
|
||||
* @return bool Write operation successful
|
||||
*/
|
||||
public function write(Profile $profile);
|
||||
public function write(Profile $profile): bool;
|
||||
|
||||
/**
|
||||
* Purges all data from the database.
|
||||
|
||||
Reference in New Issue
Block a user