mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-23 18:48:51 +02:00
N°6934 - Symfony 6.4 - upgrade Symfony bundles to 6.4 (#580)
* Update Symfony lib to version ~6.4.0 * Update code missing return type * Add an iTop general configuration entry to store application secret (Symfony mandatory parameter) * Use dependency injection in ExceptionListener & UserProvider classes
This commit is contained in:
@@ -20,10 +20,8 @@ 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.
|
||||
@@ -45,10 +43,11 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @param \Closure|null $filter A filter to apply on the list of tokens
|
||||
*/
|
||||
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/* , \Closure $filter = null */): array
|
||||
{
|
||||
$filter = 7 < \func_num_args() ? func_get_arg(7) : null;
|
||||
$file = $this->getIndexFilename();
|
||||
|
||||
if (!file_exists($file)) {
|
||||
@@ -61,10 +60,21 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
$result = [];
|
||||
while (\count($result) < $limit && $line = $this->readLineFromFile($file)) {
|
||||
$values = str_getcsv($line);
|
||||
[$csvToken, $csvIp, $csvMethod, $csvUrl, $csvTime, $csvParent, $csvStatusCode] = $values;
|
||||
|
||||
if (7 > \count($values)) {
|
||||
// skip invalid lines
|
||||
continue;
|
||||
}
|
||||
|
||||
[$csvToken, $csvIp, $csvMethod, $csvUrl, $csvTime, $csvParent, $csvStatusCode, $csvVirtualType] = $values + [7 => null];
|
||||
$csvTime = (int) $csvTime;
|
||||
|
||||
if ($ip && !str_contains($csvIp, $ip) || $url && !str_contains($csvUrl, $url) || $method && !str_contains($csvMethod, $method) || $statusCode && !str_contains($csvStatusCode, $statusCode)) {
|
||||
$urlFilter = false;
|
||||
if ($url) {
|
||||
$urlFilter = str_starts_with($url, '!') ? str_contains($csvUrl, substr($url, 1)) : !str_contains($csvUrl, $url);
|
||||
}
|
||||
|
||||
if ($ip && !str_contains($csvIp, $ip) || $urlFilter || $method && !str_contains($csvMethod, $method) || $statusCode && !str_contains($csvStatusCode, $statusCode)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -76,7 +86,7 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
continue;
|
||||
}
|
||||
|
||||
$result[$csvToken] = [
|
||||
$profile = [
|
||||
'token' => $csvToken,
|
||||
'ip' => $csvIp,
|
||||
'method' => $csvMethod,
|
||||
@@ -84,7 +94,14 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
'time' => $csvTime,
|
||||
'parent' => $csvParent,
|
||||
'status_code' => $csvStatusCode,
|
||||
'virtual_type' => $csvVirtualType ?: 'request',
|
||||
];
|
||||
|
||||
if ($filter && !$filter($profile)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$result[$csvToken] = $profile;
|
||||
}
|
||||
|
||||
fclose($file);
|
||||
@@ -93,7 +110,7 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @return void
|
||||
*/
|
||||
public function purge()
|
||||
{
|
||||
@@ -110,17 +127,12 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function read(string $token): ?Profile
|
||||
{
|
||||
return $this->doRead($token);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function write(Profile $profile): bool
|
||||
@@ -140,9 +152,7 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
// 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()));
|
||||
$childrenToken = array_filter(array_map(fn (Profile $p) => $profileToken !== $p->getToken() ? $p->getToken() : null, $profile->getChildren()));
|
||||
|
||||
// Store profile
|
||||
$data = [
|
||||
@@ -155,6 +165,7 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
'url' => $profile->getUrl(),
|
||||
'time' => $profile->getTime(),
|
||||
'status_code' => $profile->getStatusCode(),
|
||||
'virtual_type' => $profile->getVirtualType() ?? 'request',
|
||||
];
|
||||
|
||||
$data = serialize($data);
|
||||
@@ -178,11 +189,16 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
$profile->getIp(),
|
||||
$profile->getMethod(),
|
||||
$profile->getUrl(),
|
||||
$profile->getTime(),
|
||||
$profile->getTime() ?: time(),
|
||||
$profile->getParentToken(),
|
||||
$profile->getStatusCode(),
|
||||
$profile->getVirtualType() ?? 'request',
|
||||
]);
|
||||
fclose($file);
|
||||
|
||||
if (1 === mt_rand(1, 10)) {
|
||||
$this->removeExpiredProfiles();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -190,10 +206,8 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
|
||||
/**
|
||||
* Gets filename to store data, associated to the token.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getFilename(string $token)
|
||||
protected function getFilename(string $token): string
|
||||
{
|
||||
// Uses 4 last characters, because first are mostly the same.
|
||||
$folderA = substr($token, -2, 2);
|
||||
@@ -204,10 +218,8 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
|
||||
/**
|
||||
* Gets the index filename.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getIndexFilename()
|
||||
protected function getIndexFilename(): string
|
||||
{
|
||||
return $this->folder.'/index.csv';
|
||||
}
|
||||
@@ -218,10 +230,8 @@ 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
|
||||
*/
|
||||
protected function readLineFromFile($file)
|
||||
protected function readLineFromFile($file): mixed
|
||||
{
|
||||
$line = '';
|
||||
$position = ftell($file);
|
||||
@@ -259,6 +269,9 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
return '' === $line ? null : $line;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Profile
|
||||
*/
|
||||
protected function createProfileFromData(string $token, array $data, Profile $parent = null)
|
||||
{
|
||||
$profile = new Profile($token);
|
||||
@@ -267,6 +280,7 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
$profile->setUrl($data['url']);
|
||||
$profile->setTime($data['time']);
|
||||
$profile->setStatusCode($data['status_code']);
|
||||
$profile->setVirtualType($data['virtual_type'] ?: 'request');
|
||||
$profile->setCollectors($data['data']);
|
||||
|
||||
if (!$parent && $data['parent']) {
|
||||
@@ -308,4 +322,37 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
|
||||
return $this->createProfileFromData($token, $data, $profile);
|
||||
}
|
||||
|
||||
private function removeExpiredProfiles(): void
|
||||
{
|
||||
$minimalProfileTimestamp = time() - 2 * 86400;
|
||||
$file = $this->getIndexFilename();
|
||||
$handle = fopen($file, 'r');
|
||||
|
||||
if ($offset = is_file($file.'.offset') ? (int) file_get_contents($file.'.offset') : 0) {
|
||||
fseek($handle, $offset);
|
||||
}
|
||||
|
||||
while ($line = fgets($handle)) {
|
||||
$values = str_getcsv($line);
|
||||
|
||||
if (7 > \count($values)) {
|
||||
// skip invalid lines
|
||||
$offset += \strlen($line);
|
||||
continue;
|
||||
}
|
||||
|
||||
[$csvToken, , , , $csvTime] = $values;
|
||||
|
||||
if ($csvTime >= $minimalProfileTimestamp) {
|
||||
break;
|
||||
}
|
||||
|
||||
@unlink($this->getFilename($csvToken));
|
||||
$offset += \strlen($line);
|
||||
}
|
||||
fclose($handle);
|
||||
|
||||
file_put_contents($file.'.offset', $offset);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user