Final Class Yiisoft\Profiler\Profiler
| Inheritance | Yiisoft\Profiler\Profiler |
|---|---|
| Implements | Yiisoft\Profiler\ProfilerInterface |
Profiler provides profiling support. It stores profiling messages in the memory and sends them to different targets according to {@see Profiler::$targets}.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Initializes the profiler by registering {@see flush()} as a shutdown function. | Yiisoft\Profiler\Profiler |
| begin() | Yiisoft\Profiler\Profiler | |
| enable() | Enable or disable profiler. | Yiisoft\Profiler\Profiler |
| end() | Yiisoft\Profiler\Profiler | |
| findMessages() | Yiisoft\Profiler\Profiler | |
| flush() | Yiisoft\Profiler\Profiler | |
| getMessages() | Returns profiler messages. | Yiisoft\Profiler\Profiler |
| getTargets() | Yiisoft\Profiler\Profiler | |
| isEnabled() | Yiisoft\Profiler\Profiler |
Method Details
Initializes the profiler by registering {@see flush()} as a shutdown function.
| public mixed __construct ( \Psr\Log\LoggerInterface $logger, array $targets = [] ) | ||
| $logger | \Psr\Log\LoggerInterface |
Logger to use. |
| $targets | array |
Profiling targets to use. |
public function __construct(
private readonly LoggerInterface $logger,
array $targets = [],
) {
$this->setTargets($targets);
register_shutdown_function([$this, 'flush']);
}
| public void begin ( string $token, array $context = [] ) | ||
| $token | string | |
| $context | array | |
public function begin(string $token, array $context = []): void
{
if (!$this->enabled) {
return;
}
$category = $this->getCategoryFromContext($context);
$context = array_merge(
$context,
[
'token' => $token,
'category' => $category,
'nestedLevel' => $this->nestedLevel,
'time' => microtime(true),
'beginTime' => microtime(true),
'beginMemory' => memory_get_usage(),
]
);
$message = new Message($category, $token, $context);
$this->pendingMessages[$category][$token][] = $message;
$this->nestedLevel++;
}
Enable or disable profiler.
| public $this enable ( boolean $value = true ) | ||
| $value | boolean | |
public function enable(bool $value = true): self
{
$new = clone $this;
$new->enabled = $value;
return $new;
}
| public void end ( string $token, array $context = [] ) | ||
| $token | string | |
| $context | array | |
public function end(string $token, array $context = []): void
{
if (!$this->enabled) {
return;
}
$category = $this->getCategoryFromContext($context);
if (empty($this->pendingMessages[$category][$token])) {
throw new RuntimeException(
sprintf(
'Unexpected %s::end() call for category "%s" token "%s". A matching begin() was not found.',
self::class,
$category,
$token
)
);
}
$message = array_pop($this->pendingMessages[$category][$token]);
/**
* @psalm-suppress TypeDoesNotContainType, DocblockTypeContradiction
*
* @link https://github.com/vimeo/psalm/issues/7376
*/
if (empty($this->pendingMessages[$category][$token])) {
unset($this->pendingMessages[$category][$token]);
if (empty($this->pendingMessages[$category])) {
unset($this->pendingMessages[$category]);
}
}
if (array_key_exists('beginTime', $context)) {
throw new InvalidArgumentException('It is forbidden to override "beginTime" in context.');
}
if (array_key_exists('beginMemory', $context)) {
throw new InvalidArgumentException('It is forbidden to override "beginMemory" in context.');
}
$context = array_merge(
$message->context(),
$context,
[
'endTime' => microtime(true),
'endMemory' => memory_get_usage(),
]
);
/**
* @psalm-var array&array{
* beginTime: float,
* endTime: float,
* beginMemory: int,
* endMemory: int,
* } $context
*/
$context['duration'] = $context['endTime'] - $context['beginTime'];
$context['memoryDiff'] = $context['endMemory'] - $context['beginMemory'];
$this->messages[] = new Message($category, $message->token(), $context);
$this->nestedLevel--;
}
| public array findMessages ( string $token ) | ||
| $token | string | |
public function findMessages(string $token): array
{
$messages = $this->messages;
return array_filter($messages, static fn (Message $message) => $message->token() === $token);
}
| public void flush ( ) |
public function flush(): void
{
foreach ($this->pendingMessages as $category => $categoryMessages) {
$this->logCategoryMessages($category, $categoryMessages);
}
$this->pendingMessages = [];
$this->nestedLevel = 0;
if (empty($this->messages)) {
return;
}
$messages = $this->messages;
// New messages could appear while the existing ones are being handled by targets.
$this->messages = [];
$this->dispatch($messages);
}
Returns profiler messages.
| public Yiisoft\Profiler\Message[] getMessages ( ) | ||
| return | Yiisoft\Profiler\Message[] |
The profiler messages. |
|---|---|---|
public function getMessages(): array
{
return $this->messages;
}
| public Yiisoft\Profiler\Target\TargetInterface[] getTargets ( ) | ||
| return | Yiisoft\Profiler\Target\TargetInterface[] |
Profiling targets. Each array element represents a single {@see \Yiisoft\Profiler\Target\TargetInterface profiling target} instance. |
|---|---|---|
public function getTargets(): array
{
return $this->targets;
}
Signup or Login in order to comment.