Code Monkey home page Code Monkey logo

discoverer's Introduction

Discoverer for Spiral Framework

PHP Version Require Latest Stable Version phpunit psalm Total Downloads

Requirements

Make sure that your server is configured with following PHP version and extensions:

  • PHP 8.0+
  • Spiral framework 2.9+

Installation

You can install the package via composer:

composer require spiral-packages/discoverer

After package install you need to register bootloader from the package.

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.

use Spiral\Discoverer\WithDiscovering;
use Spiral\Framework\Kernel;

class App extends Kernel
{
    use WithDiscovering;
}

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

<?php

declare(strict_types=1);

use App\App;

//
// If you forgot to configure some of this in your php.ini file,
// then don't worry, we will set the standard environment
// settings for you.
//

mb_internal_encoding('UTF-8');
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 'stderr');

//
// Register Composer's auto loader.
//
require __DIR__ . '/vendor/autoload.php';


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

$app->discover(
    // Bootloaders sources
    new \Spiral\Discoverer\Bootloader\BootloadersDiscoverer(
        new \Spiral\Discoverer\Bootloader\ComposerRegistry(),
        new \Spiral\Discoverer\Bootloader\ArrayRegistry(...),
        new \Spiral\Discoverer\Bootloader\ConfigRegistry()
    ),

    // Tokenizer directories
    new \Spiral\Discoverer\Tokenizer\DirectoriesDiscoverer(
        new \Spiral\Discoverer\Tokenizer\ComposerRegistry(),
    )
);

if ($app->run() !== null) {
    $code = (int)$app->serve();
    exit($code);
}

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

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

Package composer.json

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

Application composer.json

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

From array

Will register bootloaders from the passed array

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

    // ...
]),

From config

new \Spiral\Discoverer\Bootloader\ConfigRegistry(),
// config/discoverer.php
<?php

declare(strict_types=1);

return [
    'bootloaders' => [
        // Core Services
        Framework\SnapshotsBootloader::class,
        Framework\I18nBootloader::class,

        // Security and validation
        Framework\Security\EncrypterBootloader::class,
        Framework\Security\ValidationBootloader::class,
        Framework\Security\FiltersBootloader::class,
        Framework\Security\GuardBootloader::class,
    ],
    'ignoredBootloaders' => [
        // ...
    ],
];

Custom Bootloader registry

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

use Spiral\Discoverer\Bootloader\BootloaderRegistryInterface;
use Spiral\Core\Container;
use Spiral\Files\FilesInterface;

final class JsonRegistry implements BootloaderRegistryInterface
{
    private array $bootloaders = [];
    private array $ignorableBootloaders = [];

    public function __construct(
        private string $jsonPath
    ) {
    }

    public function init(Container $container): void
    {
        // json structure
        // {
        //    "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['ignored_bootloaders'] ?? [];
    }

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

    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

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

Application composer.json

{
  // ...
  "extra": {
    "spiral": {
      "directories": {
        "self": [
          "src/Events"
        ],
        "spiral-package/event-bus": [
          "src/Events"
        ],
        "spiral-package/notifications": "src/Events"
      }
    }
  }
}
new \Spiral\Discoverer\Tokenizer\ComposerRegistry(),

Custom Directory registry

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

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

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

discoverer's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.