-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1017 from spiral/feature/bootloader-rules
[spiral/boot] Added the ability to configure bootloaders via BootloadConfig
- Loading branch information
Showing
34 changed files
with
1,123 additions
and
98 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Boot\Attribute; | ||
|
||
use Spiral\Attributes\NamedArgumentConstructor; | ||
|
||
#[\Attribute(\Attribute::TARGET_CLASS), NamedArgumentConstructor] | ||
class BootloadConfig | ||
{ | ||
public function __construct( | ||
public array $args = [], | ||
public bool $enabled = true, | ||
public array $allowEnv = [], | ||
public array $denyEnv = [], | ||
public bool $override = 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
27 changes: 27 additions & 0 deletions
27
src/Boot/src/BootloadManager/Checker/BootloaderChecker.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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Boot\BootloadManager\Checker; | ||
|
||
use Spiral\Boot\Attribute\BootloadConfig; | ||
use Spiral\Boot\Bootloader\BootloaderInterface; | ||
|
||
final class BootloaderChecker implements BootloaderCheckerInterface | ||
{ | ||
public function __construct( | ||
private readonly CheckerRegistryInterface $registry = new CheckerRegistry(), | ||
) { | ||
} | ||
|
||
public function canInitialize(BootloaderInterface|string $bootloader, ?BootloadConfig $config = null): bool | ||
{ | ||
foreach ($this->registry->getCheckers() as $checker) { | ||
if (!$checker->canInitialize($bootloader, $config)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Boot/src/BootloadManager/Checker/BootloaderCheckerInterface.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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Boot\BootloadManager\Checker; | ||
|
||
use Spiral\Boot\Attribute\BootloadConfig; | ||
use Spiral\Boot\Bootloader\BootloaderInterface; | ||
|
||
interface BootloaderCheckerInterface | ||
{ | ||
/** | ||
* @param class-string<BootloaderInterface>|BootloaderInterface $bootloader | ||
*/ | ||
public function canInitialize(string|BootloaderInterface $bootloader, ?BootloadConfig $config = null): 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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Boot\BootloadManager\Checker; | ||
|
||
use Spiral\Boot\Attribute\BootloadConfig; | ||
use Spiral\Boot\Bootloader\BootloaderInterface; | ||
use Spiral\Boot\BootloadManager\ClassesRegistry; | ||
|
||
final class CanBootedChecker implements BootloaderCheckerInterface | ||
{ | ||
public function __construct( | ||
private readonly ClassesRegistry $bootloaders, | ||
) { | ||
} | ||
|
||
public function canInitialize(BootloaderInterface|string $bootloader, ?BootloadConfig $config = null): bool | ||
{ | ||
$ref = new \ReflectionClass($bootloader); | ||
|
||
return !$this->bootloaders->isBooted($ref->getName()) | ||
&& !$ref->isAbstract() | ||
&& !$ref->isInterface() | ||
&& $ref->implementsInterface(BootloaderInterface::class); | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Boot\BootloadManager\Checker; | ||
|
||
final class CheckerRegistry implements CheckerRegistryInterface | ||
{ | ||
/** | ||
* @var array<BootloaderCheckerInterface> | ||
*/ | ||
private array $checkers = []; | ||
|
||
public function register(BootloaderCheckerInterface $checker): void | ||
{ | ||
$this->checkers[] = $checker; | ||
} | ||
|
||
/** | ||
* @return array<BootloaderCheckerInterface> | ||
*/ | ||
public function getCheckers(): array | ||
{ | ||
return $this->checkers; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Boot/src/BootloadManager/Checker/CheckerRegistryInterface.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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Boot\BootloadManager\Checker; | ||
|
||
interface CheckerRegistryInterface | ||
{ | ||
public function register(BootloaderCheckerInterface $checker): void; | ||
|
||
/** | ||
* @return array<BootloaderCheckerInterface> | ||
*/ | ||
public function getCheckers(): array; | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Boot/src/BootloadManager/Checker/ClassExistsChecker.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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Boot\BootloadManager\Checker; | ||
|
||
use Spiral\Boot\Attribute\BootloadConfig; | ||
use Spiral\Boot\Bootloader\BootloaderInterface; | ||
use Spiral\Boot\Exception\ClassNotFoundException; | ||
|
||
final class ClassExistsChecker implements BootloaderCheckerInterface | ||
{ | ||
/** | ||
* @throws ClassNotFoundException | ||
*/ | ||
public function canInitialize(BootloaderInterface|string $bootloader, ?BootloadConfig $config = null): bool | ||
{ | ||
if (!\is_string($bootloader)) { | ||
return true; | ||
} | ||
|
||
if (!\class_exists($bootloader)) { | ||
throw new ClassNotFoundException(\sprintf('Bootloader class `%s` is not exist.', $bootloader)); | ||
} | ||
|
||
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,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Boot\BootloadManager\Checker; | ||
|
||
use Spiral\Boot\Attribute\BootloadConfig; | ||
use Spiral\Boot\Bootloader\BootloaderInterface; | ||
use Spiral\Boot\EnvironmentInterface; | ||
|
||
final class ConfigChecker implements BootloaderCheckerInterface | ||
{ | ||
public function __construct( | ||
private readonly EnvironmentInterface $environment, | ||
) { | ||
} | ||
|
||
public function canInitialize(BootloaderInterface|string $bootloader, ?BootloadConfig $config = null): bool | ||
{ | ||
if ($config === null) { | ||
return true; | ||
} | ||
|
||
if (!$config->enabled) { | ||
return false; | ||
} | ||
|
||
foreach ($config->denyEnv as $env => $denyValues) { | ||
$value = $this->environment->get($env); | ||
if ($value !== null && \in_array($value, (array) $denyValues, true)) { | ||
return false; | ||
} | ||
} | ||
|
||
foreach ($config->allowEnv as $env => $allowValues) { | ||
$value = $this->environment->get($env); | ||
if ($value === null || !\in_array($value, (array) $allowValues, true)) { | ||
return false; | ||
} | ||
} | ||
|
||
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
Oops, something went wrong.