-
-
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 #1015 from spiral/feature/bootloaders-registry
[spiral/boot] Adding `BootloaderRegistry` to improve the bootloader registration process
- Loading branch information
Showing
7 changed files
with
237 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Boot\Bootloader; | ||
|
||
/** | ||
* @psalm-import-type TClass from \Spiral\Boot\BootloadManagerInterface | ||
*/ | ||
final class BootloaderRegistry implements BootloaderRegistryInterface | ||
{ | ||
/** | ||
* @param array<TClass>|array<TClass, array<string, mixed>> $systemBootloaders | ||
* @param array<TClass>|array<TClass, array<string, mixed>> $bootloaders | ||
*/ | ||
public function __construct( | ||
private array $systemBootloaders = [], | ||
private array $bootloaders = [], | ||
) { | ||
} | ||
|
||
/** | ||
* @param TClass|array<TClass, array<string, mixed>> $bootloader | ||
*/ | ||
public function registerSystem(string|array $bootloader): void | ||
{ | ||
if ($this->hasBootloader($bootloader)) { | ||
return; | ||
} | ||
|
||
\is_string($bootloader) | ||
? $this->systemBootloaders[] = $bootloader | ||
: $this->systemBootloaders[\array_key_first($bootloader)] = $bootloader[\array_key_first($bootloader)] | ||
; | ||
} | ||
|
||
/** | ||
* @param TClass|array<TClass, array<string, mixed>> $bootloader | ||
*/ | ||
public function register(string|array $bootloader): void | ||
{ | ||
if ($this->hasBootloader($bootloader)) { | ||
return; | ||
} | ||
|
||
\is_string($bootloader) | ||
? $this->bootloaders[] = $bootloader | ||
: $this->bootloaders[\array_key_first($bootloader)] = $bootloader[\array_key_first($bootloader)] | ||
; | ||
} | ||
|
||
/** | ||
* @return array<TClass>|array<TClass, array<string, mixed>> | ||
*/ | ||
public function getSystemBootloaders(): array | ||
{ | ||
return $this->systemBootloaders; | ||
} | ||
|
||
/** | ||
* @return array<TClass>|array<TClass, array<string, mixed>> | ||
*/ | ||
public function getBootloaders(): array | ||
{ | ||
return $this->bootloaders; | ||
} | ||
|
||
/** | ||
* @param TClass|array<TClass, array<string, mixed>> $bootloader | ||
*/ | ||
private function hasBootloader(string|array $bootloader): bool | ||
{ | ||
if (\is_array($bootloader)) { | ||
return false; | ||
} | ||
|
||
return | ||
\in_array($bootloader, $this->systemBootloaders, true) || | ||
\in_array($bootloader, $this->bootloaders, 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,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Boot\Bootloader; | ||
|
||
/** | ||
* @psalm-import-type TClass from \Spiral\Boot\BootloadManagerInterface | ||
*/ | ||
interface BootloaderRegistryInterface | ||
{ | ||
/** | ||
* @param TClass|array<TClass, array<string, mixed>> $bootloader | ||
* | ||
* Examples: | ||
* 1. SimpleBootloader::class, | ||
* 2. [SimpleBootloader::class => ['option' => 'value']] | ||
*/ | ||
public function registerSystem(string|array $bootloader): void; | ||
|
||
/** | ||
* @param TClass|array<TClass, array<string, mixed>> $bootloader | ||
* | ||
* Examples: | ||
* 1. SimpleBootloader::class, | ||
* 2. [SimpleBootloader::class => ['option' => 'value']] | ||
*/ | ||
public function register(string|array $bootloader): void; | ||
|
||
/** | ||
* @return array<TClass>|array<TClass, array<string, mixed>> | ||
*/ | ||
public function getSystemBootloaders(): array; | ||
|
||
/** | ||
* @return array<TClass>|array<TClass, array<string, mixed>> | ||
*/ | ||
public function getBootloaders(): array; | ||
} |
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,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Tests\Boot\Bootloader; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Spiral\Boot\Bootloader\BootloaderRegistry; | ||
|
||
final class BootloaderRegistryTest extends TestCase | ||
{ | ||
public function testConstructor(): void | ||
{ | ||
$registry = new BootloaderRegistry(); | ||
$this->assertSame([], $registry->getSystemBootloaders()); | ||
$this->assertSame([], $registry->getBootloaders()); | ||
|
||
$registry = new BootloaderRegistry( | ||
['BootloaderA', 'BootloaderB'], | ||
['BootloaderC', 'BootloaderD'], | ||
); | ||
|
||
$this->assertSame(['BootloaderA', 'BootloaderB'], $registry->getSystemBootloaders()); | ||
$this->assertSame(['BootloaderC', 'BootloaderD'], $registry->getBootloaders()); | ||
} | ||
|
||
public function testSystemBootloaders(): void | ||
{ | ||
$registry = new BootloaderRegistry(); | ||
$this->assertSame([], $registry->getSystemBootloaders()); | ||
|
||
$registry->registerSystem('BootloaderA'); | ||
$registry->registerSystem(['BootloaderB' => ['option' => 'value']]); | ||
|
||
$this->assertSame([ | ||
'BootloaderA', | ||
'BootloaderB' => ['option' => 'value'], | ||
], $registry->getSystemBootloaders()); | ||
} | ||
|
||
public function testBootloaders(): void | ||
{ | ||
$registry = new BootloaderRegistry(); | ||
$this->assertSame([], $registry->getBootloaders()); | ||
|
||
$registry->register('BootloaderA'); | ||
$registry->register(['BootloaderB' => ['option' => 'value']]); | ||
|
||
$this->assertSame([ | ||
'BootloaderA', | ||
'BootloaderB' => ['option' => 'value'], | ||
], $registry->getBootloaders()); | ||
} | ||
|
||
public function testDuplicateBootloader(): void | ||
{ | ||
$registry = new BootloaderRegistry(); | ||
$registry->register('BootloaderA'); | ||
$registry->register('BootloaderA'); | ||
$registry->registerSystem('BootloaderA'); | ||
|
||
$this->assertSame(['BootloaderA'], $registry->getBootloaders()); | ||
$this->assertSame([], $registry->getSystemBootloaders()); | ||
|
||
$registry = new BootloaderRegistry(); | ||
$registry->registerSystem('BootloaderA'); | ||
$registry->registerSystem('BootloaderA'); | ||
$registry->register('BootloaderA'); | ||
|
||
$this->assertSame([], $registry->getBootloaders()); | ||
$this->assertSame(['BootloaderA'], $registry->getSystemBootloaders()); | ||
} | ||
} |
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