0 follower

Final Class Yiisoft\Profiler\Profiler

InheritanceYiisoft\Profiler\Profiler
ImplementsYiisoft\Profiler\ProfilerInterface

Profiler provides profiling support. It stores profiling messages in the memory and sends them to different targets according to {@see Profiler::$targets}.

Method Details

Hide inherited methods

__construct() public method

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']);
}

            
begin() public method

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() public method

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;
}

            
end() public method

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--;
}

            
findMessages() public method

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);
}

            
flush() public method

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);
}

            
getMessages() public method

Returns profiler messages.

public Yiisoft\Profiler\Message[] getMessages ( )
return Yiisoft\Profiler\Message[]

The profiler messages.

                public function getMessages(): array
{
    return $this->messages;
}

            
getTargets() public method

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;
}

            
isEnabled() public method

public boolean isEnabled ( )
return boolean

If profiler is enabled.

{@see \Yiisoft\Profiler\enable}

                public function isEnabled(): bool
{
    return $this->enabled;
}