package and depencies

This commit is contained in:
RafficMohammed
2023-01-08 02:57:24 +05:30
parent d5332eb421
commit 1d54b8bc7f
4309 changed files with 193331 additions and 172289 deletions

View File

@@ -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.
@@ -44,9 +42,6 @@ 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
{
$file = $this->getIndexFilename();
@@ -92,9 +87,6 @@ class FileProfilerStorage implements ProfilerStorageInterface
return array_values($result);
}
/**
* {@inheritdoc}
*/
public function purge()
{
$flags = \FilesystemIterator::SKIP_DOTS;
@@ -110,17 +102,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
@@ -190,10 +177,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 +189,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 +201,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);

View File

@@ -20,28 +20,24 @@ use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
*/
class Profile
{
private $token;
private string $token;
/**
* @var DataCollectorInterface[]
*/
private $collectors = [];
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 = [];
private array $children = [];
public function __construct(string $token)
{
@@ -55,10 +51,8 @@ class Profile
/**
* Gets the token.
*
* @return string
*/
public function getToken()
public function getToken(): string
{
return $this->token;
}
@@ -73,30 +67,24 @@ class Profile
/**
* Returns the parent profile.
*
* @return self|null
*/
public function getParent()
public function getParent(): ?self
{
return $this->parent;
}
/**
* Returns the parent token.
*
* @return string|null
*/
public function getParentToken()
public function getParentToken(): ?string
{
return $this->parent ? $this->parent->getToken() : null;
return $this->parent?->getToken();
}
/**
* Returns the IP.
*
* @return string|null
*/
public function getIp()
public function getIp(): ?string
{
return $this->ip;
}
@@ -108,10 +96,8 @@ class Profile
/**
* Returns the request method.
*
* @return string|null
*/
public function getMethod()
public function getMethod(): ?string
{
return $this->method;
}
@@ -123,10 +109,8 @@ class Profile
/**
* Returns the URL.
*
* @return string|null
*/
public function getUrl()
public function getUrl(): ?string
{
return $this->url;
}
@@ -136,10 +120,7 @@ class Profile
$this->url = $url;
}
/**
* @return int
*/
public function getTime()
public function getTime(): int
{
return $this->time ?? 0;
}
@@ -154,10 +135,7 @@ class Profile
$this->statusCode = $statusCode;
}
/**
* @return int|null
*/
public function getStatusCode()
public function getStatusCode(): ?int
{
return $this->statusCode;
}
@@ -167,7 +145,7 @@ class Profile
*
* @return self[]
*/
public function getChildren()
public function getChildren(): array
{
return $this->children;
}
@@ -208,11 +186,9 @@ class Profile
/**
* Gets a Collector by name.
*
* @return DataCollectorInterface
*
* @throws \InvalidArgumentException if the collector does not exist
*/
public function getCollector(string $name)
public function getCollector(string $name): DataCollectorInterface
{
if (!isset($this->collectors[$name])) {
throw new \InvalidArgumentException(sprintf('Collector "%s" does not exist.', $name));
@@ -226,7 +202,7 @@ class Profile
*
* @return DataCollectorInterface[]
*/
public function getCollectors()
public function getCollectors(): array
{
return $this->collectors;
}
@@ -252,18 +228,12 @@ class Profile
$this->collectors[$collector->getName()] = $collector;
}
/**
* @return bool
*/
public function hasCollector(string $name)
public function hasCollector(string $name): bool
{
return isset($this->collectors[$name]);
}
/**
* @return array
*/
public function __sleep()
public function __sleep(): array
{
return ['token', 'parent', 'children', 'collectors', 'ip', 'method', 'url', 'time', 'statusCode'];
}

View File

@@ -26,16 +26,16 @@ use Symfony\Contracts\Service\ResetInterface;
*/
class Profiler implements ResetInterface
{
private $storage;
private ProfilerStorageInterface $storage;
/**
* @var DataCollectorInterface[]
*/
private $collectors = [];
private array $collectors = [];
private $logger;
private $initiallyEnabled = true;
private $enabled = true;
private ?LoggerInterface $logger;
private bool $initiallyEnabled = true;
private bool $enabled = true;
public function __construct(ProfilerStorageInterface $storage, LoggerInterface $logger = null, bool $enable = true)
{
@@ -60,12 +60,15 @@ class Profiler implements ResetInterface
$this->enabled = true;
}
public function isEnabled(): bool
{
return $this->enabled;
}
/**
* Loads the Profile for the given Response.
*
* @return Profile|null
*/
public function loadProfileFromResponse(Response $response)
public function loadProfileFromResponse(Response $response): ?Profile
{
if (!$token = $response->headers->get('X-Debug-Token')) {
return null;
@@ -76,20 +79,16 @@ class Profiler implements ResetInterface
/**
* Loads the Profile for the given token.
*
* @return Profile|null
*/
public function loadProfile(string $token)
public function loadProfile(string $token): ?Profile
{
return $this->storage->read($token);
}
/**
* Saves a Profile.
*
* @return bool
*/
public function saveProfile(Profile $profile)
public function saveProfile(Profile $profile): bool
{
// late collect
foreach ($profile->getCollectors() as $collector) {
@@ -120,21 +119,17 @@ class Profiler implements ResetInterface
* @param string|null $start The start date to search from
* @param string|null $end The end date to search to
*
* @return array
*
* @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, ?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.
*
* @return Profile|null
*/
public function collect(Request $request, Response $response, \Throwable $exception = null)
public function collect(Request $request, Response $response, \Throwable $exception = null): ?Profile
{
if (false === $this->enabled) {
return null;
@@ -147,7 +142,7 @@ class Profiler implements ResetInterface
$profile->setStatusCode($response->getStatusCode());
try {
$profile->setIp($request->getClientIp());
} catch (ConflictingHeadersException $e) {
} catch (ConflictingHeadersException) {
$profile->setIp('Unknown');
}
@@ -177,10 +172,8 @@ class Profiler implements ResetInterface
/**
* Gets the Collectors associated with this profiler.
*
* @return array
*/
public function all()
public function all(): array
{
return $this->collectors;
}
@@ -210,10 +203,8 @@ class Profiler implements ResetInterface
* Returns true if a Collector for the given name exists.
*
* @param string $name A collector name
*
* @return bool
*/
public function has(string $name)
public function has(string $name): bool
{
return isset($this->collectors[$name]);
}
@@ -223,11 +214,9 @@ class Profiler implements ResetInterface
*
* @param string $name A collector name
*
* @return DataCollectorInterface
*
* @throws \InvalidArgumentException if the collector does not exist
*/
public function get(string $name)
public function get(string $name): DataCollectorInterface
{
if (!isset($this->collectors[$name])) {
throw new \InvalidArgumentException(sprintf('Collector "%s" does not exist.', $name));
@@ -243,8 +232,8 @@ class Profiler implements ResetInterface
}
try {
$value = new \DateTime(is_numeric($value) ? '@'.$value : $value);
} catch (\Exception $e) {
$value = new \DateTimeImmutable(is_numeric($value) ? '@'.$value : $value);
} catch (\Exception) {
return null;
}