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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user