Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Jan 16, 2025
1 parent ca4c5e1 commit 31f70b7
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 = [];
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/StartupPolicy/Collector/BlackListCollectorPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
/**
Expand All @@ -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) {
Expand Down
35 changes: 35 additions & 0 deletions src/StartupPolicy/Collector/CallableCollectorPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Debug\StartupPolicy\Collector;

use Yiisoft\Yii\Debug\Collector\CollectorInterface;

use function call_user_func;

/**
* @psalm-type TCallable = callable(CollectorInterface, object): bool
*/
final class CallableCollectorPolicy implements CollectorStartupPolicyInterface
{
/**
* @var callable
* @psalm-var TCallable
*/
private $callable;

/**
* @psalm-param TCallable $callable
*/
public function __construct(
callable $callable
) {
$this->callable = $callable;
}

public function satisfies(CollectorInterface $collector, object $event): bool
{
return call_user_func($this->callable, $collector, $event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 2 additions & 2 deletions src/StartupPolicy/Collector/WhiteListCollectorPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
/**
Expand All @@ -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) {
Expand Down

0 comments on commit 31f70b7

Please sign in to comment.