jerowork / slim-route-attribute-provider
Define Slim routes by PHP8 attributes
0.7.0
2024-05-28 14:02 UTC
Requires
- php: ^8.1
- jerowork/route-attribute-provider: ^0.7
- psr/container: ^1.0 || ^2.0
- slim/slim: ^4.8
Requires (Dev)
- ergebnis/composer-normalize: ^2.15
- friendsofphp/php-cs-fixer: ^3.0
- mockery/mockery: ^1.4
- phpmd/phpmd: ^2.10
- phpro/grumphp-shim: ^1.4
- phpstan/phpstan: ^1.8
- phpunit/phpunit: ^9.5
- scrutinizer/ocular: ^1.8
- squizlabs/php_codesniffer: ^3.6
- symfony/cache: ^5.3 || ^6.0
Provides
README
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.