Skip to content

Commit

Permalink
Merge pull request #12 from msmakouz/feature/discoverer
Browse files Browse the repository at this point in the history
Using BootloaderRegistry
  • Loading branch information
butschster authored Nov 22, 2023
2 parents b29d2c1 + 36a4216 commit 9df217f
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 171 deletions.
1 change: 1 addition & 0 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
on:
pull_request: null
push:
branches:
- master
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/psalm.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
on:
pull_request: null
push:
branches:
- master
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
],
"require": {
"php": "^8.1",
"spiral/boot": "^3.0",
"spiral/core": "^3.0"
"spiral/boot": "^3.10",
"spiral/core": "^3.10"
},
"require-dev": {
"spiral/framework": "^3.0",
"spiral/testing": "^2.0",
"vimeo/psalm": "^4.9"
"spiral/framework": "dev-feature/bootloaders-registry as 3.10.x-dev",
"spiral/testing": "^2.6",
"vimeo/psalm": "^5.15"
},
"autoload": {
"psr-4": {
Expand Down
38 changes: 0 additions & 38 deletions src/Bootloader/ArrayRegistry.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Bootloader/BootloaderRegistryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
interface BootloaderRegistryInterface extends RegistryInterface
{
/**
* @return TClass[]|array<TClass, array<non-empty-string, mixed>>
* @return TClass[]|array<TClass, array<string, mixed>>
*/
public function getBootloaders(): array;

Expand Down
7 changes: 7 additions & 0 deletions src/Bootloader/ConfigRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
namespace Spiral\Discoverer\Bootloader;

use Psr\Container\ContainerInterface;
use Spiral\Boot\BootloadManagerInterface;
use Spiral\Discoverer\Config\DiscovererConfig;

/**
* @psalm-import-type TClass from BootloadManagerInterface
*/
final class ConfigRegistry implements BootloaderRegistryInterface
{
private ?DiscovererConfig $config = null;
Expand All @@ -21,6 +25,9 @@ public function init(ContainerInterface $container): void
\assert($this->config instanceof DiscovererConfig);
}

/**
* @return TClass[]|array<TClass, array<string, mixed>>
*/
public function getBootloaders(): array
{
return $this->config !== null ? $this->config->getBootloaders() : [];
Expand Down
43 changes: 40 additions & 3 deletions src/Config/DiscovererConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,71 @@

namespace Spiral\Discoverer\Config;

use Spiral\Boot\Bootloader\BootloaderInterface;
use Spiral\Boot\BootloadManagerInterface;
use Spiral\Core\InjectableConfig;
use Spiral\Discoverer\Bootloader\BootloaderRegistryInterface;
use Spiral\Discoverer\Bootloader as BootloaderRegistry;
use Spiral\Discoverer\Tokenizer as TokenizerRegistry;
use Spiral\Discoverer\Tokenizer\DirectoryRegistryInterface;

/**
* @psalm-import-type TClass from BootloadManagerInterface
* @property array{
* bootloaders: array<TClass>|array<TClass, array<string, mixed>>,
* ignoredBootloaders: array<TClass>,
* registries: array{
* bootloaders: array<class-string<BootloaderRegistryInterface>>,
* directories: array<class-string<DirectoryRegistryInterface>>
* }
* } $config
*/
final class DiscovererConfig extends InjectableConfig
{
public const CONFIG = 'discoverer';

protected array $config = [
'bootloaders' => [],
'ignoredBootloaders' => [],
'registries' => [
'bootloaders' => [
BootloaderRegistry\ComposerRegistry::class,
BootloaderRegistry\ConfigRegistry::class,
],
'directories' => [
TokenizerRegistry\ComposerRegistry::class,
],
],
];

/**
* @return TClass[]|array<TClass, array<non-empty-string, mixed>>
* @return array<TClass>|array<TClass, array<string, mixed>>
*/
public function getBootloaders(): array
{
return (array)$this->config['bootloaders'];
}

/**
* @return TClass[]
* @return array<TClass>
*/
public function getIgnoredBootloaders(): array
{
return (array)$this->config['ignoredBootloaders'];
}

/**
* @return array<class-string<BootloaderRegistryInterface>>
*/
public function getBootloaderRegistries(): array
{
return $this->config['registries']['bootloaders'] ?? [];
}

/**
* @return array<class-string<DirectoryRegistryInterface>>
*/
public function getDirectoryRegistries(): array
{
return $this->config['registries']['directories'] ?? [];
}
}
101 changes: 84 additions & 17 deletions src/DiscovererBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,104 @@

namespace Spiral\Discoverer;

use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Discoverer\Tokenizer\DirectoriesDiscoverer;
use Spiral\Boot\Bootloader\BootloaderRegistryInterface;
use Spiral\Config\ConfiguratorInterface;
use Spiral\Discoverer\Bootloader as BootloaderRegistry;
use Spiral\Discoverer\Config\DiscovererConfig;
use Spiral\Discoverer\Tokenizer as TokenizerRegistry;
use Spiral\Tokenizer\Bootloader\TokenizerBootloader;

final class DiscovererBootloader extends Bootloader
{
protected const DEPENDENCIES = [
TokenizerBootloader::class,
];
public function __construct(
private readonly ConfiguratorInterface $config,
) {
}

public function defineSingletons(): array
{
return [
DiscovererInterface::class => [self::class, 'initDiscoverer'],
];
}

public function init(ContainerInterface $container, TokenizerBootloader $tokenizerBootloader): void
{
$this->initDefaultConfig();
$this->registerDirectories($container, $tokenizerBootloader);
$this->registerBootloaders($container);
}

private function initDefaultConfig(): void
{
$this->config->setDefaults(DiscovererConfig::CONFIG, [
'bootloaders' => [],
'ignoredBootloaders' => [],
'registries' => [
'bootloaders' => [
BootloaderRegistry\ComposerRegistry::class,
BootloaderRegistry\ConfigRegistry::class,
],
'directories' => [
TokenizerRegistry\ComposerRegistry::class,
],
],
]);
}

/**
* @throws Exception\DiscovererRegistryException
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function init(
ContainerInterface $container,
TokenizerBootloader $tokenizerBootloader
): void {
if (!$container->has(DiscovererInterface::class)) {
return;
private function registerDirectories(ContainerInterface $container, TokenizerBootloader $tokenizerBootloader): void
{
$discoverer = $container->get(DiscovererInterface::class);

if ($discoverer->has(TokenizerRegistry\DirectoriesDiscoverer::getName())) {
foreach ($discoverer->discover(TokenizerRegistry\DirectoriesDiscoverer::getName()) as $directory) {
$tokenizerBootloader->addDirectory($directory);
}
}
}

private function registerBootloaders(ContainerInterface $container): void
{
$discoverer = $container->get(DiscovererInterface::class);
\assert($discoverer instanceof DiscovererInterface);
$bootloadersRegistry = $container->get(BootloaderRegistryInterface::class);

if ($discoverer->has(DirectoriesDiscoverer::getName())) {
foreach ($discoverer->discover(DirectoriesDiscoverer::getName()) as $directory) {
$tokenizerBootloader->addDirectory($directory);
if ($discoverer->has(BootloaderRegistry\BootloadersDiscoverer::getName())) {
$bootloaders = $discoverer->discover(BootloaderRegistry\BootloadersDiscoverer::getName());
foreach ($bootloaders as $bootloader => $options) {
$bootloadersRegistry->register([$bootloader => $options]);
}
};
}
}

private function initDiscoverer(DiscovererConfig $config, ContainerInterface $container): DiscovererInterface
{
$bootloaders = [];
foreach ($config->getBootloaderRegistries() as $registry) {
$registry = $container->get($registry);
\assert($registry instanceof BootloaderRegistry\BootloaderRegistryInterface);
$bootloaders[] = $registry;
}

$directories = [];
foreach ($config->getDirectoryRegistries() as $registry) {
$registry = $container->get($registry);
\assert($registry instanceof TokenizerRegistry\DirectoryRegistryInterface);
$directories[] = $registry;
}

return new Discoverer(
$container,
new BootloaderRegistry\BootloadersDiscoverer(...$bootloaders),
new TokenizerRegistry\DirectoriesDiscoverer(...$directories),
);
}
}
31 changes: 0 additions & 31 deletions src/WithDiscovering.php

This file was deleted.

31 changes: 0 additions & 31 deletions tests/src/Bootloader/ArrayRegistryTest.php

This file was deleted.

7 changes: 2 additions & 5 deletions tests/src/Bootloader/BootloadersDiscovererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@

final class BootloadersDiscovererTest extends TestCase
{
public function testGetsName()
public function testGetsName(): void
{
return $this->assertSame(
'bootloaders',
BootloadersDiscoverer::getName()
);
$this->assertSame('bootloaders', BootloadersDiscoverer::getName());
}

public function testDiscover(): void
Expand Down
18 changes: 18 additions & 0 deletions tests/src/DiscovererBootloaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Spiral\Discoverer\Tests;

final class DiscovererBootloaderTest extends TestCase
{
public function testTokenizerDirectoriesShouldBeDiscovered(): void
{
$dirs = $this->getConfig('tokenizer')['directories'];

$rootDir = $this->getDirectoryByAlias('root');

$this->assertContains($rootDir.'vendor/composer/../foo/notifications/src/foo', $dirs);
$this->assertContains($rootDir.'vendor/composer/../foo/notifications/src/bar', $dirs);
}
}
Loading

0 comments on commit 9df217f

Please sign in to comment.