updated-packages
This commit is contained in:
@@ -34,7 +34,7 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
*/
|
||||
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);
|
||||
@@ -47,24 +47,24 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function find($ip, $url, $limit, $method, $start = null, $end = null, $statusCode = null)
|
||||
public function find($ip, $url, $limit, $method, $start = null, $end = null, $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();
|
||||
$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;
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
continue;
|
||||
}
|
||||
|
||||
$result[$csvToken] = array(
|
||||
$result[$csvToken] = [
|
||||
'token' => $csvToken,
|
||||
'ip' => $csvIp,
|
||||
'method' => $csvMethod,
|
||||
@@ -84,7 +84,7 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
'time' => $csvTime,
|
||||
'parent' => $csvParent,
|
||||
'status_code' => $csvStatusCode,
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
fclose($file);
|
||||
@@ -113,13 +113,9 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function read($token)
|
||||
public function read($token): ?Profile
|
||||
{
|
||||
if (!$token || !file_exists($file = $this->getFilename($token))) {
|
||||
return;
|
||||
}
|
||||
|
||||
return $this->createProfileFromData($token, unserialize(file_get_contents($file)));
|
||||
return $this->doRead($token);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -127,7 +123,7 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function write(Profile $profile)
|
||||
public function write(Profile $profile): bool
|
||||
{
|
||||
$file = $this->getFilename($profile->getToken());
|
||||
|
||||
@@ -149,7 +145,7 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
}, $profile->getChildren()));
|
||||
|
||||
// Store profile
|
||||
$data = array(
|
||||
$data = [
|
||||
'token' => $profileToken,
|
||||
'parent' => $parentToken,
|
||||
'children' => $childrenToken,
|
||||
@@ -159,9 +155,15 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
'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;
|
||||
}
|
||||
|
||||
@@ -171,7 +173,7 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
return false;
|
||||
}
|
||||
|
||||
fputcsv($file, array(
|
||||
fputcsv($file, [
|
||||
$profile->getToken(),
|
||||
$profile->getIp(),
|
||||
$profile->getMethod(),
|
||||
@@ -179,7 +181,7 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
$profile->getTime(),
|
||||
$profile->getParentToken(),
|
||||
$profile->getStatusCode(),
|
||||
));
|
||||
]);
|
||||
fclose($file);
|
||||
}
|
||||
|
||||
@@ -227,7 +229,7 @@ class FileProfilerStorage implements ProfilerStorageInterface
|
||||
$position = ftell($file);
|
||||
|
||||
if (0 === $position) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
while (true) {
|
||||
@@ -249,7 +251,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;
|
||||
@@ -278,13 +280,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