Aggiornato Composer
This commit is contained in:
@@ -47,7 +47,7 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function find(?string $ip, ?string $url, ?int $limit, ?string $method, int $start = null, int $end = null, string $statusCode = null): array
|
||||
public function find(?string $ip, ?string $url, ?int $limit, ?string $method, ?int $start = null, ?int $end = null, ?string $statusCode = null): array
|
||||
{
|
||||
$file = $this->getIndexFilename();
|
||||
|
||||
@@ -61,6 +61,12 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
$result = [];
|
||||
while (\count($result) < $limit && $line = $this->readLineFromFile($file)) {
|
||||
$values = str_getcsv($line);
|
||||
|
||||
if (7 !== \count($values)) {
|
||||
// skip invalid lines
|
||||
continue;
|
||||
}
|
||||
|
||||
[$csvToken, $csvIp, $csvMethod, $csvUrl, $csvTime, $csvParent, $csvStatusCode] = $values;
|
||||
$csvTime = (int) $csvTime;
|
||||
|
||||
@@ -115,19 +121,7 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
*/
|
||||
public function read(string $token): ?Profile
|
||||
{
|
||||
if (!$token || !file_exists($file = $this->getFilename($token))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (\function_exists('gzcompress')) {
|
||||
$file = 'compress.zlib://'.$file;
|
||||
}
|
||||
|
||||
if (!$data = unserialize(file_get_contents($file))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->createProfileFromData($token, $data);
|
||||
return $this->doRead($token);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -169,14 +163,13 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
'status_code' => $profile->getStatusCode(),
|
||||
];
|
||||
|
||||
$context = stream_context_create();
|
||||
$data = serialize($data);
|
||||
|
||||
if (\function_exists('gzcompress')) {
|
||||
$file = 'compress.zlib://'.$file;
|
||||
stream_context_set_option($context, 'zlib', 'level', 3);
|
||||
if (\function_exists('gzencode')) {
|
||||
$data = gzencode($data, 3);
|
||||
}
|
||||
|
||||
if (false === file_put_contents($file, serialize($data), 0, $context)) {
|
||||
if (false === file_put_contents($file, $data, \LOCK_EX)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -272,7 +265,7 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
return '' === $line ? null : $line;
|
||||
}
|
||||
|
||||
protected function createProfileFromData(string $token, array $data, Profile $parent = null)
|
||||
protected function createProfileFromData(string $token, array $data, ?Profile $parent = null)
|
||||
{
|
||||
$profile = new Profile($token);
|
||||
$profile->setIp($data['ip']);
|
||||
@@ -291,21 +284,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);
|
||||
}
|
||||
|
||||
if (\function_exists('gzcompress')) {
|
||||
$file = 'compress.zlib://'.$file;
|
||||
}
|
||||
|
||||
if (!$childData = unserialize(file_get_contents($file))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$profile->addChild($this->createProfileFromData($token, $childData, $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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ class Profiler implements ResetInterface
|
||||
private $initiallyEnabled = true;
|
||||
private $enabled = true;
|
||||
|
||||
public function __construct(ProfilerStorageInterface $storage, LoggerInterface $logger = null, bool $enable = true)
|
||||
public function __construct(ProfilerStorageInterface $storage, ?LoggerInterface $logger = null, bool $enable = true)
|
||||
{
|
||||
$this->storage = $storage;
|
||||
$this->logger = $logger;
|
||||
@@ -116,7 +116,7 @@ class Profiler implements ResetInterface
|
||||
/**
|
||||
* Finds profiler tokens for the given criteria.
|
||||
*
|
||||
* @param string|null $limit The maximum number of tokens to return
|
||||
* @param int|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
|
||||
*
|
||||
@@ -124,7 +124,7 @@ class Profiler implements ResetInterface
|
||||
*
|
||||
* @see https://php.net/datetime.formats for the supported date/time formats
|
||||
*/
|
||||
public function find(?string $ip, ?string $url, ?string $limit, ?string $method, ?string $start, ?string $end, string $statusCode = null)
|
||||
public function find(?string $ip, ?string $url, ?int $limit, ?string $method, ?string $start, ?string $end, ?string $statusCode = null)
|
||||
{
|
||||
return $this->storage->find($ip, $url, $limit, $method, $this->getTimestamp($start), $this->getTimestamp($end), $statusCode);
|
||||
}
|
||||
@@ -134,7 +134,7 @@ class Profiler implements ResetInterface
|
||||
*
|
||||
* @return Profile|null
|
||||
*/
|
||||
public function collect(Request $request, Response $response, \Throwable $exception = null)
|
||||
public function collect(Request $request, Response $response, ?\Throwable $exception = null)
|
||||
{
|
||||
if (false === $this->enabled) {
|
||||
return null;
|
||||
|
||||
@@ -33,7 +33,7 @@ interface ProfilerStorageInterface
|
||||
* @param int|null $start The start date to search from
|
||||
* @param int|null $end The end date to search to
|
||||
*/
|
||||
public function find(?string $ip, ?string $url, ?int $limit, ?string $method, int $start = null, int $end = null): array;
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user