dakujem / slim-factory
A tiny tool to help you bootstrap a Slim v4 app.
Installs: 4 660
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^7.2 || ^8.0
- psr/container: ^1
- slim/slim: ^4
Requires (Dev)
- ext-json: *
- dakujem/sleeve: ^1.0
- nette/tester: ^2.3
- slim/psr7: ^1.2
This package is auto-updated.
Last update: 2024-11-29 10:42:32 UTC
README
💿
composer require dakujem/slim-factory
Usage
use Dakujem\Slim\SlimFactory;
Build an App instance, optionally provide decorators and/or a container for the App instance:
$app = SlimFactory::build(); $app = SlimFactory::build($decorators); $app = SlimFactory::build($decorators, $container);
Or, build an App instance using core services from a container, with optional decorators:
$app = SlimFactory::buildFromContainer($container); $app = SlimFactory::buildFromContainer($container, $decorators);
In case you already have an instance od Slim App
, your decorators can be used to decorate it:
$app = Slim\Factory\AppFactory::create( ... ); SlimFactory::decorate($app, $decorators);
A decorator may be:
- an instance of
AppDecoratorInterface
implementation - a string name of such a class
- a callable provider of such an instance **
- a callable that directly decorates the slim app instance **
class MiddlewareDecorator implements AppDecoratorInterface { public function decorate(App $slim): void { $slim->addRoutingMiddleware(); $slim->addBodyParsingMiddleware(); $slim->addErrorMiddleware(); } } // The following 4 decorators are equivalent: $decorators = [ new MiddlewareDecorator(), // a decorator instance MiddlewareDecorator::class, // a class name fn() => new MiddlewareDecorator(), // a decorator provider function(App $slim): void { // a callable decorator $slim->addRoutingMiddleware(); $slim->addBodyParsingMiddleware(); $slim->addErrorMiddleware(); } ];
** Note
If a callable is used as a decorator, it is always invoked, regardless of its signature.
If the callable returns an instance of a decorator (an implementation of
AppDecoratorInterface
), the returned decorator is immediately applied too.The callables receive the instance of the App being decorated as the first argument, which is the same signature as the one of
AppDecoratorInterface::decorate
method.
Testing
Run unit tests using the following command:
$
composer test
Contributing
Ideas, feature requests and other contribution is welcome. Please send a PR or create an issue.