From 67ddbe5a08b64490f0c7f93df9336092fbac4bdb Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 16 Jan 2025 15:24:42 +0300 Subject: [PATCH 01/25] Introduce `PreventionPolicyInterface` --- config/di-console.php | 11 +- config/di-web.php | 13 ++- src/Debugger.php | 46 +------- src/PreventionPolicy/CommandPolicy.php | 37 ++++++ src/PreventionPolicy/CompositePolicy.php | 32 ++++++ .../EnvironmentVariablePolicy.php | 18 +++ src/PreventionPolicy/HeaderPolicy.php | 26 +++++ src/PreventionPolicy/PredefinedPolicy.php | 18 +++ .../PreventionPolicyInterface.php | 10 ++ src/PreventionPolicy/RequestPolicy.php | 37 ++++++ tests/Unit/DebuggerTest.php | 106 +----------------- 11 files changed, 206 insertions(+), 148 deletions(-) create mode 100644 src/PreventionPolicy/CommandPolicy.php create mode 100644 src/PreventionPolicy/CompositePolicy.php create mode 100644 src/PreventionPolicy/EnvironmentVariablePolicy.php create mode 100644 src/PreventionPolicy/HeaderPolicy.php create mode 100644 src/PreventionPolicy/PredefinedPolicy.php create mode 100644 src/PreventionPolicy/PreventionPolicyInterface.php create mode 100644 src/PreventionPolicy/RequestPolicy.php diff --git a/config/di-console.php b/config/di-console.php index 711de43a..dbc35fd0 100644 --- a/config/di-console.php +++ b/config/di-console.php @@ -2,8 +2,12 @@ declare(strict_types=1); +use Yiisoft\Definitions\DynamicReference; use Yiisoft\Definitions\ReferencesArray; use Yiisoft\Yii\Debug\Debugger; +use Yiisoft\Yii\Debug\PreventionPolicy\CommandPolicy; +use Yiisoft\Yii\Debug\PreventionPolicy\CompositePolicy; +use Yiisoft\Yii\Debug\PreventionPolicy\EnvironmentVariablePolicy; if (!(bool)($params['yiisoft/yii-debug']['enabled'] ?? false)) { return []; @@ -18,7 +22,12 @@ $params['yiisoft/yii-debug']['collectors.console'] ?? [] ) ), - 'ignoredCommands' => $params['yiisoft/yii-debug']['ignoredCommands'], + 'startupPreventionPolicy' => DynamicReference::to( + static fn () => new CompositePolicy( + new EnvironmentVariablePolicy(), + new CommandPolicy($params['yiisoft/yii-debug']['ignoredCommands']) + ), + ), 'excludedClasses' => $params['yiisoft/yii-debug']['excludedClasses'], ], ], diff --git a/config/di-web.php b/config/di-web.php index 946141e7..8a497543 100644 --- a/config/di-web.php +++ b/config/di-web.php @@ -2,8 +2,13 @@ declare(strict_types=1); +use Yiisoft\Definitions\DynamicReference; use Yiisoft\Definitions\ReferencesArray; use Yiisoft\Yii\Debug\Debugger; +use Yiisoft\Yii\Debug\PreventionPolicy\CompositePolicy; +use Yiisoft\Yii\Debug\PreventionPolicy\EnvironmentVariablePolicy; +use Yiisoft\Yii\Debug\PreventionPolicy\HeaderPolicy; +use Yiisoft\Yii\Debug\PreventionPolicy\RequestPolicy; if (!(bool)($params['yiisoft/yii-debug']['enabled'] ?? false)) { return []; @@ -18,7 +23,13 @@ $params['yiisoft/yii-debug']['collectors.web'] ?? [], ) ), - 'ignoredRequests' => $params['yiisoft/yii-debug']['ignoredRequests'], + 'startupPreventionPolicy' => DynamicReference::to( + static fn () => new CompositePolicy( + new EnvironmentVariablePolicy(), + new HeaderPolicy(), + new RequestPolicy($params['yiisoft/yii-debug']['ignoredRequests']) + ), + ), 'excludedClasses' => $params['yiisoft/yii-debug']['excludedClasses'], ], ], diff --git a/src/Debugger.php b/src/Debugger.php index 06434afa..81ef2dec 100644 --- a/src/Debugger.php +++ b/src/Debugger.php @@ -5,13 +5,10 @@ namespace Yiisoft\Yii\Debug; use LogicException; -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\Collector\SummaryCollectorInterface; +use Yiisoft\Yii\Debug\PreventionPolicy\PreventionPolicyInterface; use Yiisoft\Yii\Debug\Storage\StorageInterface; -use Yiisoft\Yii\Http\Event\BeforeRequest; /** * @psalm-type BacktraceType = list @@ -31,14 +28,11 @@ final class Debugger /** * @param CollectorInterface[] $collectors - * @param string[] $ignoredRequests - * @param string[] $ignoredCommands */ public function __construct( private readonly StorageInterface $storage, array $collectors, - private readonly array $ignoredRequests = [], - private readonly array $ignoredCommands = [], + private readonly ?PreventionPolicyInterface $startupPreventionPolicy = null, array $excludedClasses = [], ) { $preparedCollectors = []; @@ -64,11 +58,7 @@ public function getId(): string public function startup(object $event): void { - if ($event instanceof BeforeRequest && $this->isRequestIgnored($event->getRequest())) { - return; - } - - if ($event instanceof ApplicationStartup && $this->isCommandIgnored($event->commandName)) { + if ($this->startupPreventionPolicy?->shouldPrevent($event)) { return; } @@ -118,36 +108,6 @@ public function stop(): void $this->id = null; } - private function isRequestIgnored(ServerRequestInterface $request): bool - { - if ($request->hasHeader('X-Debug-Ignore') && $request->getHeaderLine('X-Debug-Ignore') === 'true') { - return true; - } - $path = $request->getUri()->getPath(); - 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; - } - if (getenv('YII_DEBUG_IGNORE') === 'true') { - return true; - } - foreach ($this->ignoredCommands as $pattern) { - if ((new WildcardPattern($pattern))->match($command)) { - return true; - } - } - return false; - } - /** * Collects summary data of current request. */ diff --git a/src/PreventionPolicy/CommandPolicy.php b/src/PreventionPolicy/CommandPolicy.php new file mode 100644 index 00000000..492a3c34 --- /dev/null +++ b/src/PreventionPolicy/CommandPolicy.php @@ -0,0 +1,37 @@ + + */ + private readonly array $ignoreCommands = [], + ) { + } + + public function shouldPrevent(object $event): bool + { + if (!$event instanceof ApplicationStartup) { + return false; + } + + $command = (string) $event->commandName; + + foreach ($this->ignoreCommands as $pattern) { + if ((new WildcardPattern($pattern))->match($command)) { + return true; + } + } + + return false; + } +} diff --git a/src/PreventionPolicy/CompositePolicy.php b/src/PreventionPolicy/CompositePolicy.php new file mode 100644 index 00000000..e03c8d67 --- /dev/null +++ b/src/PreventionPolicy/CompositePolicy.php @@ -0,0 +1,32 @@ + + */ + private array $policies; + + /** + * @no-named-arguments + */ + public function __construct(PreventionPolicyInterface ...$policies) + { + $this->policies = $policies; + } + + public function shouldPrevent(object $event): bool + { + foreach ($this->policies as $policy) { + if ($policy->shouldPrevent($event)) { + return true; + } + } + + return false; + } +} diff --git a/src/PreventionPolicy/EnvironmentVariablePolicy.php b/src/PreventionPolicy/EnvironmentVariablePolicy.php new file mode 100644 index 00000000..7673ca1e --- /dev/null +++ b/src/PreventionPolicy/EnvironmentVariablePolicy.php @@ -0,0 +1,18 @@ +variableName); + } +} diff --git a/src/PreventionPolicy/HeaderPolicy.php b/src/PreventionPolicy/HeaderPolicy.php new file mode 100644 index 00000000..bab720ed --- /dev/null +++ b/src/PreventionPolicy/HeaderPolicy.php @@ -0,0 +1,26 @@ +getRequest(); + + return $request->hasHeader($this->headerName) && $request->getHeaderLine($this->headerName); + } +} diff --git a/src/PreventionPolicy/PredefinedPolicy.php b/src/PreventionPolicy/PredefinedPolicy.php new file mode 100644 index 00000000..8d9e96da --- /dev/null +++ b/src/PreventionPolicy/PredefinedPolicy.php @@ -0,0 +1,18 @@ +shouldPrevent; + } +} diff --git a/src/PreventionPolicy/PreventionPolicyInterface.php b/src/PreventionPolicy/PreventionPolicyInterface.php new file mode 100644 index 00000000..b0fefa9f --- /dev/null +++ b/src/PreventionPolicy/PreventionPolicyInterface.php @@ -0,0 +1,10 @@ + + */ + private readonly array $ignoreUriPaths = [], + ) { + } + + public function shouldPrevent(object $event): bool + { + if (!$event instanceof BeforeRequest) { + return false; + } + + $path = $event->getRequest()->getUri()->getPath(); + + foreach ($this->ignoreUriPaths as $pattern) { + if ((new WildcardPattern($pattern))->match($path)) { + return true; + } + } + + return false; + } +} diff --git a/tests/Unit/DebuggerTest.php b/tests/Unit/DebuggerTest.php index 0477b446..f11cfa28 100644 --- a/tests/Unit/DebuggerTest.php +++ b/tests/Unit/DebuggerTest.php @@ -11,6 +11,7 @@ use Yiisoft\Yii\Console\Event\ApplicationStartup; use Yiisoft\Yii\Debug\Collector\CollectorInterface; use Yiisoft\Yii\Debug\Debugger; +use Yiisoft\Yii\Debug\PreventionPolicy\PredefinedPolicy; use Yiisoft\Yii\Debug\Storage\MemoryStorage; use Yiisoft\Yii\Debug\Storage\StorageInterface; use Yiisoft\Yii\Http\Event\BeforeRequest; @@ -27,46 +28,6 @@ public function testStartup(): void $debugger->startup(new stdClass()); } - public function testStartupWithSkipCollect(): void - { - $collector = $this->getMockBuilder(CollectorInterface::class)->getMock(); - $collector->expects($this->once())->method('startup'); - $storage = new MemoryStorage(); - - $debugger = new Debugger($storage, [$collector], ['/test']); - $debugger->startup(new BeforeRequest(new ServerRequest('GET', '/debug'))); - } - - public function testIgnoreByHeader(): void - { - $collector = $this->getMockBuilder(CollectorInterface::class)->getMock(); - $collector->expects($this->never())->method('startup'); - $collector->expects($this->never())->method('shutdown'); - - $storage = $this->getMockBuilder(StorageInterface::class)->getMock(); - $storage->expects($this->never())->method('write'); - - $debugger = new Debugger($storage, [$collector], []); - $debugger->startup(new BeforeRequest(new ServerRequest('GET', '/test', ['X-Debug-Ignore' => 'true']))); - $debugger->shutdown(); - } - - public function testIgnoreByEnv(): void - { - $collector = $this->getMockBuilder(CollectorInterface::class)->getMock(); - $collector->expects($this->never())->method('startup'); - $collector->expects($this->never())->method('shutdown'); - - $storage = $this->getMockBuilder(StorageInterface::class)->getMock(); - $storage->expects($this->never())->method('write'); - - putenv('YII_DEBUG_IGNORE=true'); - $debugger = new Debugger($storage, [$collector], []); - $debugger->startup(new ApplicationStartup('command')); - putenv('YII_DEBUG_IGNORE=false'); - $debugger->shutdown(); - } - public function testShutdown(): void { $collector = $this->getMockBuilder(CollectorInterface::class)->getMock(); @@ -81,7 +42,7 @@ public function testShutdown(): void $debugger->shutdown(); } - public function testShutdownWithSkipRequestCollect(): void + public function testShutdownWithStartupPrevention(): void { $collector = $this->getMockBuilder(CollectorInterface::class)->getMock(); $collector->expects($this->never())->method('startup'); @@ -90,72 +51,11 @@ public function testShutdownWithSkipRequestCollect(): void $storage = $this->getMockBuilder(StorageInterface::class)->getMock(); $storage->expects($this->never())->method('write'); - $debugger = new Debugger($storage, [$collector], ['/test']); + $debugger = new Debugger($storage, [$collector], new PredefinedPolicy(true)); $debugger->startup(new BeforeRequest(new ServerRequest('GET', '/test'))); $debugger->shutdown(); } - #[DataProvider('dataShutdownWithSkipCommandCollect')] - public function testShutdownWithSkipCommandCollect(array $ignoredCommands, ?string $ignoredCommand): void - { - $collector = $this->getMockBuilder(CollectorInterface::class)->getMock(); - $collector->expects($this->never())->method('startup'); - $collector->expects($this->never())->method('shutdown'); - - $storage = $this->getMockBuilder(StorageInterface::class)->getMock(); - $storage->expects($this->never())->method('write'); - - $debugger = new Debugger($storage, [$collector], [], $ignoredCommands); - $debugger->startup(new ApplicationStartup($ignoredCommand)); - $debugger->shutdown(); - } - - public static function dataShutdownWithSkipCommandCollect(): iterable - { - yield [ - ['app:ignored-command'], - 'app:ignored-command', - ]; - yield [ - ['app:ignored-command1', 'app:ignored-command2'], - 'app:ignored-command2', - ]; - yield [ - ['app:ignored-command'], - null, - ]; - yield [ - ['app:ignored-command'], - '', - ]; - } - - #[DataProvider('dataShutdownWithoutSkipCommandCollect')] - public function testShutdownWithoutSkipCommandCollect(array $ignoredCommands, ?string $ignoredCommand): void - { - $collector = $this->getMockBuilder(CollectorInterface::class)->getMock(); - $collector->expects($this->once())->method('startup'); - $collector->expects($this->once())->method('shutdown'); - $storage = $this->getMockBuilder(StorageInterface::class)->getMock(); - $storage->expects($this->once())->method('write'); - - $debugger = new Debugger($storage, [$collector], [], $ignoredCommands); - $debugger->startup(new ApplicationStartup($ignoredCommand)); - $debugger->shutdown(); - } - - public static function dataShutdownWithoutSkipCommandCollect(): iterable - { - yield [ - [], - 'app:not-ignored-command', - ]; - yield [ - ['app:ignored-command'], - 'app:not-ignored-command', - ]; - } - public function testStopSkipped(): void { $collector = $this->getMockBuilder(CollectorInterface::class)->getMock(); From f00a0cc54b69a04ae680883b801450dfef38594a Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 16 Jan 2025 12:25:05 +0000 Subject: [PATCH 02/25] Apply fixes from StyleCI --- tests/Unit/DebuggerTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/Unit/DebuggerTest.php b/tests/Unit/DebuggerTest.php index f11cfa28..8c0199c8 100644 --- a/tests/Unit/DebuggerTest.php +++ b/tests/Unit/DebuggerTest.php @@ -5,10 +5,8 @@ namespace Yiisoft\Yii\Debug\Tests\Unit; use Nyholm\Psr7\ServerRequest; -use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use stdClass; -use Yiisoft\Yii\Console\Event\ApplicationStartup; use Yiisoft\Yii\Debug\Collector\CollectorInterface; use Yiisoft\Yii\Debug\Debugger; use Yiisoft\Yii\Debug\PreventionPolicy\PredefinedPolicy; From f1ef706262e895f3ebe2080a4ef52dcef173b239 Mon Sep 17 00:00:00 2001 From: vjik <525501+vjik@users.noreply.github.com> Date: Thu, 16 Jan 2025 12:35:46 +0000 Subject: [PATCH 03/25] Apply Rector changes (CI) --- src/PreventionPolicy/CompositePolicy.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PreventionPolicy/CompositePolicy.php b/src/PreventionPolicy/CompositePolicy.php index e03c8d67..78a446f9 100644 --- a/src/PreventionPolicy/CompositePolicy.php +++ b/src/PreventionPolicy/CompositePolicy.php @@ -9,7 +9,7 @@ final class CompositePolicy implements PreventionPolicyInterface /** * @psalm-var list */ - private array $policies; + private readonly array $policies; /** * @no-named-arguments From 86de140c6bd5370c4a77374cee3b74ea28e99b83 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 16 Jan 2025 16:15:13 +0300 Subject: [PATCH 04/25] improve --- config/di-console.php | 14 ++++---- config/di-web.php | 18 +++++------ src/Debugger.php | 12 ++++--- src/PreventionPolicy/CompositePolicy.php | 32 ------------------- .../EnvironmentVariablePolicy.php | 18 ----------- src/PreventionPolicy/PredefinedPolicy.php | 18 ----------- .../PreventionPolicyInterface.php | 10 ------ .../Collector/BlackListCollectorPolicy.php | 30 +++++++++++++++++ .../Collector/CollectorPolicyInterface.php | 12 +++++++ .../Collector/WhiteListCollectorPolicy.php | 30 +++++++++++++++++ .../Condition/CommandCondition.php} | 10 +++--- .../Condition/ConditionInterface.php | 10 ++++++ .../EnvironmentVariableCondition.php | 18 +++++++++++ .../Condition/HeaderCondition.php} | 8 ++--- src/StartupPolicy/Condition/OrCondition.php | 32 +++++++++++++++++++ .../Condition/PredefinedCondition.php | 18 +++++++++++ .../Condition/RequestCondition.php} | 10 +++--- tests/Unit/DebuggerTest.php | 4 +-- 18 files changed, 190 insertions(+), 114 deletions(-) delete mode 100644 src/PreventionPolicy/CompositePolicy.php delete mode 100644 src/PreventionPolicy/EnvironmentVariablePolicy.php delete mode 100644 src/PreventionPolicy/PredefinedPolicy.php delete mode 100644 src/PreventionPolicy/PreventionPolicyInterface.php create mode 100644 src/StartupPolicy/Collector/BlackListCollectorPolicy.php create mode 100644 src/StartupPolicy/Collector/CollectorPolicyInterface.php create mode 100644 src/StartupPolicy/Collector/WhiteListCollectorPolicy.php rename src/{PreventionPolicy/CommandPolicy.php => StartupPolicy/Condition/CommandCondition.php} (67%) create mode 100644 src/StartupPolicy/Condition/ConditionInterface.php create mode 100644 src/StartupPolicy/Condition/EnvironmentVariableCondition.php rename src/{PreventionPolicy/HeaderPolicy.php => StartupPolicy/Condition/HeaderCondition.php} (61%) create mode 100644 src/StartupPolicy/Condition/OrCondition.php create mode 100644 src/StartupPolicy/Condition/PredefinedCondition.php rename src/{PreventionPolicy/RequestPolicy.php => StartupPolicy/Condition/RequestCondition.php} (66%) diff --git a/config/di-console.php b/config/di-console.php index dbc35fd0..270d3632 100644 --- a/config/di-console.php +++ b/config/di-console.php @@ -5,9 +5,9 @@ use Yiisoft\Definitions\DynamicReference; use Yiisoft\Definitions\ReferencesArray; use Yiisoft\Yii\Debug\Debugger; -use Yiisoft\Yii\Debug\PreventionPolicy\CommandPolicy; -use Yiisoft\Yii\Debug\PreventionPolicy\CompositePolicy; -use Yiisoft\Yii\Debug\PreventionPolicy\EnvironmentVariablePolicy; +use Yiisoft\Yii\Debug\StartupPolicy\Condition\CommandCondition; +use Yiisoft\Yii\Debug\StartupPolicy\Condition\OrCondition; +use Yiisoft\Yii\Debug\StartupPolicy\Condition\EnvironmentVariableCondition; if (!(bool)($params['yiisoft/yii-debug']['enabled'] ?? false)) { return []; @@ -22,10 +22,10 @@ $params['yiisoft/yii-debug']['collectors.console'] ?? [] ) ), - 'startupPreventionPolicy' => DynamicReference::to( - static fn () => new CompositePolicy( - new EnvironmentVariablePolicy(), - new CommandPolicy($params['yiisoft/yii-debug']['ignoredCommands']) + 'startupPreventionCondition' => DynamicReference::to( + static fn () => new OrCondition( + new EnvironmentVariableCondition('YII_DEBUG_IGNORE'), + new CommandCondition($params['yiisoft/yii-debug']['ignoredCommands']) ), ), 'excludedClasses' => $params['yiisoft/yii-debug']['excludedClasses'], diff --git a/config/di-web.php b/config/di-web.php index 8a497543..6d558a75 100644 --- a/config/di-web.php +++ b/config/di-web.php @@ -5,10 +5,10 @@ use Yiisoft\Definitions\DynamicReference; use Yiisoft\Definitions\ReferencesArray; use Yiisoft\Yii\Debug\Debugger; -use Yiisoft\Yii\Debug\PreventionPolicy\CompositePolicy; -use Yiisoft\Yii\Debug\PreventionPolicy\EnvironmentVariablePolicy; -use Yiisoft\Yii\Debug\PreventionPolicy\HeaderPolicy; -use Yiisoft\Yii\Debug\PreventionPolicy\RequestPolicy; +use Yiisoft\Yii\Debug\StartupPolicy\Condition\OrCondition; +use Yiisoft\Yii\Debug\StartupPolicy\Condition\EnvironmentVariableCondition; +use Yiisoft\Yii\Debug\StartupPolicy\Condition\HeaderCondition; +use Yiisoft\Yii\Debug\StartupPolicy\Condition\RequestCondition; if (!(bool)($params['yiisoft/yii-debug']['enabled'] ?? false)) { return []; @@ -23,11 +23,11 @@ $params['yiisoft/yii-debug']['collectors.web'] ?? [], ) ), - 'startupPreventionPolicy' => DynamicReference::to( - static fn () => new CompositePolicy( - new EnvironmentVariablePolicy(), - new HeaderPolicy(), - new RequestPolicy($params['yiisoft/yii-debug']['ignoredRequests']) + 'startupPreventionCondition' => DynamicReference::to( + static fn () => new OrCondition( + new EnvironmentVariableCondition('YII_DEBUG_IGNORE'), + new HeaderCondition('X-Debug-Ignore'), + new RequestCondition($params['yiisoft/yii-debug']['ignoredRequests']) ), ), 'excludedClasses' => $params['yiisoft/yii-debug']['excludedClasses'], diff --git a/src/Debugger.php b/src/Debugger.php index 81ef2dec..a71165d9 100644 --- a/src/Debugger.php +++ b/src/Debugger.php @@ -7,7 +7,8 @@ use LogicException; use Yiisoft\Yii\Debug\Collector\CollectorInterface; use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface; -use Yiisoft\Yii\Debug\PreventionPolicy\PreventionPolicyInterface; +use Yiisoft\Yii\Debug\StartupPolicy\Collector\CollectorPolicyInterface; +use Yiisoft\Yii\Debug\StartupPolicy\Condition\ConditionInterface; use Yiisoft\Yii\Debug\Storage\StorageInterface; /** @@ -32,7 +33,8 @@ final class Debugger public function __construct( private readonly StorageInterface $storage, array $collectors, - private readonly ?PreventionPolicyInterface $startupPreventionPolicy = null, + private readonly ?ConditionInterface $startupPreventionCondition = null, + private readonly ?CollectorPolicyInterface $collectorPolicy = null, array $excludedClasses = [], ) { $preparedCollectors = []; @@ -58,14 +60,16 @@ public function getId(): string public function startup(object $event): void { - if ($this->startupPreventionPolicy?->shouldPrevent($event)) { + if ($this->startupPreventionCondition?->match($event) === true) { return; } $this->id = str_replace('.', '', uniqid('', true)); foreach ($this->collectors as $collector) { - $collector->startup(); + if ($this->collectorPolicy?->shouldStartup($collector, $event) === true) { + $collector->startup(); + } } } diff --git a/src/PreventionPolicy/CompositePolicy.php b/src/PreventionPolicy/CompositePolicy.php deleted file mode 100644 index 78a446f9..00000000 --- a/src/PreventionPolicy/CompositePolicy.php +++ /dev/null @@ -1,32 +0,0 @@ - - */ - private readonly array $policies; - - /** - * @no-named-arguments - */ - public function __construct(PreventionPolicyInterface ...$policies) - { - $this->policies = $policies; - } - - public function shouldPrevent(object $event): bool - { - foreach ($this->policies as $policy) { - if ($policy->shouldPrevent($event)) { - return true; - } - } - - return false; - } -} diff --git a/src/PreventionPolicy/EnvironmentVariablePolicy.php b/src/PreventionPolicy/EnvironmentVariablePolicy.php deleted file mode 100644 index 7673ca1e..00000000 --- a/src/PreventionPolicy/EnvironmentVariablePolicy.php +++ /dev/null @@ -1,18 +0,0 @@ -variableName); - } -} diff --git a/src/PreventionPolicy/PredefinedPolicy.php b/src/PreventionPolicy/PredefinedPolicy.php deleted file mode 100644 index 8d9e96da..00000000 --- a/src/PreventionPolicy/PredefinedPolicy.php +++ /dev/null @@ -1,18 +0,0 @@ -shouldPrevent; - } -} diff --git a/src/PreventionPolicy/PreventionPolicyInterface.php b/src/PreventionPolicy/PreventionPolicyInterface.php deleted file mode 100644 index b0fefa9f..00000000 --- a/src/PreventionPolicy/PreventionPolicyInterface.php +++ /dev/null @@ -1,10 +0,0 @@ - + */ + private readonly array $conditions, + ) { + } + + public function shouldStartup(CollectorInterface $collector, object $event): bool + { + $condition = $this->conditions[$collector->getName()] ?? null; + if ($condition === null) { + return true; + } + + return !$condition->match($event); + } +} diff --git a/src/StartupPolicy/Collector/CollectorPolicyInterface.php b/src/StartupPolicy/Collector/CollectorPolicyInterface.php new file mode 100644 index 00000000..e404405d --- /dev/null +++ b/src/StartupPolicy/Collector/CollectorPolicyInterface.php @@ -0,0 +1,12 @@ + + */ + private readonly array $conditions, + ) { + } + + public function shouldStartup(CollectorInterface $collector, object $event): bool + { + $condition = $this->conditions[$collector->getName()] ?? null; + if ($condition === null) { + return false; + } + + return $condition->match($event); + } +} diff --git a/src/PreventionPolicy/CommandPolicy.php b/src/StartupPolicy/Condition/CommandCondition.php similarity index 67% rename from src/PreventionPolicy/CommandPolicy.php rename to src/StartupPolicy/Condition/CommandCondition.php index 492a3c34..3f53e8c8 100644 --- a/src/PreventionPolicy/CommandPolicy.php +++ b/src/StartupPolicy/Condition/CommandCondition.php @@ -2,23 +2,23 @@ declare(strict_types=1); -namespace Yiisoft\Yii\Debug\PreventionPolicy; +namespace Yiisoft\Yii\Debug\StartupPolicy\Condition; use Yiisoft\Strings\WildcardPattern; use Yiisoft\Yii\Console\Event\ApplicationStartup; -final class CommandPolicy implements PreventionPolicyInterface +final class CommandCondition implements ConditionInterface { public function __construct( /** * @var string[] * @psalm-var list */ - private readonly array $ignoreCommands = [], + private readonly array $commands, ) { } - public function shouldPrevent(object $event): bool + public function match(object $event): bool { if (!$event instanceof ApplicationStartup) { return false; @@ -26,7 +26,7 @@ public function shouldPrevent(object $event): bool $command = (string) $event->commandName; - foreach ($this->ignoreCommands as $pattern) { + foreach ($this->commands as $pattern) { if ((new WildcardPattern($pattern))->match($command)) { return true; } diff --git a/src/StartupPolicy/Condition/ConditionInterface.php b/src/StartupPolicy/Condition/ConditionInterface.php new file mode 100644 index 00000000..8b741815 --- /dev/null +++ b/src/StartupPolicy/Condition/ConditionInterface.php @@ -0,0 +1,10 @@ +variableName); + } +} diff --git a/src/PreventionPolicy/HeaderPolicy.php b/src/StartupPolicy/Condition/HeaderCondition.php similarity index 61% rename from src/PreventionPolicy/HeaderPolicy.php rename to src/StartupPolicy/Condition/HeaderCondition.php index bab720ed..2eef8500 100644 --- a/src/PreventionPolicy/HeaderPolicy.php +++ b/src/StartupPolicy/Condition/HeaderCondition.php @@ -2,18 +2,18 @@ declare(strict_types=1); -namespace Yiisoft\Yii\Debug\PreventionPolicy; +namespace Yiisoft\Yii\Debug\StartupPolicy\Condition; use Yiisoft\Yii\Http\Event\BeforeRequest; -final class HeaderPolicy implements PreventionPolicyInterface +final class HeaderCondition implements ConditionInterface { public function __construct( - private readonly string $headerName = 'X-Debug-Ignore', + private readonly string $headerName, ) { } - public function shouldPrevent(object $event): bool + public function match(object $event): bool { if (!$event instanceof BeforeRequest) { return false; diff --git a/src/StartupPolicy/Condition/OrCondition.php b/src/StartupPolicy/Condition/OrCondition.php new file mode 100644 index 00000000..ef858a7f --- /dev/null +++ b/src/StartupPolicy/Condition/OrCondition.php @@ -0,0 +1,32 @@ + + */ + private readonly array $conditions; + + /** + * @no-named-arguments + */ + public function __construct(ConditionInterface ...$policies) + { + $this->conditions = $policies; + } + + public function match(object $event): bool + { + foreach ($this->conditions as $policy) { + if ($policy->match($event)) { + return true; + } + } + + return false; + } +} diff --git a/src/StartupPolicy/Condition/PredefinedCondition.php b/src/StartupPolicy/Condition/PredefinedCondition.php new file mode 100644 index 00000000..00e714b2 --- /dev/null +++ b/src/StartupPolicy/Condition/PredefinedCondition.php @@ -0,0 +1,18 @@ +match; + } +} diff --git a/src/PreventionPolicy/RequestPolicy.php b/src/StartupPolicy/Condition/RequestCondition.php similarity index 66% rename from src/PreventionPolicy/RequestPolicy.php rename to src/StartupPolicy/Condition/RequestCondition.php index 8ce4403b..0e413c09 100644 --- a/src/PreventionPolicy/RequestPolicy.php +++ b/src/StartupPolicy/Condition/RequestCondition.php @@ -2,23 +2,23 @@ declare(strict_types=1); -namespace Yiisoft\Yii\Debug\PreventionPolicy; +namespace Yiisoft\Yii\Debug\StartupPolicy\Condition; use Yiisoft\Strings\WildcardPattern; use Yiisoft\Yii\Http\Event\BeforeRequest; -final class RequestPolicy implements PreventionPolicyInterface +final class RequestCondition implements ConditionInterface { public function __construct( /** * @var string[] * @psalm-var list */ - private readonly array $ignoreUriPaths = [], + private readonly array $uriPaths, ) { } - public function shouldPrevent(object $event): bool + public function match(object $event): bool { if (!$event instanceof BeforeRequest) { return false; @@ -26,7 +26,7 @@ public function shouldPrevent(object $event): bool $path = $event->getRequest()->getUri()->getPath(); - foreach ($this->ignoreUriPaths as $pattern) { + foreach ($this->uriPaths as $pattern) { if ((new WildcardPattern($pattern))->match($path)) { return true; } diff --git a/tests/Unit/DebuggerTest.php b/tests/Unit/DebuggerTest.php index 8c0199c8..ec40192c 100644 --- a/tests/Unit/DebuggerTest.php +++ b/tests/Unit/DebuggerTest.php @@ -9,7 +9,7 @@ use stdClass; use Yiisoft\Yii\Debug\Collector\CollectorInterface; use Yiisoft\Yii\Debug\Debugger; -use Yiisoft\Yii\Debug\PreventionPolicy\PredefinedPolicy; +use Yiisoft\Yii\Debug\StartupPolicy\Condition\PredefinedCondition; use Yiisoft\Yii\Debug\Storage\MemoryStorage; use Yiisoft\Yii\Debug\Storage\StorageInterface; use Yiisoft\Yii\Http\Event\BeforeRequest; @@ -49,7 +49,7 @@ public function testShutdownWithStartupPrevention(): void $storage = $this->getMockBuilder(StorageInterface::class)->getMock(); $storage->expects($this->never())->method('write'); - $debugger = new Debugger($storage, [$collector], new PredefinedPolicy(true)); + $debugger = new Debugger($storage, [$collector], new PredefinedCondition(true)); $debugger->startup(new BeforeRequest(new ServerRequest('GET', '/test'))); $debugger->shutdown(); } From 463a3ed993040bcb31c913d67aeb271dc8552154 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 16 Jan 2025 16:19:16 +0300 Subject: [PATCH 05/25] fix --- src/Debugger.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Debugger.php b/src/Debugger.php index a71165d9..3301138a 100644 --- a/src/Debugger.php +++ b/src/Debugger.php @@ -67,7 +67,7 @@ public function startup(object $event): void $this->id = str_replace('.', '', uniqid('', true)); foreach ($this->collectors as $collector) { - if ($this->collectorPolicy?->shouldStartup($collector, $event) === true) { + if ($this->collectorPolicy === null || $this->collectorPolicy->shouldStartup($collector, $event)) { $collector->startup(); } } From 5490f29f2dc237389a30e64ae1e61bf75a950302 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 16 Jan 2025 16:29:03 +0300 Subject: [PATCH 06/25] improve --- config/di-console.php | 6 +++--- config/di-web.php | 6 +++--- src/Debugger.php | 8 ++++---- .../{Condition/OrCondition.php => DebuggerPolicy.php} | 8 +++++--- tests/Unit/Collector/HttpStreamCollectorTest.php | 2 ++ tests/Unit/DebuggerTest.php | 3 ++- 6 files changed, 19 insertions(+), 14 deletions(-) rename src/StartupPolicy/{Condition/OrCondition.php => DebuggerPolicy.php} (71%) diff --git a/config/di-console.php b/config/di-console.php index 270d3632..52ac0f4c 100644 --- a/config/di-console.php +++ b/config/di-console.php @@ -6,8 +6,8 @@ use Yiisoft\Definitions\ReferencesArray; use Yiisoft\Yii\Debug\Debugger; use Yiisoft\Yii\Debug\StartupPolicy\Condition\CommandCondition; -use Yiisoft\Yii\Debug\StartupPolicy\Condition\OrCondition; use Yiisoft\Yii\Debug\StartupPolicy\Condition\EnvironmentVariableCondition; +use Yiisoft\Yii\Debug\StartupPolicy\DebuggerPolicy; if (!(bool)($params['yiisoft/yii-debug']['enabled'] ?? false)) { return []; @@ -22,8 +22,8 @@ $params['yiisoft/yii-debug']['collectors.console'] ?? [] ) ), - 'startupPreventionCondition' => DynamicReference::to( - static fn () => new OrCondition( + 'startupPreventionPolicy' => DynamicReference::to( + static fn () => new DebuggerPolicy( new EnvironmentVariableCondition('YII_DEBUG_IGNORE'), new CommandCondition($params['yiisoft/yii-debug']['ignoredCommands']) ), diff --git a/config/di-web.php b/config/di-web.php index 6d558a75..2eecf778 100644 --- a/config/di-web.php +++ b/config/di-web.php @@ -5,10 +5,10 @@ use Yiisoft\Definitions\DynamicReference; use Yiisoft\Definitions\ReferencesArray; use Yiisoft\Yii\Debug\Debugger; -use Yiisoft\Yii\Debug\StartupPolicy\Condition\OrCondition; use Yiisoft\Yii\Debug\StartupPolicy\Condition\EnvironmentVariableCondition; use Yiisoft\Yii\Debug\StartupPolicy\Condition\HeaderCondition; use Yiisoft\Yii\Debug\StartupPolicy\Condition\RequestCondition; +use Yiisoft\Yii\Debug\StartupPolicy\DebuggerPolicy; if (!(bool)($params['yiisoft/yii-debug']['enabled'] ?? false)) { return []; @@ -23,8 +23,8 @@ $params['yiisoft/yii-debug']['collectors.web'] ?? [], ) ), - 'startupPreventionCondition' => DynamicReference::to( - static fn () => new OrCondition( + 'startupPreventionPolicy' => DynamicReference::to( + static fn () => new DebuggerPolicy( new EnvironmentVariableCondition('YII_DEBUG_IGNORE'), new HeaderCondition('X-Debug-Ignore'), new RequestCondition($params['yiisoft/yii-debug']['ignoredRequests']) diff --git a/src/Debugger.php b/src/Debugger.php index 3301138a..7d752e58 100644 --- a/src/Debugger.php +++ b/src/Debugger.php @@ -8,7 +8,7 @@ use Yiisoft\Yii\Debug\Collector\CollectorInterface; use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface; use Yiisoft\Yii\Debug\StartupPolicy\Collector\CollectorPolicyInterface; -use Yiisoft\Yii\Debug\StartupPolicy\Condition\ConditionInterface; +use Yiisoft\Yii\Debug\StartupPolicy\DebuggerPolicy; use Yiisoft\Yii\Debug\Storage\StorageInterface; /** @@ -33,7 +33,7 @@ final class Debugger public function __construct( private readonly StorageInterface $storage, array $collectors, - private readonly ?ConditionInterface $startupPreventionCondition = null, + private readonly ?DebuggerPolicy $preventionPolicy = null, private readonly ?CollectorPolicyInterface $collectorPolicy = null, array $excludedClasses = [], ) { @@ -60,14 +60,14 @@ public function getId(): string public function startup(object $event): void { - if ($this->startupPreventionCondition?->match($event) === true) { + if ($this->preventionPolicy?->shouldPrevent($event) === true) { return; } $this->id = str_replace('.', '', uniqid('', true)); foreach ($this->collectors as $collector) { - if ($this->collectorPolicy === null || $this->collectorPolicy->shouldStartup($collector, $event)) { + if ($this->collectorPolicy?->shouldStartup($collector, $event) !== false) { $collector->startup(); } } diff --git a/src/StartupPolicy/Condition/OrCondition.php b/src/StartupPolicy/DebuggerPolicy.php similarity index 71% rename from src/StartupPolicy/Condition/OrCondition.php rename to src/StartupPolicy/DebuggerPolicy.php index ef858a7f..87c22be7 100644 --- a/src/StartupPolicy/Condition/OrCondition.php +++ b/src/StartupPolicy/DebuggerPolicy.php @@ -2,9 +2,11 @@ declare(strict_types=1); -namespace Yiisoft\Yii\Debug\StartupPolicy\Condition; +namespace Yiisoft\Yii\Debug\StartupPolicy; -final class OrCondition implements ConditionInterface +use Yiisoft\Yii\Debug\StartupPolicy\Condition\ConditionInterface; + +final class DebuggerPolicy { /** * @psalm-var list @@ -19,7 +21,7 @@ public function __construct(ConditionInterface ...$policies) $this->conditions = $policies; } - public function match(object $event): bool + public function shouldPrevent(object $event): bool { foreach ($this->conditions as $policy) { if ($policy->match($event)) { diff --git a/tests/Unit/Collector/HttpStreamCollectorTest.php b/tests/Unit/Collector/HttpStreamCollectorTest.php index 8664174d..86d15ff5 100644 --- a/tests/Unit/Collector/HttpStreamCollectorTest.php +++ b/tests/Unit/Collector/HttpStreamCollectorTest.php @@ -10,6 +10,8 @@ use Yiisoft\Yii\Debug\Collector\Stream\HttpStreamCollector; use Yiisoft\Yii\Debug\Tests\Shared\AbstractCollectorTestCase; +use function is_array; + final class HttpStreamCollectorTest extends AbstractCollectorTestCase { /** diff --git a/tests/Unit/DebuggerTest.php b/tests/Unit/DebuggerTest.php index ec40192c..59bffad5 100644 --- a/tests/Unit/DebuggerTest.php +++ b/tests/Unit/DebuggerTest.php @@ -10,6 +10,7 @@ use Yiisoft\Yii\Debug\Collector\CollectorInterface; use Yiisoft\Yii\Debug\Debugger; use Yiisoft\Yii\Debug\StartupPolicy\Condition\PredefinedCondition; +use Yiisoft\Yii\Debug\StartupPolicy\DebuggerPolicy; use Yiisoft\Yii\Debug\Storage\MemoryStorage; use Yiisoft\Yii\Debug\Storage\StorageInterface; use Yiisoft\Yii\Http\Event\BeforeRequest; @@ -49,7 +50,7 @@ public function testShutdownWithStartupPrevention(): void $storage = $this->getMockBuilder(StorageInterface::class)->getMock(); $storage->expects($this->never())->method('write'); - $debugger = new Debugger($storage, [$collector], new PredefinedCondition(true)); + $debugger = new Debugger($storage, [$collector], new DebuggerPolicy(new PredefinedCondition(true))); $debugger->startup(new BeforeRequest(new ServerRequest('GET', '/test'))); $debugger->shutdown(); } From 00d8c5346ed9390ee723193d4738c7fbf7e151e6 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 16 Jan 2025 16:38:04 +0300 Subject: [PATCH 07/25] improve --- config/di-console.php | 4 ++-- config/di-web.php | 4 ++-- src/Debugger.php | 6 +++--- .../{DebuggerPolicy.php => StartupPreventionPolicy.php} | 2 +- tests/Unit/DebuggerTest.php | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) rename src/StartupPolicy/{DebuggerPolicy.php => StartupPreventionPolicy.php} (94%) diff --git a/config/di-console.php b/config/di-console.php index 52ac0f4c..26c7d52f 100644 --- a/config/di-console.php +++ b/config/di-console.php @@ -7,7 +7,7 @@ use Yiisoft\Yii\Debug\Debugger; use Yiisoft\Yii\Debug\StartupPolicy\Condition\CommandCondition; use Yiisoft\Yii\Debug\StartupPolicy\Condition\EnvironmentVariableCondition; -use Yiisoft\Yii\Debug\StartupPolicy\DebuggerPolicy; +use Yiisoft\Yii\Debug\StartupPolicy\StartupPreventionPolicy; if (!(bool)($params['yiisoft/yii-debug']['enabled'] ?? false)) { return []; @@ -23,7 +23,7 @@ ) ), 'startupPreventionPolicy' => DynamicReference::to( - static fn () => new DebuggerPolicy( + static fn () => new StartupPreventionPolicy( new EnvironmentVariableCondition('YII_DEBUG_IGNORE'), new CommandCondition($params['yiisoft/yii-debug']['ignoredCommands']) ), diff --git a/config/di-web.php b/config/di-web.php index 2eecf778..d17726bc 100644 --- a/config/di-web.php +++ b/config/di-web.php @@ -8,7 +8,7 @@ use Yiisoft\Yii\Debug\StartupPolicy\Condition\EnvironmentVariableCondition; use Yiisoft\Yii\Debug\StartupPolicy\Condition\HeaderCondition; use Yiisoft\Yii\Debug\StartupPolicy\Condition\RequestCondition; -use Yiisoft\Yii\Debug\StartupPolicy\DebuggerPolicy; +use Yiisoft\Yii\Debug\StartupPolicy\StartupPreventionPolicy; if (!(bool)($params['yiisoft/yii-debug']['enabled'] ?? false)) { return []; @@ -24,7 +24,7 @@ ) ), 'startupPreventionPolicy' => DynamicReference::to( - static fn () => new DebuggerPolicy( + static fn () => new StartupPreventionPolicy( new EnvironmentVariableCondition('YII_DEBUG_IGNORE'), new HeaderCondition('X-Debug-Ignore'), new RequestCondition($params['yiisoft/yii-debug']['ignoredRequests']) diff --git a/src/Debugger.php b/src/Debugger.php index 7d752e58..de848168 100644 --- a/src/Debugger.php +++ b/src/Debugger.php @@ -8,7 +8,7 @@ use Yiisoft\Yii\Debug\Collector\CollectorInterface; use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface; use Yiisoft\Yii\Debug\StartupPolicy\Collector\CollectorPolicyInterface; -use Yiisoft\Yii\Debug\StartupPolicy\DebuggerPolicy; +use Yiisoft\Yii\Debug\StartupPolicy\StartupPreventionPolicy; use Yiisoft\Yii\Debug\Storage\StorageInterface; /** @@ -33,7 +33,7 @@ final class Debugger public function __construct( private readonly StorageInterface $storage, array $collectors, - private readonly ?DebuggerPolicy $preventionPolicy = null, + private readonly ?StartupPreventionPolicy $startupPreventionPolicy = null, private readonly ?CollectorPolicyInterface $collectorPolicy = null, array $excludedClasses = [], ) { @@ -60,7 +60,7 @@ public function getId(): string public function startup(object $event): void { - if ($this->preventionPolicy?->shouldPrevent($event) === true) { + if ($this->startupPreventionPolicy?->shouldPrevent($event) === true) { return; } diff --git a/src/StartupPolicy/DebuggerPolicy.php b/src/StartupPolicy/StartupPreventionPolicy.php similarity index 94% rename from src/StartupPolicy/DebuggerPolicy.php rename to src/StartupPolicy/StartupPreventionPolicy.php index 87c22be7..bf7c5dee 100644 --- a/src/StartupPolicy/DebuggerPolicy.php +++ b/src/StartupPolicy/StartupPreventionPolicy.php @@ -6,7 +6,7 @@ use Yiisoft\Yii\Debug\StartupPolicy\Condition\ConditionInterface; -final class DebuggerPolicy +final class StartupPreventionPolicy { /** * @psalm-var list diff --git a/tests/Unit/DebuggerTest.php b/tests/Unit/DebuggerTest.php index 59bffad5..aea521a6 100644 --- a/tests/Unit/DebuggerTest.php +++ b/tests/Unit/DebuggerTest.php @@ -10,7 +10,7 @@ use Yiisoft\Yii\Debug\Collector\CollectorInterface; use Yiisoft\Yii\Debug\Debugger; use Yiisoft\Yii\Debug\StartupPolicy\Condition\PredefinedCondition; -use Yiisoft\Yii\Debug\StartupPolicy\DebuggerPolicy; +use Yiisoft\Yii\Debug\StartupPolicy\StartupPreventionPolicy; use Yiisoft\Yii\Debug\Storage\MemoryStorage; use Yiisoft\Yii\Debug\Storage\StorageInterface; use Yiisoft\Yii\Http\Event\BeforeRequest; @@ -50,7 +50,7 @@ public function testShutdownWithStartupPrevention(): void $storage = $this->getMockBuilder(StorageInterface::class)->getMock(); $storage->expects($this->never())->method('write'); - $debugger = new Debugger($storage, [$collector], new DebuggerPolicy(new PredefinedCondition(true))); + $debugger = new Debugger($storage, [$collector], new StartupPreventionPolicy(new PredefinedCondition(true))); $debugger->startup(new BeforeRequest(new ServerRequest('GET', '/test'))); $debugger->shutdown(); } From c704b346146ef6669f60e8a70e90145370be0471 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 16 Jan 2025 16:39:45 +0300 Subject: [PATCH 08/25] improve --- config/di-console.php | 6 +++--- config/di-web.php | 4 ++-- src/Debugger.php | 6 +++--- .../{StartupPreventionPolicy.php => StartupPolicy.php} | 2 +- tests/Unit/DebuggerTest.php | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) rename src/StartupPolicy/{StartupPreventionPolicy.php => StartupPolicy.php} (94%) diff --git a/config/di-console.php b/config/di-console.php index 26c7d52f..0973c90f 100644 --- a/config/di-console.php +++ b/config/di-console.php @@ -7,7 +7,7 @@ use Yiisoft\Yii\Debug\Debugger; use Yiisoft\Yii\Debug\StartupPolicy\Condition\CommandCondition; use Yiisoft\Yii\Debug\StartupPolicy\Condition\EnvironmentVariableCondition; -use Yiisoft\Yii\Debug\StartupPolicy\StartupPreventionPolicy; +use Yiisoft\Yii\Debug\StartupPolicy\StartupPolicy; if (!(bool)($params['yiisoft/yii-debug']['enabled'] ?? false)) { return []; @@ -22,8 +22,8 @@ $params['yiisoft/yii-debug']['collectors.console'] ?? [] ) ), - 'startupPreventionPolicy' => DynamicReference::to( - static fn () => new StartupPreventionPolicy( + 'startupPolicy' => DynamicReference::to( + static fn () => new StartupPolicy( new EnvironmentVariableCondition('YII_DEBUG_IGNORE'), new CommandCondition($params['yiisoft/yii-debug']['ignoredCommands']) ), diff --git a/config/di-web.php b/config/di-web.php index d17726bc..55b7e3cc 100644 --- a/config/di-web.php +++ b/config/di-web.php @@ -8,7 +8,7 @@ use Yiisoft\Yii\Debug\StartupPolicy\Condition\EnvironmentVariableCondition; use Yiisoft\Yii\Debug\StartupPolicy\Condition\HeaderCondition; use Yiisoft\Yii\Debug\StartupPolicy\Condition\RequestCondition; -use Yiisoft\Yii\Debug\StartupPolicy\StartupPreventionPolicy; +use Yiisoft\Yii\Debug\StartupPolicy\StartupPolicy; if (!(bool)($params['yiisoft/yii-debug']['enabled'] ?? false)) { return []; @@ -24,7 +24,7 @@ ) ), 'startupPreventionPolicy' => DynamicReference::to( - static fn () => new StartupPreventionPolicy( + static fn () => new StartupPolicy( new EnvironmentVariableCondition('YII_DEBUG_IGNORE'), new HeaderCondition('X-Debug-Ignore'), new RequestCondition($params['yiisoft/yii-debug']['ignoredRequests']) diff --git a/src/Debugger.php b/src/Debugger.php index de848168..1b70ea04 100644 --- a/src/Debugger.php +++ b/src/Debugger.php @@ -8,7 +8,7 @@ use Yiisoft\Yii\Debug\Collector\CollectorInterface; use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface; use Yiisoft\Yii\Debug\StartupPolicy\Collector\CollectorPolicyInterface; -use Yiisoft\Yii\Debug\StartupPolicy\StartupPreventionPolicy; +use Yiisoft\Yii\Debug\StartupPolicy\StartupPolicy; use Yiisoft\Yii\Debug\Storage\StorageInterface; /** @@ -33,7 +33,7 @@ final class Debugger public function __construct( private readonly StorageInterface $storage, array $collectors, - private readonly ?StartupPreventionPolicy $startupPreventionPolicy = null, + private readonly ?StartupPolicy $startupPolicy = null, private readonly ?CollectorPolicyInterface $collectorPolicy = null, array $excludedClasses = [], ) { @@ -60,7 +60,7 @@ public function getId(): string public function startup(object $event): void { - if ($this->startupPreventionPolicy?->shouldPrevent($event) === true) { + if ($this->startupPolicy?->shouldPrevent($event) === true) { return; } diff --git a/src/StartupPolicy/StartupPreventionPolicy.php b/src/StartupPolicy/StartupPolicy.php similarity index 94% rename from src/StartupPolicy/StartupPreventionPolicy.php rename to src/StartupPolicy/StartupPolicy.php index bf7c5dee..e06943d1 100644 --- a/src/StartupPolicy/StartupPreventionPolicy.php +++ b/src/StartupPolicy/StartupPolicy.php @@ -6,7 +6,7 @@ use Yiisoft\Yii\Debug\StartupPolicy\Condition\ConditionInterface; -final class StartupPreventionPolicy +final class StartupPolicy { /** * @psalm-var list diff --git a/tests/Unit/DebuggerTest.php b/tests/Unit/DebuggerTest.php index aea521a6..b0d99142 100644 --- a/tests/Unit/DebuggerTest.php +++ b/tests/Unit/DebuggerTest.php @@ -10,7 +10,7 @@ use Yiisoft\Yii\Debug\Collector\CollectorInterface; use Yiisoft\Yii\Debug\Debugger; use Yiisoft\Yii\Debug\StartupPolicy\Condition\PredefinedCondition; -use Yiisoft\Yii\Debug\StartupPolicy\StartupPreventionPolicy; +use Yiisoft\Yii\Debug\StartupPolicy\StartupPolicy; use Yiisoft\Yii\Debug\Storage\MemoryStorage; use Yiisoft\Yii\Debug\Storage\StorageInterface; use Yiisoft\Yii\Http\Event\BeforeRequest; @@ -50,7 +50,7 @@ public function testShutdownWithStartupPrevention(): void $storage = $this->getMockBuilder(StorageInterface::class)->getMock(); $storage->expects($this->never())->method('write'); - $debugger = new Debugger($storage, [$collector], new StartupPreventionPolicy(new PredefinedCondition(true))); + $debugger = new Debugger($storage, [$collector], new StartupPolicy(new PredefinedCondition(true))); $debugger->startup(new BeforeRequest(new ServerRequest('GET', '/test'))); $debugger->shutdown(); } From de4af90a7dfdc82f4c695bb0ccb966de62eeaf5e Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 16 Jan 2025 16:40:38 +0300 Subject: [PATCH 09/25] fix --- config/di-web.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/di-web.php b/config/di-web.php index 55b7e3cc..d16e4eda 100644 --- a/config/di-web.php +++ b/config/di-web.php @@ -23,7 +23,7 @@ $params['yiisoft/yii-debug']['collectors.web'] ?? [], ) ), - 'startupPreventionPolicy' => DynamicReference::to( + 'startupPolicy' => DynamicReference::to( static fn () => new StartupPolicy( new EnvironmentVariableCondition('YII_DEBUG_IGNORE'), new HeaderCondition('X-Debug-Ignore'), From a828a1897782a2cca1ceca68f44b624ac450603f Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 16 Jan 2025 17:20:12 +0300 Subject: [PATCH 10/25] rename --- config/di-console.php | 4 ++-- config/di-web.php | 4 ++-- .../{CommandCondition.php => CommandNameCondition.php} | 10 +++++----- .../{RequestCondition.php => UriPathsCondition.php} | 6 +++--- 4 files changed, 12 insertions(+), 12 deletions(-) rename src/StartupPolicy/Condition/{CommandCondition.php => CommandNameCondition.php} (66%) rename src/StartupPolicy/Condition/{RequestCondition.php => UriPathsCondition.php} (81%) diff --git a/config/di-console.php b/config/di-console.php index 0973c90f..dc01c77d 100644 --- a/config/di-console.php +++ b/config/di-console.php @@ -5,7 +5,7 @@ use Yiisoft\Definitions\DynamicReference; use Yiisoft\Definitions\ReferencesArray; use Yiisoft\Yii\Debug\Debugger; -use Yiisoft\Yii\Debug\StartupPolicy\Condition\CommandCondition; +use Yiisoft\Yii\Debug\StartupPolicy\Condition\CommandNameCondition; use Yiisoft\Yii\Debug\StartupPolicy\Condition\EnvironmentVariableCondition; use Yiisoft\Yii\Debug\StartupPolicy\StartupPolicy; @@ -25,7 +25,7 @@ 'startupPolicy' => DynamicReference::to( static fn () => new StartupPolicy( new EnvironmentVariableCondition('YII_DEBUG_IGNORE'), - new CommandCondition($params['yiisoft/yii-debug']['ignoredCommands']) + new CommandNameCondition($params['yiisoft/yii-debug']['ignoredCommands']) ), ), 'excludedClasses' => $params['yiisoft/yii-debug']['excludedClasses'], diff --git a/config/di-web.php b/config/di-web.php index d16e4eda..783a8d55 100644 --- a/config/di-web.php +++ b/config/di-web.php @@ -7,7 +7,7 @@ use Yiisoft\Yii\Debug\Debugger; use Yiisoft\Yii\Debug\StartupPolicy\Condition\EnvironmentVariableCondition; use Yiisoft\Yii\Debug\StartupPolicy\Condition\HeaderCondition; -use Yiisoft\Yii\Debug\StartupPolicy\Condition\RequestCondition; +use Yiisoft\Yii\Debug\StartupPolicy\Condition\UriPathsCondition; use Yiisoft\Yii\Debug\StartupPolicy\StartupPolicy; if (!(bool)($params['yiisoft/yii-debug']['enabled'] ?? false)) { @@ -27,7 +27,7 @@ static fn () => new StartupPolicy( new EnvironmentVariableCondition('YII_DEBUG_IGNORE'), new HeaderCondition('X-Debug-Ignore'), - new RequestCondition($params['yiisoft/yii-debug']['ignoredRequests']) + new UriPathsCondition($params['yiisoft/yii-debug']['ignoredRequests']) ), ), 'excludedClasses' => $params['yiisoft/yii-debug']['excludedClasses'], diff --git a/src/StartupPolicy/Condition/CommandCondition.php b/src/StartupPolicy/Condition/CommandNameCondition.php similarity index 66% rename from src/StartupPolicy/Condition/CommandCondition.php rename to src/StartupPolicy/Condition/CommandNameCondition.php index 3f53e8c8..2f6188bb 100644 --- a/src/StartupPolicy/Condition/CommandCondition.php +++ b/src/StartupPolicy/Condition/CommandNameCondition.php @@ -7,14 +7,14 @@ use Yiisoft\Strings\WildcardPattern; use Yiisoft\Yii\Console\Event\ApplicationStartup; -final class CommandCondition implements ConditionInterface +final class CommandNameCondition implements ConditionInterface { public function __construct( /** * @var string[] * @psalm-var list */ - private readonly array $commands, + private readonly array $names, ) { } @@ -24,10 +24,10 @@ public function match(object $event): bool return false; } - $command = (string) $event->commandName; + $name = (string) $event->commandName; - foreach ($this->commands as $pattern) { - if ((new WildcardPattern($pattern))->match($command)) { + foreach ($this->names as $pattern) { + if ((new WildcardPattern($pattern))->match($name)) { return true; } } diff --git a/src/StartupPolicy/Condition/RequestCondition.php b/src/StartupPolicy/Condition/UriPathsCondition.php similarity index 81% rename from src/StartupPolicy/Condition/RequestCondition.php rename to src/StartupPolicy/Condition/UriPathsCondition.php index 0e413c09..97e0a6e5 100644 --- a/src/StartupPolicy/Condition/RequestCondition.php +++ b/src/StartupPolicy/Condition/UriPathsCondition.php @@ -7,14 +7,14 @@ use Yiisoft\Strings\WildcardPattern; use Yiisoft\Yii\Http\Event\BeforeRequest; -final class RequestCondition implements ConditionInterface +final class UriPathsCondition implements ConditionInterface { public function __construct( /** * @var string[] * @psalm-var list */ - private readonly array $uriPaths, + private readonly array $paths, ) { } @@ -26,7 +26,7 @@ public function match(object $event): bool $path = $event->getRequest()->getUri()->getPath(); - foreach ($this->uriPaths as $pattern) { + foreach ($this->paths as $pattern) { if ((new WildcardPattern($pattern))->match($path)) { return true; } From ca4c5e12c1992029815e7f30d7ca455870267361 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 16 Jan 2025 17:40:03 +0300 Subject: [PATCH 11/25] improve --- config/di-console.php | 6 ++-- config/di-web.php | 6 ++-- src/Debugger.php | 11 +++--- .../AllowDebuggerPolicy.php} | 6 ++-- .../Debugger/CallableDebuggerPolicy.php | 33 ++++++++++++++++++ .../DebuggerStartupPolicyInterface.php | 10 ++++++ .../Debugger/DenyDebuggerPolicy.php | 34 +++++++++++++++++++ tests/Unit/DebuggerTest.php | 5 ++- 8 files changed, 94 insertions(+), 17 deletions(-) rename src/StartupPolicy/{StartupPolicy.php => Debugger/AllowDebuggerPolicy.php} (75%) create mode 100644 src/StartupPolicy/Debugger/CallableDebuggerPolicy.php create mode 100644 src/StartupPolicy/Debugger/DebuggerStartupPolicyInterface.php create mode 100644 src/StartupPolicy/Debugger/DenyDebuggerPolicy.php diff --git a/config/di-console.php b/config/di-console.php index dc01c77d..0c3daaa6 100644 --- a/config/di-console.php +++ b/config/di-console.php @@ -7,7 +7,7 @@ use Yiisoft\Yii\Debug\Debugger; use Yiisoft\Yii\Debug\StartupPolicy\Condition\CommandNameCondition; use Yiisoft\Yii\Debug\StartupPolicy\Condition\EnvironmentVariableCondition; -use Yiisoft\Yii\Debug\StartupPolicy\StartupPolicy; +use Yiisoft\Yii\Debug\StartupPolicy\Debugger\DenyDebuggerPolicy; if (!(bool)($params['yiisoft/yii-debug']['enabled'] ?? false)) { return []; @@ -22,8 +22,8 @@ $params['yiisoft/yii-debug']['collectors.console'] ?? [] ) ), - 'startupPolicy' => DynamicReference::to( - static fn () => new StartupPolicy( + 'debuggerStartupPolicy' => DynamicReference::to( + static fn () => new DenyDebuggerPolicy( new EnvironmentVariableCondition('YII_DEBUG_IGNORE'), new CommandNameCondition($params['yiisoft/yii-debug']['ignoredCommands']) ), diff --git a/config/di-web.php b/config/di-web.php index 783a8d55..22e4205b 100644 --- a/config/di-web.php +++ b/config/di-web.php @@ -8,7 +8,7 @@ use Yiisoft\Yii\Debug\StartupPolicy\Condition\EnvironmentVariableCondition; use Yiisoft\Yii\Debug\StartupPolicy\Condition\HeaderCondition; use Yiisoft\Yii\Debug\StartupPolicy\Condition\UriPathsCondition; -use Yiisoft\Yii\Debug\StartupPolicy\StartupPolicy; +use Yiisoft\Yii\Debug\StartupPolicy\Debugger\DenyDebuggerPolicy; if (!(bool)($params['yiisoft/yii-debug']['enabled'] ?? false)) { return []; @@ -23,8 +23,8 @@ $params['yiisoft/yii-debug']['collectors.web'] ?? [], ) ), - 'startupPolicy' => DynamicReference::to( - static fn () => new StartupPolicy( + 'debuggerStartupPolicy' => DynamicReference::to( + static fn () => new DenyDebuggerPolicy( new EnvironmentVariableCondition('YII_DEBUG_IGNORE'), new HeaderCondition('X-Debug-Ignore'), new UriPathsCondition($params['yiisoft/yii-debug']['ignoredRequests']) diff --git a/src/Debugger.php b/src/Debugger.php index 1b70ea04..bb2a671d 100644 --- a/src/Debugger.php +++ b/src/Debugger.php @@ -8,7 +8,7 @@ use Yiisoft\Yii\Debug\Collector\CollectorInterface; use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface; use Yiisoft\Yii\Debug\StartupPolicy\Collector\CollectorPolicyInterface; -use Yiisoft\Yii\Debug\StartupPolicy\StartupPolicy; +use Yiisoft\Yii\Debug\StartupPolicy\Debugger\DebuggerStartupPolicyInterface; use Yiisoft\Yii\Debug\Storage\StorageInterface; /** @@ -33,7 +33,7 @@ final class Debugger public function __construct( private readonly StorageInterface $storage, array $collectors, - private readonly ?StartupPolicy $startupPolicy = null, + private readonly ?DebuggerStartupPolicyInterface $debuggerStartupPolicy = null, private readonly ?CollectorPolicyInterface $collectorPolicy = null, array $excludedClasses = [], ) { @@ -60,16 +60,17 @@ public function getId(): string public function startup(object $event): void { - if ($this->startupPolicy?->shouldPrevent($event) === true) { + if ($this->debuggerStartupPolicy?->satisfies($event) === false) { return; } $this->id = str_replace('.', '', uniqid('', true)); foreach ($this->collectors as $collector) { - if ($this->collectorPolicy?->shouldStartup($collector, $event) !== false) { - $collector->startup(); + if ($this->collectorPolicy?->shouldStartup($collector, $event) === false) { + continue; } + $collector->startup(); } } diff --git a/src/StartupPolicy/StartupPolicy.php b/src/StartupPolicy/Debugger/AllowDebuggerPolicy.php similarity index 75% rename from src/StartupPolicy/StartupPolicy.php rename to src/StartupPolicy/Debugger/AllowDebuggerPolicy.php index e06943d1..e22001bc 100644 --- a/src/StartupPolicy/StartupPolicy.php +++ b/src/StartupPolicy/Debugger/AllowDebuggerPolicy.php @@ -2,11 +2,11 @@ declare(strict_types=1); -namespace Yiisoft\Yii\Debug\StartupPolicy; +namespace Yiisoft\Yii\Debug\StartupPolicy\Debugger; use Yiisoft\Yii\Debug\StartupPolicy\Condition\ConditionInterface; -final class StartupPolicy +final class AllowDebuggerPolicy implements DebuggerStartupPolicyInterface { /** * @psalm-var list @@ -21,7 +21,7 @@ public function __construct(ConditionInterface ...$policies) $this->conditions = $policies; } - public function shouldPrevent(object $event): bool + public function satisfies(object $event): bool { foreach ($this->conditions as $policy) { if ($policy->match($event)) { diff --git a/src/StartupPolicy/Debugger/CallableDebuggerPolicy.php b/src/StartupPolicy/Debugger/CallableDebuggerPolicy.php new file mode 100644 index 00000000..f4d65c5a --- /dev/null +++ b/src/StartupPolicy/Debugger/CallableDebuggerPolicy.php @@ -0,0 +1,33 @@ +callable = $callable; + } + + public function satisfies(object $event): bool + { + return call_user_func($this->callable, $event); + } +} diff --git a/src/StartupPolicy/Debugger/DebuggerStartupPolicyInterface.php b/src/StartupPolicy/Debugger/DebuggerStartupPolicyInterface.php new file mode 100644 index 00000000..81c62313 --- /dev/null +++ b/src/StartupPolicy/Debugger/DebuggerStartupPolicyInterface.php @@ -0,0 +1,10 @@ + + */ + private readonly array $conditions; + + /** + * @no-named-arguments + */ + public function __construct(ConditionInterface ...$policies) + { + $this->conditions = $policies; + } + + public function satisfies(object $event): bool + { + foreach ($this->conditions as $policy) { + if ($policy->match($event)) { + return false; + } + } + + return true; + } +} diff --git a/tests/Unit/DebuggerTest.php b/tests/Unit/DebuggerTest.php index b0d99142..d0ee1c3e 100644 --- a/tests/Unit/DebuggerTest.php +++ b/tests/Unit/DebuggerTest.php @@ -9,8 +9,7 @@ use stdClass; use Yiisoft\Yii\Debug\Collector\CollectorInterface; use Yiisoft\Yii\Debug\Debugger; -use Yiisoft\Yii\Debug\StartupPolicy\Condition\PredefinedCondition; -use Yiisoft\Yii\Debug\StartupPolicy\StartupPolicy; +use Yiisoft\Yii\Debug\StartupPolicy\Debugger\AllowDebuggerPolicy; use Yiisoft\Yii\Debug\Storage\MemoryStorage; use Yiisoft\Yii\Debug\Storage\StorageInterface; use Yiisoft\Yii\Http\Event\BeforeRequest; @@ -50,7 +49,7 @@ public function testShutdownWithStartupPrevention(): void $storage = $this->getMockBuilder(StorageInterface::class)->getMock(); $storage->expects($this->never())->method('write'); - $debugger = new Debugger($storage, [$collector], new StartupPolicy(new PredefinedCondition(true))); + $debugger = new Debugger($storage, [$collector], new AllowDebuggerPolicy()); $debugger->startup(new BeforeRequest(new ServerRequest('GET', '/test'))); $debugger->shutdown(); } From 31f70b75be5c0a74fbdd99e81617310f54293eb9 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 16 Jan 2025 17:43:27 +0300 Subject: [PATCH 12/25] improve --- src/Debugger.php | 6 ++-- .../Collector/BlackListCollectorPolicy.php | 4 +-- .../Collector/CallableCollectorPolicy.php | 35 +++++++++++++++++++ ...hp => CollectorStartupPolicyInterface.php} | 4 +-- .../Collector/WhiteListCollectorPolicy.php | 4 +-- 5 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 src/StartupPolicy/Collector/CallableCollectorPolicy.php rename src/StartupPolicy/Collector/{CollectorPolicyInterface.php => CollectorStartupPolicyInterface.php} (53%) diff --git a/src/Debugger.php b/src/Debugger.php index bb2a671d..0d5809b3 100644 --- a/src/Debugger.php +++ b/src/Debugger.php @@ -7,7 +7,7 @@ use LogicException; use Yiisoft\Yii\Debug\Collector\CollectorInterface; use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface; -use Yiisoft\Yii\Debug\StartupPolicy\Collector\CollectorPolicyInterface; +use Yiisoft\Yii\Debug\StartupPolicy\Collector\CollectorStartupPolicyInterface; use Yiisoft\Yii\Debug\StartupPolicy\Debugger\DebuggerStartupPolicyInterface; use Yiisoft\Yii\Debug\Storage\StorageInterface; @@ -34,7 +34,7 @@ public function __construct( private readonly StorageInterface $storage, array $collectors, private readonly ?DebuggerStartupPolicyInterface $debuggerStartupPolicy = null, - private readonly ?CollectorPolicyInterface $collectorPolicy = null, + private readonly ?CollectorStartupPolicyInterface $collectorStartupPolicy = null, array $excludedClasses = [], ) { $preparedCollectors = []; @@ -67,7 +67,7 @@ public function startup(object $event): void $this->id = str_replace('.', '', uniqid('', true)); foreach ($this->collectors as $collector) { - if ($this->collectorPolicy?->shouldStartup($collector, $event) === false) { + if ($this->collectorStartupPolicy?->satisfies($collector, $event) === false) { continue; } $collector->startup(); diff --git a/src/StartupPolicy/Collector/BlackListCollectorPolicy.php b/src/StartupPolicy/Collector/BlackListCollectorPolicy.php index 948f2b34..4d556c13 100644 --- a/src/StartupPolicy/Collector/BlackListCollectorPolicy.php +++ b/src/StartupPolicy/Collector/BlackListCollectorPolicy.php @@ -7,7 +7,7 @@ use Yiisoft\Yii\Debug\Collector\CollectorInterface; use Yiisoft\Yii\Debug\StartupPolicy\Condition\ConditionInterface; -final class BlackListCollectorPolicy implements CollectorPolicyInterface +final class BlackListCollectorPolicy implements CollectorStartupPolicyInterface { public function __construct( /** @@ -18,7 +18,7 @@ public function __construct( ) { } - public function shouldStartup(CollectorInterface $collector, object $event): bool + public function satisfies(CollectorInterface $collector, object $event): bool { $condition = $this->conditions[$collector->getName()] ?? null; if ($condition === null) { diff --git a/src/StartupPolicy/Collector/CallableCollectorPolicy.php b/src/StartupPolicy/Collector/CallableCollectorPolicy.php new file mode 100644 index 00000000..26c2884d --- /dev/null +++ b/src/StartupPolicy/Collector/CallableCollectorPolicy.php @@ -0,0 +1,35 @@ +callable = $callable; + } + + public function satisfies(CollectorInterface $collector, object $event): bool + { + return call_user_func($this->callable, $collector, $event); + } +} diff --git a/src/StartupPolicy/Collector/CollectorPolicyInterface.php b/src/StartupPolicy/Collector/CollectorStartupPolicyInterface.php similarity index 53% rename from src/StartupPolicy/Collector/CollectorPolicyInterface.php rename to src/StartupPolicy/Collector/CollectorStartupPolicyInterface.php index e404405d..4f3374ec 100644 --- a/src/StartupPolicy/Collector/CollectorPolicyInterface.php +++ b/src/StartupPolicy/Collector/CollectorStartupPolicyInterface.php @@ -6,7 +6,7 @@ use Yiisoft\Yii\Debug\Collector\CollectorInterface; -interface CollectorPolicyInterface +interface CollectorStartupPolicyInterface { - public function shouldStartup(CollectorInterface $collector, object $event): bool; + public function satisfies(CollectorInterface $collector, object $event): bool; } diff --git a/src/StartupPolicy/Collector/WhiteListCollectorPolicy.php b/src/StartupPolicy/Collector/WhiteListCollectorPolicy.php index 450e2e54..ec725121 100644 --- a/src/StartupPolicy/Collector/WhiteListCollectorPolicy.php +++ b/src/StartupPolicy/Collector/WhiteListCollectorPolicy.php @@ -7,7 +7,7 @@ use Yiisoft\Yii\Debug\Collector\CollectorInterface; use Yiisoft\Yii\Debug\StartupPolicy\Condition\ConditionInterface; -final class WhiteListCollectorPolicy implements CollectorPolicyInterface +final class WhiteListCollectorPolicy implements CollectorStartupPolicyInterface { public function __construct( /** @@ -18,7 +18,7 @@ public function __construct( ) { } - public function shouldStartup(CollectorInterface $collector, object $event): bool + public function satisfies(CollectorInterface $collector, object $event): bool { $condition = $this->conditions[$collector->getName()] ?? null; if ($condition === null) { From 1159a75161a2f21c156d26a1e398d024cf729c96 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 16 Jan 2025 14:43:39 +0000 Subject: [PATCH 13/25] Apply fixes from StyleCI --- src/StartupPolicy/Collector/CallableCollectorPolicy.php | 4 +--- src/StartupPolicy/Debugger/CallableDebuggerPolicy.php | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/StartupPolicy/Collector/CallableCollectorPolicy.php b/src/StartupPolicy/Collector/CallableCollectorPolicy.php index 26c2884d..ff438e56 100644 --- a/src/StartupPolicy/Collector/CallableCollectorPolicy.php +++ b/src/StartupPolicy/Collector/CallableCollectorPolicy.php @@ -6,8 +6,6 @@ use Yiisoft\Yii\Debug\Collector\CollectorInterface; -use function call_user_func; - /** * @psalm-type TCallable = callable(CollectorInterface, object): bool */ @@ -30,6 +28,6 @@ public function __construct( public function satisfies(CollectorInterface $collector, object $event): bool { - return call_user_func($this->callable, $collector, $event); + return ($this->callable)($collector, $event); } } diff --git a/src/StartupPolicy/Debugger/CallableDebuggerPolicy.php b/src/StartupPolicy/Debugger/CallableDebuggerPolicy.php index f4d65c5a..47ed02c6 100644 --- a/src/StartupPolicy/Debugger/CallableDebuggerPolicy.php +++ b/src/StartupPolicy/Debugger/CallableDebuggerPolicy.php @@ -4,8 +4,6 @@ namespace Yiisoft\Yii\Debug\StartupPolicy\Debugger; -use function call_user_func; - /** * @psalm-type TCallable = callable(object): bool */ @@ -28,6 +26,6 @@ public function __construct( public function satisfies(object $event): bool { - return call_user_func($this->callable, $event); + return ($this->callable)($event); } } From 557e78d861ccac84ef07e08d733158a2036aa51c Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 16 Jan 2025 22:04:33 +0300 Subject: [PATCH 14/25] Add `PredefinedConditionTest` --- .../Condition/PredefinedConditionTest.php | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 tests/Unit/StartupPolicy/Condition/PredefinedConditionTest.php diff --git a/tests/Unit/StartupPolicy/Condition/PredefinedConditionTest.php b/tests/Unit/StartupPolicy/Condition/PredefinedConditionTest.php new file mode 100644 index 00000000..7a4b791f --- /dev/null +++ b/tests/Unit/StartupPolicy/Condition/PredefinedConditionTest.php @@ -0,0 +1,28 @@ + [true]; + yield 'false' => [false]; + } + + #[DataProvider('dataBase')] + public function testBase(bool $value): void + { + $event = new stdClass(); + $condition = new PredefinedCondition($value); + + $this->assertSame($value, $condition->match($event)); + } +} From e638d42a42742be0fcb7de7ab5d78e02d197536c Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 16 Jan 2025 22:13:26 +0300 Subject: [PATCH 15/25] Add `EnvironmentVariableCondition` --- .../EnvironmentVariableCondition.php | 12 +++++- .../EnvironmentVariableConditionTest.php | 37 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/StartupPolicy/Condition/EnvironmentVariableConditionTest.php diff --git a/src/StartupPolicy/Condition/EnvironmentVariableCondition.php b/src/StartupPolicy/Condition/EnvironmentVariableCondition.php index 063a6f9b..1f82fc69 100644 --- a/src/StartupPolicy/Condition/EnvironmentVariableCondition.php +++ b/src/StartupPolicy/Condition/EnvironmentVariableCondition.php @@ -4,8 +4,13 @@ namespace Yiisoft\Yii\Debug\StartupPolicy\Condition; +use function in_array; +use function is_string; + final class EnvironmentVariableCondition implements ConditionInterface { + private const TRUE_VALUES = ['1', 'true', 'on']; + public function __construct( private readonly string $variableName, ) { @@ -13,6 +18,11 @@ public function __construct( public function match(object $event): bool { - return (bool) getenv($this->variableName); + $value = getenv($this->variableName); + if (!is_string($value)) { + return false; + } + + return in_array(strtolower($value), self::TRUE_VALUES, true); } } diff --git a/tests/Unit/StartupPolicy/Condition/EnvironmentVariableConditionTest.php b/tests/Unit/StartupPolicy/Condition/EnvironmentVariableConditionTest.php new file mode 100644 index 00000000..1a670132 --- /dev/null +++ b/tests/Unit/StartupPolicy/Condition/EnvironmentVariableConditionTest.php @@ -0,0 +1,37 @@ + [true, 'true']; + yield 'false' => [false, 'false']; + yield 'on' => [true, 'on']; + yield 'off' => [false, 'off']; + yield 'one' => [true, '1']; + yield 'zero' => [false, '0']; + yield 'empty-string' => [false, '']; + yield 'custom-string' => [false, 'test']; + } + + #[DataProvider('dataBase')] + public function testBase(bool $expected, string $value): void + { + $variableName = 'YII_DEBUG_TEST_ENVIRONMENT_VARIABLE'; + $event = new stdClass(); + $condition = new EnvironmentVariableCondition($variableName); + + putenv("$variableName=$value"); + + $this->assertSame($expected, $condition->match($event)); + } +} From c3da577ea50bc553d950bc13c55ae8bff062cbe7 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 16 Jan 2025 22:17:28 +0300 Subject: [PATCH 16/25] improve --- .../Condition/EnvironmentVariableCondition.php | 7 +------ .../Condition/EnvironmentVariableConditionTest.php | 8 ++++++++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/StartupPolicy/Condition/EnvironmentVariableCondition.php b/src/StartupPolicy/Condition/EnvironmentVariableCondition.php index 1f82fc69..696963a9 100644 --- a/src/StartupPolicy/Condition/EnvironmentVariableCondition.php +++ b/src/StartupPolicy/Condition/EnvironmentVariableCondition.php @@ -5,7 +5,6 @@ namespace Yiisoft\Yii\Debug\StartupPolicy\Condition; use function in_array; -use function is_string; final class EnvironmentVariableCondition implements ConditionInterface { @@ -19,10 +18,6 @@ public function __construct( public function match(object $event): bool { $value = getenv($this->variableName); - if (!is_string($value)) { - return false; - } - - return in_array(strtolower($value), self::TRUE_VALUES, true); + return $value !== false && in_array(strtolower($value), self::TRUE_VALUES, true); } } diff --git a/tests/Unit/StartupPolicy/Condition/EnvironmentVariableConditionTest.php b/tests/Unit/StartupPolicy/Condition/EnvironmentVariableConditionTest.php index 1a670132..ec7fd1b4 100644 --- a/tests/Unit/StartupPolicy/Condition/EnvironmentVariableConditionTest.php +++ b/tests/Unit/StartupPolicy/Condition/EnvironmentVariableConditionTest.php @@ -34,4 +34,12 @@ public function testBase(bool $expected, string $value): void $this->assertSame($expected, $condition->match($event)); } + + public function testNonExistVariable(): void + { + $event = new stdClass(); + $condition = new EnvironmentVariableCondition('YII_NON_EXIST_VARIABLE'); + + $this->assertFalse($condition->match($event)); + } } From 6b02ae7f1f5fcca6faaa62ff5653d580a50e4fbb Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Fri, 17 Jan 2025 10:22:55 +0300 Subject: [PATCH 17/25] improve test --- .../Condition/EnvironmentVariableConditionTest.php | 6 +++++- .../StartupPolicy/Condition/PredefinedConditionTest.php | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/Unit/StartupPolicy/Condition/EnvironmentVariableConditionTest.php b/tests/Unit/StartupPolicy/Condition/EnvironmentVariableConditionTest.php index ec7fd1b4..5b6d34f0 100644 --- a/tests/Unit/StartupPolicy/Condition/EnvironmentVariableConditionTest.php +++ b/tests/Unit/StartupPolicy/Condition/EnvironmentVariableConditionTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Unit\StartupPolicy\Condition; +namespace Yiisoft\Yii\Debug\Tests\Unit\StartupPolicy\Condition; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; @@ -17,6 +17,10 @@ public static function dataBase(): iterable yield 'false' => [false, 'false']; yield 'on' => [true, 'on']; yield 'off' => [false, 'off']; + yield 'TRUE' => [true, 'TRUE']; + yield 'FALSE' => [false, 'FALSE']; + yield 'ON' => [true, 'ON']; + yield 'OFF' => [false, 'OFF']; yield 'one' => [true, '1']; yield 'zero' => [false, '0']; yield 'empty-string' => [false, '']; diff --git a/tests/Unit/StartupPolicy/Condition/PredefinedConditionTest.php b/tests/Unit/StartupPolicy/Condition/PredefinedConditionTest.php index 7a4b791f..16d059cd 100644 --- a/tests/Unit/StartupPolicy/Condition/PredefinedConditionTest.php +++ b/tests/Unit/StartupPolicy/Condition/PredefinedConditionTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Unit\StartupPolicy\Condition; +namespace Yiisoft\Yii\Debug\Tests\Unit\StartupPolicy\Condition; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; From 828594a11f0d854dfdba83366b7ddad99c0ad2cf Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Fri, 17 Jan 2025 11:10:06 +0300 Subject: [PATCH 18/25] Add `CommandNameConditionTest` --- .../Condition/CommandNameCondition.php | 4 +-- .../Condition/CommandNameConditionTest.php | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 tests/Unit/StartupPolicy/Condition/CommandNameConditionTest.php diff --git a/src/StartupPolicy/Condition/CommandNameCondition.php b/src/StartupPolicy/Condition/CommandNameCondition.php index 2f6188bb..780c5bd7 100644 --- a/src/StartupPolicy/Condition/CommandNameCondition.php +++ b/src/StartupPolicy/Condition/CommandNameCondition.php @@ -12,7 +12,7 @@ final class CommandNameCondition implements ConditionInterface public function __construct( /** * @var string[] - * @psalm-var list + * @psalm-var list */ private readonly array $names, ) { @@ -27,7 +27,7 @@ public function match(object $event): bool $name = (string) $event->commandName; foreach ($this->names as $pattern) { - if ((new WildcardPattern($pattern))->match($name)) { + if ((new WildcardPattern($pattern, [':']))->match($name)) { return true; } } diff --git a/tests/Unit/StartupPolicy/Condition/CommandNameConditionTest.php b/tests/Unit/StartupPolicy/Condition/CommandNameConditionTest.php new file mode 100644 index 00000000..0a66a2cc --- /dev/null +++ b/tests/Unit/StartupPolicy/Condition/CommandNameConditionTest.php @@ -0,0 +1,33 @@ + [false, ['test:run', 'test:stop'], null]; + yield 'with-empty-name' => [false, ['test:run', 'test:stop'], '']; + yield 'not-match' => [false, ['test:run', 'test:stop'], 'email:send']; + yield 'match' => [true, ['test:run', 'test:stop'], 'test:stop']; + yield 'match-wildcard-1' => [true, ['test:app:*'], 'test:app:stop']; + yield 'match-wildcard-2' => [false, ['test:*'], 'test:app:stop']; + yield 'match-wildcard-3' => [true, ['test:**'], 'test:app:stop']; + } + + #[DataProvider('dataBase')] + public function testBase(bool $expected, array $names, ?string $currentName): void + { + $event = new ApplicationStartup($currentName); + $condition = new CommandNameCondition($names); + + $this->assertSame($expected, $condition->match($event)); + } +} From fe1de858c1ec6b899ee45372139d7426d81ff6dd Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Fri, 17 Jan 2025 11:19:31 +0300 Subject: [PATCH 19/25] Add `HeaderConditionTest` --- .../Condition/HeaderCondition.php | 11 ++++- .../Condition/HeaderConditionTest.php | 48 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/StartupPolicy/Condition/HeaderConditionTest.php diff --git a/src/StartupPolicy/Condition/HeaderCondition.php b/src/StartupPolicy/Condition/HeaderCondition.php index 2eef8500..e960f969 100644 --- a/src/StartupPolicy/Condition/HeaderCondition.php +++ b/src/StartupPolicy/Condition/HeaderCondition.php @@ -6,9 +6,17 @@ use Yiisoft\Yii\Http\Event\BeforeRequest; +use function in_array; +use function strtolower; + final class HeaderCondition implements ConditionInterface { + private const TRUE_VALUES = ['1', 'true', 'on']; + public function __construct( + /** + * @psalm-var non-empty-string + */ private readonly string $headerName, ) { } @@ -21,6 +29,7 @@ public function match(object $event): bool $request = $event->getRequest(); - return $request->hasHeader($this->headerName) && $request->getHeaderLine($this->headerName); + return $request->hasHeader($this->headerName) + && in_array(strtolower($request->getHeaderLine($this->headerName)), self::TRUE_VALUES, true); } } diff --git a/tests/Unit/StartupPolicy/Condition/HeaderConditionTest.php b/tests/Unit/StartupPolicy/Condition/HeaderConditionTest.php new file mode 100644 index 00000000..0999c009 --- /dev/null +++ b/tests/Unit/StartupPolicy/Condition/HeaderConditionTest.php @@ -0,0 +1,48 @@ + [true, 'true']; + yield 'false' => [false, 'false']; + yield 'on' => [true, 'on']; + yield 'off' => [false, 'off']; + yield 'TRUE' => [true, 'TRUE']; + yield 'FALSE' => [false, 'FALSE']; + yield 'ON' => [true, 'ON']; + yield 'OFF' => [false, 'OFF']; + yield 'one' => [true, '1']; + yield 'zero' => [false, '0']; + yield 'empty-string' => [false, '']; + yield 'custom-string' => [false, 'test']; + } + + #[DataProvider('dataBase')] + public function testBase(bool $expected, string $headerValue): void + { + $headerName = 'X-Debug-Ignore'; + $event = new BeforeRequest(new ServerRequest('GET', '/test', [$headerName => $headerValue])); + $condition = new HeaderCondition($headerName); + + $this->assertSame($expected, $condition->match($event)); + } + + public function testNonExistHeader(): void + { + $event = new BeforeRequest(new ServerRequest('GET', '/test', [])); + $condition = new HeaderCondition('X-Debug-Ignore'); + + $this->assertFalse($condition->match($event)); + } +} From 4e3f17c9aa8e65c2d4f7ee4497480e03d03b52ea Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Fri, 17 Jan 2025 11:26:12 +0300 Subject: [PATCH 20/25] Add `UriPathConditionTest` --- config/di-web.php | 4 +-- ...athsCondition.php => UriPathCondition.php} | 6 ++-- .../Condition/CommandNameConditionTest.php | 6 ++-- .../Condition/UriPathConditionTest.php | 33 +++++++++++++++++++ 4 files changed, 41 insertions(+), 8 deletions(-) rename src/StartupPolicy/Condition/{UriPathsCondition.php => UriPathCondition.php} (78%) create mode 100644 tests/Unit/StartupPolicy/Condition/UriPathConditionTest.php diff --git a/config/di-web.php b/config/di-web.php index 22e4205b..e52674a2 100644 --- a/config/di-web.php +++ b/config/di-web.php @@ -7,7 +7,7 @@ use Yiisoft\Yii\Debug\Debugger; use Yiisoft\Yii\Debug\StartupPolicy\Condition\EnvironmentVariableCondition; use Yiisoft\Yii\Debug\StartupPolicy\Condition\HeaderCondition; -use Yiisoft\Yii\Debug\StartupPolicy\Condition\UriPathsCondition; +use Yiisoft\Yii\Debug\StartupPolicy\Condition\UriPathCondition; use Yiisoft\Yii\Debug\StartupPolicy\Debugger\DenyDebuggerPolicy; if (!(bool)($params['yiisoft/yii-debug']['enabled'] ?? false)) { @@ -27,7 +27,7 @@ static fn () => new DenyDebuggerPolicy( new EnvironmentVariableCondition('YII_DEBUG_IGNORE'), new HeaderCondition('X-Debug-Ignore'), - new UriPathsCondition($params['yiisoft/yii-debug']['ignoredRequests']) + new UriPathCondition($params['yiisoft/yii-debug']['ignoredRequests']) ), ), 'excludedClasses' => $params['yiisoft/yii-debug']['excludedClasses'], diff --git a/src/StartupPolicy/Condition/UriPathsCondition.php b/src/StartupPolicy/Condition/UriPathCondition.php similarity index 78% rename from src/StartupPolicy/Condition/UriPathsCondition.php rename to src/StartupPolicy/Condition/UriPathCondition.php index 97e0a6e5..93ffa169 100644 --- a/src/StartupPolicy/Condition/UriPathsCondition.php +++ b/src/StartupPolicy/Condition/UriPathCondition.php @@ -7,12 +7,12 @@ use Yiisoft\Strings\WildcardPattern; use Yiisoft\Yii\Http\Event\BeforeRequest; -final class UriPathsCondition implements ConditionInterface +final class UriPathCondition implements ConditionInterface { public function __construct( /** * @var string[] - * @psalm-var list + * @psalm-var list */ private readonly array $paths, ) { @@ -27,7 +27,7 @@ public function match(object $event): bool $path = $event->getRequest()->getUri()->getPath(); foreach ($this->paths as $pattern) { - if ((new WildcardPattern($pattern))->match($path)) { + if ((new WildcardPattern($pattern, ['/']))->match($path)) { return true; } } diff --git a/tests/Unit/StartupPolicy/Condition/CommandNameConditionTest.php b/tests/Unit/StartupPolicy/Condition/CommandNameConditionTest.php index 0a66a2cc..b333d892 100644 --- a/tests/Unit/StartupPolicy/Condition/CommandNameConditionTest.php +++ b/tests/Unit/StartupPolicy/Condition/CommandNameConditionTest.php @@ -17,9 +17,9 @@ public static function dataBase(): iterable yield 'with-empty-name' => [false, ['test:run', 'test:stop'], '']; yield 'not-match' => [false, ['test:run', 'test:stop'], 'email:send']; yield 'match' => [true, ['test:run', 'test:stop'], 'test:stop']; - yield 'match-wildcard-1' => [true, ['test:app:*'], 'test:app:stop']; - yield 'match-wildcard-2' => [false, ['test:*'], 'test:app:stop']; - yield 'match-wildcard-3' => [true, ['test:**'], 'test:app:stop']; + yield 'wildcard-1' => [true, ['test:app:*'], 'test:app:stop']; + yield 'wildcard-2' => [false, ['test:*'], 'test:app:stop']; + yield 'wildcard-3' => [true, ['test:**'], 'test:app:stop']; } #[DataProvider('dataBase')] diff --git a/tests/Unit/StartupPolicy/Condition/UriPathConditionTest.php b/tests/Unit/StartupPolicy/Condition/UriPathConditionTest.php new file mode 100644 index 00000000..ad96e6d2 --- /dev/null +++ b/tests/Unit/StartupPolicy/Condition/UriPathConditionTest.php @@ -0,0 +1,33 @@ + [true, ['/'], '/']; + yield 'root-not-match' => [false, ['/test'], '/']; + yield 'not-match' => [false, ['/test/run', '/test/stop'], '/blog/post']; + yield 'wildcard-1' => [true, ['/blog/*'], '/blog/post']; + yield 'wildcard-2' => [false, ['/blog/*'], '/blog/post/23']; + yield 'wildcard-3' => [true, ['/blog/**'], '/blog/post/23']; + } + + #[DataProvider('dataBase')] + public function testBase(bool $expected, array $paths, string $uri): void + { + $event = new BeforeRequest(new ServerRequest('GET', $uri)); + $condition = new UriPathCondition($paths); + + $this->assertSame($expected, $condition->match($event)); + } +} From deb021bdc7f67ee593850bba5ec6a1035e520f62 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Fri, 17 Jan 2025 11:38:41 +0300 Subject: [PATCH 21/25] Add collector policies' tests --- tests/Support/StubCollector.php | 34 +++++++++++++++++++ .../BlackListCollectorPolicyTest.php | 33 ++++++++++++++++++ .../Collector/CallableCollectorPolicyTest.php | 32 +++++++++++++++++ .../WhiteListCollectorPolicyTest.php | 33 ++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 tests/Support/StubCollector.php create mode 100644 tests/Unit/StartupPolicy/Collector/BlackListCollectorPolicyTest.php create mode 100644 tests/Unit/StartupPolicy/Collector/CallableCollectorPolicyTest.php create mode 100644 tests/Unit/StartupPolicy/Collector/WhiteListCollectorPolicyTest.php diff --git a/tests/Support/StubCollector.php b/tests/Support/StubCollector.php new file mode 100644 index 00000000..2724f0a3 --- /dev/null +++ b/tests/Support/StubCollector.php @@ -0,0 +1,34 @@ +name; + } + + public function startup(): void + { + } + + public function shutdown(): void + { + } + + public function getCollected(): array + { + return $this->collected; + } +} diff --git a/tests/Unit/StartupPolicy/Collector/BlackListCollectorPolicyTest.php b/tests/Unit/StartupPolicy/Collector/BlackListCollectorPolicyTest.php new file mode 100644 index 00000000..6b1f9f59 --- /dev/null +++ b/tests/Unit/StartupPolicy/Collector/BlackListCollectorPolicyTest.php @@ -0,0 +1,33 @@ + [true, []]; + yield 'collector-not-exist' => [true, ['other' => new PredefinedCondition(true)]]; + yield 'collector-true' => [false, ['test' => new PredefinedCondition(true)]]; + yield 'collector-false' => [true, ['test' => new PredefinedCondition(false)]]; + } + + #[DataProvider('dataBase')] + public function testBase(bool $expected, array $conditions): void + { + $event = new stdClass(); + $collector = new StubCollector('test'); + $policy = new BlackListCollectorPolicy($conditions); + + $this->assertSame($expected, $policy->satisfies($collector, $event)); + } +} diff --git a/tests/Unit/StartupPolicy/Collector/CallableCollectorPolicyTest.php b/tests/Unit/StartupPolicy/Collector/CallableCollectorPolicyTest.php new file mode 100644 index 00000000..852269eb --- /dev/null +++ b/tests/Unit/StartupPolicy/Collector/CallableCollectorPolicyTest.php @@ -0,0 +1,32 @@ + [true, static fn ($collector, $event) => true]; + yield 'false' => [false, static fn ($collector, $event) => false]; + yield 'check-arguments' => [true, static fn (StubCollector $collector, stdClass $event) => true]; + } + + #[DataProvider('dataBase')] + public function testBase(bool $expected, callable $callable): void + { + $event = new stdClass(); + $collector = new StubCollector(); + + $policy = new CallableCollectorPolicy($callable); + + $this->assertSame($expected, $policy->satisfies($collector, $event)); + } +} diff --git a/tests/Unit/StartupPolicy/Collector/WhiteListCollectorPolicyTest.php b/tests/Unit/StartupPolicy/Collector/WhiteListCollectorPolicyTest.php new file mode 100644 index 00000000..b32504f6 --- /dev/null +++ b/tests/Unit/StartupPolicy/Collector/WhiteListCollectorPolicyTest.php @@ -0,0 +1,33 @@ + [false, []]; + yield 'collector-not-exist' => [false, ['other' => new PredefinedCondition(true)]]; + yield 'collector-true' => [true, ['test' => new PredefinedCondition(true)]]; + yield 'collector-false' => [false, ['test' => new PredefinedCondition(false)]]; + } + + #[DataProvider('dataBase')] + public function testBase(bool $expected, array $conditions): void + { + $event = new stdClass(); + $collector = new StubCollector('test'); + $policy = new WhiteListCollectorPolicy($conditions); + + $this->assertSame($expected, $policy->satisfies($collector, $event)); + } +} From 18d688d903d4a6bf8f456096e70aef8dbb7b8cb4 Mon Sep 17 00:00:00 2001 From: vjik <525501+vjik@users.noreply.github.com> Date: Fri, 17 Jan 2025 08:40:13 +0000 Subject: [PATCH 22/25] Apply Rector changes (CI) --- tests/Support/StubCollector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Support/StubCollector.php b/tests/Support/StubCollector.php index 2724f0a3..f678d3bd 100644 --- a/tests/Support/StubCollector.php +++ b/tests/Support/StubCollector.php @@ -9,7 +9,7 @@ final class StubCollector implements CollectorInterface { public function __construct( - private readonly string $name = __CLASS__, + private readonly string $name = self::class, private readonly array $collected = [], ) { } From c4aa34ae87b0301f464630dd734e147b96e0b412 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Fri, 17 Jan 2025 13:02:09 +0300 Subject: [PATCH 23/25] Add debugger policies' tests --- .../Debugger/AllowDebuggerPolicyTest.php | 38 +++++++++++++++++++ .../Debugger/CallableDebuggerPolicyTest.php | 29 ++++++++++++++ .../Debugger/DenyDebuggerPolicyTest.php | 38 +++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 tests/Unit/StartupPolicy/Debugger/AllowDebuggerPolicyTest.php create mode 100644 tests/Unit/StartupPolicy/Debugger/CallableDebuggerPolicyTest.php create mode 100644 tests/Unit/StartupPolicy/Debugger/DenyDebuggerPolicyTest.php diff --git a/tests/Unit/StartupPolicy/Debugger/AllowDebuggerPolicyTest.php b/tests/Unit/StartupPolicy/Debugger/AllowDebuggerPolicyTest.php new file mode 100644 index 00000000..d3f53a63 --- /dev/null +++ b/tests/Unit/StartupPolicy/Debugger/AllowDebuggerPolicyTest.php @@ -0,0 +1,38 @@ + [false, []]; + yield 'true' => [true, [new PredefinedCondition(true)]]; + yield 'false' => [false, [new PredefinedCondition(false)]]; + yield 'false-false-true' => [ + true, + [ + new PredefinedCondition(false), + new PredefinedCondition(false), + new PredefinedCondition(true), + ] + ]; + } + + #[DataProvider('dataBase')] + public function testBase(bool $expected, array $conditions): void + { + $event = new stdClass(); + $policy = new AllowDebuggerPolicy(...$conditions); + + $this->assertSame($expected, $policy->satisfies($event)); + } +} diff --git a/tests/Unit/StartupPolicy/Debugger/CallableDebuggerPolicyTest.php b/tests/Unit/StartupPolicy/Debugger/CallableDebuggerPolicyTest.php new file mode 100644 index 00000000..206097ed --- /dev/null +++ b/tests/Unit/StartupPolicy/Debugger/CallableDebuggerPolicyTest.php @@ -0,0 +1,29 @@ + [true, static fn ($event) => true]; + yield 'false' => [false, static fn ($event) => false]; + yield 'check-arguments' => [true, static fn (stdClass $event) => true]; + } + + #[DataProvider('dataBase')] + public function testBase(bool $expected, callable $callable): void + { + $event = new stdClass(); + $policy = new CallableDebuggerPolicy($callable); + + $this->assertSame($expected, $policy->satisfies($event)); + } +} diff --git a/tests/Unit/StartupPolicy/Debugger/DenyDebuggerPolicyTest.php b/tests/Unit/StartupPolicy/Debugger/DenyDebuggerPolicyTest.php new file mode 100644 index 00000000..0bd9fcf8 --- /dev/null +++ b/tests/Unit/StartupPolicy/Debugger/DenyDebuggerPolicyTest.php @@ -0,0 +1,38 @@ + [true, []]; + yield 'true' => [false, [new PredefinedCondition(true)]]; + yield 'false' => [true, [new PredefinedCondition(false)]]; + yield 'false-false-true' => [ + false, + [ + new PredefinedCondition(false), + new PredefinedCondition(false), + new PredefinedCondition(true), + ] + ]; + } + + #[DataProvider('dataBase')] + public function testBase(bool $expected, array $conditions): void + { + $event = new stdClass(); + $policy = new DenyDebuggerPolicy(...$conditions); + + $this->assertSame($expected, $policy->satisfies($event)); + } +} From 55fc2dbfee328b2f1fe5a37f96123c5f743b5797 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 17 Jan 2025 10:17:34 +0000 Subject: [PATCH 24/25] Apply fixes from StyleCI --- tests/Unit/StartupPolicy/Debugger/AllowDebuggerPolicyTest.php | 2 +- tests/Unit/StartupPolicy/Debugger/DenyDebuggerPolicyTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Unit/StartupPolicy/Debugger/AllowDebuggerPolicyTest.php b/tests/Unit/StartupPolicy/Debugger/AllowDebuggerPolicyTest.php index d3f53a63..183e8d9b 100644 --- a/tests/Unit/StartupPolicy/Debugger/AllowDebuggerPolicyTest.php +++ b/tests/Unit/StartupPolicy/Debugger/AllowDebuggerPolicyTest.php @@ -23,7 +23,7 @@ public static function dataBase(): iterable new PredefinedCondition(false), new PredefinedCondition(false), new PredefinedCondition(true), - ] + ], ]; } diff --git a/tests/Unit/StartupPolicy/Debugger/DenyDebuggerPolicyTest.php b/tests/Unit/StartupPolicy/Debugger/DenyDebuggerPolicyTest.php index 0bd9fcf8..e1b6ef43 100644 --- a/tests/Unit/StartupPolicy/Debugger/DenyDebuggerPolicyTest.php +++ b/tests/Unit/StartupPolicy/Debugger/DenyDebuggerPolicyTest.php @@ -23,7 +23,7 @@ public static function dataBase(): iterable new PredefinedCondition(false), new PredefinedCondition(false), new PredefinedCondition(true), - ] + ], ]; } From 14448dcce8cbf517ab2649ed3c43eaf5b90ed865 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Fri, 17 Jan 2025 14:57:53 +0300 Subject: [PATCH 25/25] improve --- src/Debugger.php | 13 ++++++----- .../Collector/AllowAllCollectorPolicy.php | 15 +++++++++++++ .../Debugger/AlwaysOnDebuggerPolicy.php | 13 +++++++++++ .../Collector/AllowAllCollectorPolicyTest.php | 22 +++++++++++++++++++ .../Debugger/AlwaysOnDebuggerPolicyTest.php | 20 +++++++++++++++++ 5 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 src/StartupPolicy/Collector/AllowAllCollectorPolicy.php create mode 100644 src/StartupPolicy/Debugger/AlwaysOnDebuggerPolicy.php create mode 100644 tests/Unit/StartupPolicy/Collector/AllowAllCollectorPolicyTest.php create mode 100644 tests/Unit/StartupPolicy/Debugger/AlwaysOnDebuggerPolicyTest.php diff --git a/src/Debugger.php b/src/Debugger.php index 0d5809b3..38cf267f 100644 --- a/src/Debugger.php +++ b/src/Debugger.php @@ -7,7 +7,9 @@ use LogicException; use Yiisoft\Yii\Debug\Collector\CollectorInterface; use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface; +use Yiisoft\Yii\Debug\StartupPolicy\Collector\AllowAllCollectorPolicy; use Yiisoft\Yii\Debug\StartupPolicy\Collector\CollectorStartupPolicyInterface; +use Yiisoft\Yii\Debug\StartupPolicy\Debugger\AlwaysOnDebuggerPolicy; use Yiisoft\Yii\Debug\StartupPolicy\Debugger\DebuggerStartupPolicyInterface; use Yiisoft\Yii\Debug\Storage\StorageInterface; @@ -33,8 +35,8 @@ final class Debugger public function __construct( private readonly StorageInterface $storage, array $collectors, - private readonly ?DebuggerStartupPolicyInterface $debuggerStartupPolicy = null, - private readonly ?CollectorStartupPolicyInterface $collectorStartupPolicy = null, + private readonly DebuggerStartupPolicyInterface $debuggerStartupPolicy = new AlwaysOnDebuggerPolicy(), + private readonly CollectorStartupPolicyInterface $collectorStartupPolicy = new AllowAllCollectorPolicy(), array $excludedClasses = [], ) { $preparedCollectors = []; @@ -60,17 +62,16 @@ public function getId(): string public function startup(object $event): void { - if ($this->debuggerStartupPolicy?->satisfies($event) === false) { + if (!$this->debuggerStartupPolicy->satisfies($event)) { return; } $this->id = str_replace('.', '', uniqid('', true)); foreach ($this->collectors as $collector) { - if ($this->collectorStartupPolicy?->satisfies($collector, $event) === false) { - continue; + if ($this->collectorStartupPolicy->satisfies($collector, $event)) { + $collector->startup(); } - $collector->startup(); } } diff --git a/src/StartupPolicy/Collector/AllowAllCollectorPolicy.php b/src/StartupPolicy/Collector/AllowAllCollectorPolicy.php new file mode 100644 index 00000000..fb6d7cfa --- /dev/null +++ b/src/StartupPolicy/Collector/AllowAllCollectorPolicy.php @@ -0,0 +1,15 @@ +assertTrue($policy->satisfies($collector, $event)); + } +} diff --git a/tests/Unit/StartupPolicy/Debugger/AlwaysOnDebuggerPolicyTest.php b/tests/Unit/StartupPolicy/Debugger/AlwaysOnDebuggerPolicyTest.php new file mode 100644 index 00000000..fa1281b6 --- /dev/null +++ b/tests/Unit/StartupPolicy/Debugger/AlwaysOnDebuggerPolicyTest.php @@ -0,0 +1,20 @@ +assertTrue($policy->satisfies($event)); + } +}