Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ignore commands #144

Merged
merged 11 commits into from
Jul 29, 2022
1 change: 1 addition & 0 deletions config/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
$params['yiisoft/yii-debug']['collectors.console'] ?? []
)
),
'ignoredCommands' => $params['yiisoft/yii-debug']['ignoredCommands'],
],
],
];
1 change: 1 addition & 0 deletions config/events-console.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
[CommandCollector::class, 'collect'],
],
ConsoleErrorEvent::class => [
[ConsoleAppInfoCollector::class, 'collect'],
[CommandCollector::class, 'collect'],
],
ConsoleTerminateEvent::class => [
Expand Down
9 changes: 8 additions & 1 deletion config/params.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,16 @@
],
'logLevel' => ContainerInterfaceProxy::LOG_ARGUMENTS | ContainerInterfaceProxy::LOG_RESULT | ContainerInterfaceProxy::LOG_ERROR,
'path' => '@runtime/debug',
'optionalRequests' => [
'ignoredRequests' => [
'/assets/*',
],
'ignoredCommands' => [
'completion',
'help',
'list',
'serve',
'debug/reset',
],
],
'yiisoft/yii-console' => [
'commands' => [
Expand Down
4 changes: 2 additions & 2 deletions config/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
'collectors' => ReferencesArray::from(
array_merge(
$params['yiisoft/yii-debug']['collectors'],
$params['yiisoft/yii-debug']['collectors.web'] ?? []
$params['yiisoft/yii-debug']['collectors.web'] ?? [],
)
),
'optionalRequests' => $params['yiisoft/yii-debug']['optionalRequests'],
'ignoredRequests' => $params['yiisoft/yii-debug']['ignoredRequests'],
],
],
];
24 changes: 18 additions & 6 deletions src/Collector/CommandCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Yii\Debug\Collector;

use RuntimeException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\Console\Event\ConsoleEvent;
Expand Down Expand Up @@ -32,17 +33,20 @@ public function collect(object $event): void

if ($event instanceof ConsoleErrorEvent) {
$this->commands[get_class($event)] = [
'name' => $event->getInput()->getFirstArgument() ?? '',
'command' => $event->getCommand(),
'input' => $event->getInput()->__toString(),
'output' => $event->getOutput()->fetch(),
'error' => $event->getError()->getMessage(),
'exitCode' => $event->getExitCode(),
];

return;
}

