Skip to content

Commit

Permalink
Package refactoring
Browse files Browse the repository at this point in the history
- Added ability to use custom Discoverers
- Added tokenizer discoverer
- Covered with unit tests
  • Loading branch information
butschster committed Feb 11, 2022
1 parent 0138675 commit e8e120d
Show file tree
Hide file tree
Showing 35 changed files with 1,021 additions and 338 deletions.
248 changes: 204 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Bootloaders discoverer for Spiral Framework
# Discoverer for Spiral Framework

[![Latest Version on Packagist](https://img.shields.io/packagist/v/spiral-packages/bootloaders-discover.svg?style=flat-square)](https://packagist.org/packages/spiral-packages/bootloaders-discover)
[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/spiral-packages/bootloaders-discover/run-tests?label=tests)](https://github.com/spiral-packages/bootloaders-discover/actions?query=workflow%3Arun-tests+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/spiral-packages/bootloaders-discover.svg?style=flat-square)](https://packagist.org/packages/spiral-packages/bootloaders-discover)
[![Latest Version on Packagist](https://img.shields.io/packagist/v/spiral-packages/discoverer.svg?style=flat-square)](https://packagist.org/packages/spiral-packages/discoverer)
[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/spiral-packages/discoverer/run-tests?label=tests)](https://github.com/spiral-packages/discoverer/actions?query=workflow%3Arun-tests+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/spiral-packages/discoverer.svg?style=flat-square)](https://packagist.org/packages/spiral-packages/discoverer)

## Requirements

Expand All @@ -16,23 +16,32 @@ Make sure that your server is configured with following PHP version and extensio
You can install the package via composer:

```bash
composer require spiral-packages/bootloaders-discover
composer require spiral-packages/discoverer
```

After package install you need to add `Spiral\BootloadersDiscover\WithBootloadersDiscovering` trait from the package to
your Application kernel.
After package install you need to register bootloader from the package.

```php
protected const LOAD = [
// ...
\Spiral\Discoverer\DiscovererBootloader::class,
];
```

After package install you need to add `Spiral\Discoverer\WithDiscovering` trait from the package to your Application
kernel.

```php
use Spiral\Discoverer\WithDiscovering;
use Spiral\Framework\Kernel;
use Spiral\BootloadersDiscover\WithBootloadersDiscovering;

class App extends Kernel
{
use WithBootloadersDiscovering;
use WithDiscovering;
}
```

And then you should modify you `app.php` file.
And then you should modify you `app.php` file. Example you can see below.

```php
<?php
Expand All @@ -58,37 +67,22 @@ require __DIR__ . '/vendor/autoload.php';


//
// Initialize shared container, bindings, directories and etc.
// Initialize shared container, bindings, directories etc.
//
$app = App::create([
'root' => __DIR__
]);

$app->discoverBootloadersFrom(
// Will load bootloaders from composer.json and from other installed composer packages
// "extra": {
// "spiral": {
// "bootloaders": [
// "Spiral\\Monolog\\Bootloader\\DotenvBootloader",
// "Spiral\\DotEnv\\Bootloader\\MonologBootloader",
// ],
// "dont-discover": [
// "spiral-packages/event-bus"
// ]
// }
// },
new \Spiral\BootloadersDiscover\Registry\ComposerRegistry(),
$app->discover(
new \Spiral\Discoverer\Bootloader\BootloadersDiscoverer(
new \Spiral\Discoverer\Bootloader\ComposerRegistry(),
new \Spiral\Discoverer\Bootloader\ArrayRegistry(...),
new \Spiral\Discoverer\Bootloader\ConfigRegistry()
),

// Will load bootloaders from passed array of bootloaders
new \Spiral\BootloadersDiscover\Registry\ArrayRegistry([
// Application specific logs
Bootloader\LoggingBootloader::class,

// ...
]),

// Will load bootloaders from config/bootloaders.php
new \Spiral\BootloadersDiscover\Registry\ConfigRegistry()
new \Spiral\Discoverer\Tokenizer\DirectoriesDiscoverer(
new \Spiral\Discoverer\Tokenizer\ComposerRegistry(),
)
);

if ($app->run() !== null) {
Expand All @@ -97,9 +91,74 @@ if ($app->run() !== null) {
}
```

### Example of config/bootloaders.php
### Bootloaders discoverer

It will help you to register bootloaders from different sources

#### From composer

Will register bootloaders from application `composer.json` and from other installed composer packages

```php
new \Spiral\Discoverer\Bootloader\ComposerRegistry(),
```

**Package composer.json**

```json
{
// ...
"extra": {
"spiral": {
"bootloaders": [
"Spiral\\Monolog\\Bootloader\\DotenvBootloader",
"Spiral\\DotEnv\\Bootloader\\MonologBootloader"
],
"dont-discover": [
"spiral-packages/event-bus"
]
}
}
}
```

**Application composer.json**

```json
{
// ...
"extra": {
"spiral": {
"dont-discover": [
"spiral-packages/foo",
"spiral-packages/bar"
]
}
}
}
```

#### From array

Will register bootloaders from the passed array

```php
new \Spiral\Discoverer\Bootloader\ArrayRegistry([
// Application specific logs
Bootloader\LoggingBootloader::class,

// ...
]),
```

#### From config

```php
new \Spiral\Discoverer\Bootloader\ConfigRegistry(),
```

```php
// config/discoverer.php
<?php

declare(strict_types=1);
Expand All @@ -116,22 +175,23 @@ return [
Framework\Security\FiltersBootloader::class,
Framework\Security\GuardBootloader::class,
],
'ignorableBootloaders' => [
'ignoredBootloaders' => [
// ...
],
];
```

### Custom registry
#### Custom Bootloader registry

You have the ability to create your custom Registries by implementing `Spiral\BootloadersDiscover\RegistryInterface`
You have the ability to create your custom Registries by
implementing `Spiral\Discoverer\Bootloader\BootloaderRegistryInterface`

```php
use Spiral\BootloadersDiscover\RegistryInterface;
use Spiral\Discoverer\Bootloader\BootloaderRegistryInterface;
use Spiral\Core\Container;
use Spiral\Files\FilesInterface;

final class JsonRegistry implements RegistryInterface
final class JsonRegistry implements BootloaderRegistryInterface
{
private array $bootloaders = [];
private array $ignorableBootloaders = [];
Expand All @@ -144,27 +204,127 @@ final class JsonRegistry implements RegistryInterface
public function init(Container $container): void
{
// json structure
// {"bootloaders": [], "ignorable_bootloaders": []}
// {
// "bootloaders": [
// "Framework\Security\EncrypterBootloader",
// "Framework\Security\GuardBootloader"
// ],
// "ignored_bootloaders": []
//}

$files = $container->get(FilesInterface::class);
$data = \json_decode($files->read($this->jsonPath), true);

$this->bootloaders = $data['bootloaders'] ?? [];
$this->ignorableBootloaders = $data['ignorable_bootloaders'] ?? [];
$this->ignorableBootloaders = $data['ignored_bootloaders'] ?? [];
}

public function getBootloaders(): array
{
return $this->bootloaders;
}

public function getIgnorableBootloaders(): array
public function getIgnoredBootloaders(): array
{
return $this->ignorableBootloaders;
}
}
```

### Tokenizer directories discoverer

It will help you to register Tokenizer directories from different sources

#### From composer

Will register directories from application `composer.json` and from other installed composer packages

**Package composer.json**

```json
{
// ...
"extra": {
"spiral": {
"directories": [
"src/Entities"
]
}
}
}
```

**Application composer.json**

```json
{
// ...
"extra": {
"spiral": {
"directories": {
"self": [
"src/Events"
],
"spiral-package/event-bus": [
"src/Events"
],
"spiral-package/notifications": "src/Events"
}
}
}
}
```

```php
new \Spiral\Discoverer\Tokenizer\ComposerRegistry(),
```

#### Custom Directory registry

You have the ability to create your custom Registries by
implementing `Spiral\Discoverer\Tokenizer\DirectoryRegistryInterface`

```php
use Spiral\Discoverer\Tokenizer\DirectoryRegistryInterface;
use Spiral\Core\Container;
use Spiral\Files\FilesInterface;

final class JsonRegistry implements DirectoryRegistryInterface
{
private array $directories = [];

public function __construct(
private string $jsonPath
) {
}

public function init(Container $container): void
{
// json structure
// {
// "directories": [
// "src/Listeners",
// "src/Entities"
// ]
// }

$root = $container->get(\Spiral\Boot\DirectoriesInterface::class)->get('root');

$files = $container->get(FilesInterface::class);
$data = \json_decode($files->read($this->jsonPath), true);

$this->directories = \array_map(function (string $dir) use($root) {
return $root . $dir;
}, $data['directories'] ?? []);
}

public function getDirectories(): array
{
return $this->directories;
}
}
```

## Testing

```bash
Expand Down
25 changes: 17 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "spiral-packages/discoverer",
"description": "Bootloaders discoverer for Spiral Framework",
"description": "Discoverer for Spiral Framework",
"keywords": [
"spiral-packages",
"spiral",
"bootloaders-discover"
"discoverer"
],
"homepage": "https://github.com/spiral-packages/bootloaders-discover",
"license": "MIT",
Expand All @@ -17,8 +17,7 @@
],
"require": {
"php": "^8.0",
"spiral/boot": "^2.9",
"spiral/files": "^2.9"
"spiral/framework": "^2.9"
},
"require-dev": {
"mockery/mockery": "^1.5",
Expand All @@ -28,21 +27,31 @@
},
"autoload": {
"psr-4": {
"Spiral\\BootloadersDiscover\\": "src"
"Spiral\\Discoverer\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Spiral\\BootloadersDiscover\\Tests\\App\\": "tests/app",
"Spiral\\BootloadersDiscover\\Tests\\": "tests/src"
"Spiral\\Discoverer\\Tests\\App\\": "tests/app",
"Spiral\\Discoverer\\Tests\\": "tests/src"
}
},
"extra": {
"spiral": {
"bootloaders": [
"Spiral\\Discoverer\\DiscovererBootloader"
]
}
},
"scripts": {
"test": "vendor/bin/phpunit",
"psalm": "vendor/bin/psalm --config=psalm.xml ./src"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"spiral/composer-publish-plugin": true
}
},
"minimum-stability": "dev",
"prefer-stable": true
Expand Down
Loading

0 comments on commit e8e120d

Please sign in to comment.