-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
909 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Debug\StartupPolicy\Collector; | ||
|
||
use Yiisoft\Yii\Debug\Collector\CollectorInterface; | ||
|
||
final class AllowAllCollectorPolicy implements CollectorStartupPolicyInterface | ||
{ | ||
public function satisfies(CollectorInterface $collector, object $event): bool | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Debug\StartupPolicy\Collector; | ||
|
||
use Yiisoft\Yii\Debug\Collector\CollectorInterface; | ||
use Yiisoft\Yii\Debug\StartupPolicy\Condition\ConditionInterface; | ||
|
||
final class BlackListCollectorPolicy implements CollectorStartupPolicyInterface | ||
{ | ||
public function __construct( | ||
/** | ||
* @var ConditionInterface[] | ||
* @psalm-var array<string, ConditionInterface> | ||
*/ | ||
private readonly array $conditions, | ||
) { | ||
} | ||
|
||
public function satisfies(CollectorInterface $collector, object $event): bool | ||
{ | ||
$condition = $this->conditions[$collector->getName()] ?? null; | ||
if ($condition === null) { | ||
return true; | ||
} | ||
|
||
return !$condition->match($event); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Debug\StartupPolicy\Collector; | ||
|
||
use Yiisoft\Yii\Debug\Collector\CollectorInterface; | ||
|
||
/** | ||
* @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 ($this->callable)($collector, $event); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/StartupPolicy/Collector/CollectorStartupPolicyInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Debug\StartupPolicy\Collector; | ||
|
||
use Yiisoft\Yii\Debug\Collector\CollectorInterface; | ||
|
||
interface CollectorStartupPolicyInterface | ||
{ | ||
public function satisfies(CollectorInterface $collector, object $event): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Debug\StartupPolicy\Collector; | ||
|
||
use Yiisoft\Yii\Debug\Collector\CollectorInterface; | ||
use Yiisoft\Yii\Debug\StartupPolicy\Condition\ConditionInterface; | ||
|
||
final class WhiteListCollectorPolicy implements CollectorStartupPolicyInterface | ||
{ | ||
public function __construct( | ||
/** | ||
* @var ConditionInterface[] | ||
* @psalm-var array<string, ConditionInterface> | ||
*/ | ||
private readonly array $conditions, | ||
) { | ||
} | ||
|
||
public function satisfies(CollectorInterface $collector, object $event): bool | ||
{ | ||
$condition = $this->conditions[$collector->getName()] ?? null; | ||
if ($condition === null) { | ||
return false; | ||
} | ||
|
||
return $condition->match($event); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Debug\StartupPolicy\Condition; | ||
|
||
use Yiisoft\Strings\WildcardPattern; | ||
use Yiisoft\Yii\Console\Event\ApplicationStartup; | ||
|
||
final class CommandNameCondition implements ConditionInterface | ||
{ | ||
public function __construct( | ||
/** | ||
* @var string[] | ||
* @psalm-var list<non-empty-string> | ||
*/ | ||
private readonly array $names, | ||
) { | ||
} | ||
|
||
public function match(object $event): bool | ||
{ | ||
if (!$event instanceof ApplicationStartup) { | ||
return false; | ||
} | ||
|
||
$name = (string) $event->commandName; | ||
|
||
foreach ($this->names as $pattern) { | ||
if ((new WildcardPattern($pattern, [':']))->match($name)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Debug\StartupPolicy\Condition; | ||
|
||
interface ConditionInterface | ||
{ | ||
public function match(object $event): bool; | ||
} |
23 changes: 23 additions & 0 deletions
23
src/StartupPolicy/Condition/EnvironmentVariableCondition.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Debug\StartupPolicy\Condition; | ||
|
||
use function in_array; | ||
|
||
final class EnvironmentVariableCondition implements ConditionInterface | ||
{ | ||
private const TRUE_VALUES = ['1', 'true', 'on']; | ||
|
||
public function __construct( | ||
private readonly string $variableName, | ||
) { | ||
} | ||
|
||
public function match(object $event): bool | ||
{ | ||
$value = getenv($this->variableName); | ||
return $value !== false && in_array(strtolower($value), self::TRUE_VALUES, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Debug\StartupPolicy\Condition; | ||
|
||
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, | ||
) { | ||
} | ||
|
||
public function match(object $event): bool | ||
{ | ||
if (!$event instanceof BeforeRequest) { | ||
return false; | ||
} | ||
|
||
$request = $event->getRequest(); | ||
|
||
return $request->hasHeader($this->headerName) | ||
&& in_array(strtolower($request->getHeaderLine($this->headerName)), self::TRUE_VALUES, true); | ||
} | ||
} |
Oops, something went wrong.