jerowork/slim-route-attribute-provider

Define Slim routes by PHP8 attributes

0.7.0 2024-05-28 14:02 UTC

This package is auto-updated.

Last update: 2024-12-01 00:08:22 UTC


README

Build Status Coverage Status Quality Score Software License Packagist Version PHP Version

Define Slim routes by PHP8 attributes.

Installation

Install via Composer:

$ composer require jerowork/slim-route-attribute-provider

Configuration

Instantiate RouteAttributeConfigurator somewhere close to the construction of your Slim application, e.g. in your front controller (or ideally register in your PSR-11 container).

Basic configuration:

use Jerowork\RouteAttributeProvider\RouteAttributeConfigurator;
use Jerowork\RouteAttributeProvider\Slim\SlimRouteAttributeProvider;
use Slim\Factory\AppFactory;

// Setup a (fictive) PSR-11 container and create Slim application
$container = new Container();
$app       = AppFactory::createFromContainer($container);

// ...

// Setup route attribute configuration
$routeConfigurator = new RouteAttributeConfigurator(
    SlimRouteAttributeProvider::createFromApp($app)
);

$routeConfigurator
    ->addDirectory(sprintf('%s/src/Infrastructure/Api/Http/Action', __DIR__))
    ->configure();

// ...

// Run Slim application
$app->run();

Extended configuration:

use Jerowork\FileClassReflector\FileFinder\RegexIterator\RegexIteratorFileFinder;
use Jerowork\FileClassReflector\NikicParser\NikicParserClassReflectorFactory;
use Jerowork\RouteAttributeProvider\RouteAttributeConfigurator;
use Jerowork\RouteAttributeProvider\Slim\SlimRouteAttributeProvider;
use PhpParser\NodeTraverser;
use PhpParser\ParserFactory;

// ...

// All parts of the configurator can be replaced with a custom implementation
$routeConfigurator = new RouteAttributeConfigurator(
    new SlimRouteAttributeProvider(
        $app->getRouteCollector(),
        $container
    ),
    new ClassReflectorRouteLoader(
        new NikicParserClassReflectorFactory(
            new RegexIteratorFileFinder(),
            (new ParserFactory())->create(ParserFactory::PREFER_PHP7),
            new NodeTraverser()
        )
    )
);

// Multiple directories can be defined
$routeConfigurator
    ->addDirectory(
        sprintf('%s/src/Infrastructure/Api/Http/Action', __DIR__),
        sprintf('%s/src/Other/Controller', __DIR__)
    )
    ->configure();

// ...

Usage

See jerowork/route-attribute-provider for examples.