dakujem/slim-factory

A tiny tool to help you bootstrap a Slim v4 app.

1.0 2020-11-10 10:05 UTC

This package is auto-updated.

Last update: 2024-11-29 10:42:32 UTC


README

PHP from Packagist Test Suite Coverage Status

💿 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.