if ($event instanceof ConsoleTerminateEvent) {
$this->commands[get_class($event)] = [
'name' => $event->getCommand()->getName(),
'command' => $event->getCommand(),
'input' => $event->getInput()->__toString(),
'output' => $event->getOutput()->fetch(),
Expand All @@ -53,6 +57,7 @@ public function collect(object $event): void

if ($event instanceof ConsoleEvent) {
$this->commands[get_class($event)] = [
'name' => $event->getCommand()->getName(),
'command' => $event->getCommand(),
'input' => $event->getInput()->__toString(),
'output' => $event->getOutput()->fetch(),
Expand All @@ -68,25 +73,32 @@ public function getIndexData(): array
ConsoleCommandEvent::class,
];

$command = null;
$commandEvent = null;
foreach ($eventTypes as $eventType) {
if (!array_key_exists($eventType, $this->commands)) {
continue;
}

$command = $this->commands[$eventType];
$commandEvent = $this->commands[$eventType];
break;
}

if ($command === null) {
if ($commandEvent === null) {
$types = array_keys($this->commands);
throw new RuntimeException('Unsupported event type encountered among "' . implode('", "', $types) . '".');
throw new RuntimeException(
sprintf(
'Unsupported event type encountered among "%s". Supported only "%s"',
implode('", "', $types),
implode('", "', $eventTypes),
)
);
}

return [
'command' => [
'input' => $command['input'],
'class' => $command['command'] !== null ? get_class($command['command']) : null,
'name' => $commandEvent['name'],
'class' => $commandEvent['command'] instanceof Command ? get_class($commandEvent['command']) : null,
'input' => $commandEvent['input'],
],
];
}
Expand Down
10 changes: 9 additions & 1 deletion src/Collector/ConsoleAppInfoCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Yii\Debug\Collector;

use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Yiisoft\Yii\Console\Event\ApplicationShutdown;
use Yiisoft\Yii\Console\Event\ApplicationStartup;
Expand All @@ -22,7 +23,7 @@ public function getCollected(): array
{
return [
'applicationProcessingTime' => $this->applicationProcessingTimeStopped - $this->applicationProcessingTimeStarted,
'preloadTime' => $this->requestProcessingTimeStarted - $this->applicationProcessingTimeStarted,
'preloadTime' => $this->applicationProcessingTimeStarted - $this->requestProcessingTimeStarted,
'applicationEmit' => $this->applicationProcessingTimeStopped - $this->requestProcessingTimeStopped,
'requestProcessingTime' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted,
'memoryPeakUsage' => memory_get_peak_usage(),
Expand All @@ -40,6 +41,13 @@ public function collect(object $event): void
$this->applicationProcessingTimeStarted = microtime(true);
} elseif ($event instanceof ConsoleCommandEvent) {
$this->requestProcessingTimeStarted = microtime(true);
} elseif ($event instanceof ConsoleErrorEvent) {
/**
* If we receive this event, then {@see ConsoleCommandEvent} hasn't received and won't.
* So {@see requestProcessingTimeStarted} equals to 0 now and better to set it at least with application startup time.
*/
$this->requestProcessingTimeStarted = $this->applicationProcessingTimeStarted;
$this->requestProcessingTimeStopped = microtime(true);
} elseif ($event instanceof ConsoleTerminateEvent) {
$this->requestProcessingTimeStopped = microtime(true);
} elseif ($event instanceof ApplicationShutdown) {
Expand Down
68 changes: 47 additions & 21 deletions src/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,25 @@

use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\Strings\WildcardPattern;
use Yiisoft\Yii\Console\Event\ApplicationStartup;
use Yiisoft\Yii\Debug\Collector\CollectorInterface;
use Yiisoft\Yii\Debug\Storage\StorageInterface;
use Yiisoft\Yii\Http\Event\BeforeRequest;

final class Debugger
{
/**
* @var CollectorInterface[]
*/
private array $collectors;
private bool $skipCollect = false;
private array $optionalRequests;
private StorageInterface $target;
private DebuggerIdGenerator $idGenerator;

public function __construct(
DebuggerIdGenerator $idGenerator,
StorageInterface $target,
array $collectors,
array $optionalRequests = []
private DebuggerIdGenerator $idGenerator,
private StorageInterface $target,
/**
* @var CollectorInterface[]
*/
private array $collectors,
private array $ignoredRequests = [],
private array $ignoredCommands = [],
) {
$this->collectors = $collectors;
$this->optionalRequests = $optionalRequests;
$this->target = $target;
$this->idGenerator = $idGenerator;
}

public function getId(): string
Expand All @@ -40,7 +34,12 @@ public function getId(): string

public function startup(object $event): void
{
if ($event instanceof BeforeRequest && $this->isOptionalRequest($event->getRequest())) {
if ($event instanceof BeforeRequest && $this->isRequestIgnored($event->getRequest())) {
$this->skipCollect = true;
return;
}

if ($event instanceof ApplicationStartup && $this->isCommandIgnored($event->commandName)) {
$this->skipCollect = true;
return;
}
Expand All @@ -52,17 +51,30 @@ public function startup(object $event): void
}
}

private function isOptionalRequest(ServerRequestInterface $request): bool
private function isRequestIgnored(ServerRequestInterface $request): bool
{
$path = $request->getUri()->getPath();
foreach ($this->optionalRequests as $pattern) {
foreach ($this->ignoredRequests as $pattern) {
if ((new WildcardPattern($pattern))->match($path)) {
return true;
}
}
return false;
}

private function isCommandIgnored(?string $command): bool
{
if ($command === null || $command === '') {
return true;
}
foreach ($this->ignoredCommands as $pattern) {
if ((new WildcardPattern($pattern))->match($command)) {
return true;
}
}
return false;
}

public function shutdown(): void
{
try {
Expand All @@ -78,16 +90,30 @@ public function shutdown(): void
}

/**
* @param array $optionalRequests Patterns for optional request URLs.
* @param array $ignoredRequests Patterns for ignored request URLs.
*
* @return self
*
* @see WildcardPattern
*/
public function withIgnoredRequests(array $ignoredRequests): self
{
$new = clone $this;
$new->ignoredRequests = $ignoredRequests;
return $new;
}

/**
* @param array $ignoredCommands Patterns for ignored commands names.
*
* @return self
*
* @see WildcardPattern
*/
public function withOptionalRequests(array $optionalRequests): self
public function withIgnoredCommands(array $ignoredCommands): self
{
$new = clone $this;
$new->optionalRequests = $optionalRequests;
$new->ignoredCommands = $ignoredCommands;
return $new;
}
}
4 changes: 2 additions & 2 deletions tests/Collector/ConsoleAppInfoCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
final class ConsoleAppInfoCollectorTest extends CollectorTestCase
{
/**
* @param WebAppInfoCollector|\Yiisoft\Yii\Debug\Collector\CollectorInterface $collector
* @param CollectorInterface|WebAppInfoCollector $collector
*/
protected function collectTestData(CollectorInterface $collector): void
{
$collector->collect(new ApplicationStartup());
$collector->collect(new ApplicationStartup(null));
usleep(123_000);
$collector->collect(new ApplicationShutdown(0));
}
Expand Down
16 changes: 13 additions & 3 deletions tests/DebuggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Nyholm\Psr7\ServerRequest;
use PHPUnit\Framework\TestCase;
use stdClass;
use Yiisoft\Yii\Debug\Collector\CollectorInterface;
use Yiisoft\Yii\Debug\Debugger;
use Yiisoft\Yii\Debug\DebuggerIdGenerator;
Expand All @@ -22,7 +23,7 @@ public function testStartup(): void
$collector->expects($this->once())->method('startup');

$debugger = new Debugger($idGenerator, new MemoryStorage($idGenerator), [$collector]);
$debugger->startup(new \stdClass());
$debugger->startup(new stdClass());
}

public function testStartupWithSkipCollect(): void
Expand All @@ -43,11 +44,20 @@ public function testGetId(): void
$this->assertEquals($idGenerator->getId(), $debugger->getId());
}

public function testWithOptionalRequests(): void
public function testWithIgnoredRequests(): void
{
$idGenerator = new DebuggerIdGenerator();
$debugger1 = new Debugger($idGenerator, new MemoryStorage($idGenerator), []);
$debugger2 = $debugger1->withOptionalRequests(['/test']);
$debugger2 = $debugger1->withIgnoredRequests(['/test']);

$this->assertNotSame($debugger1, $debugger2);
}

public function testWithIgnoredCommands(): void
{
$idGenerator = new DebuggerIdGenerator();
$debugger1 = new Debugger($idGenerator, new MemoryStorage($idGenerator), []);
$debugger2 = $debugger1->withIgnoredCommands(['command/test']);

$this->assertNotSame($debugger1, $debugger2);
}
Expand Down