bnf/slim-interop-service-provider

container-interop/service-provider compatible service provider for Slim

4.2.0 2019-08-14 08:04 UTC

This package is auto-updated.

Last update: 2024-04-14 19:19:47 UTC


README

Provides container-interop/service-provider support for Slim.

Installation

$ composer require bnf/slim-interop-service-provider:^4.0

Usage

Add Bnf\SlimInterop\ServiceProvider to the list of service providers to register the default Slim services.

Specify it as first service provider to be able to overwrite or extend default services.

new Container([
    new \Bnf\SlimInterop\ServiceProvider,
    new YouServiceProvider,
]);

Example

Example usage with a container-interop/service-provider compatible container bnf/di.

$ composer require bnf/slim-interop-service-provider:^4.0 bnf/di:~0.1.0 slim/slim:^4.0 slim/psr7:~0.4.0
<?php
declare(strict_types = 1);
require 'vendor/autoload.php';

use Bnf\Di\Container;
use Bnf\SlimInterop\ServiceProvider as SlimServiceProvider;
use Psr\Container\ContainerInterface;
use Interop\Container\ServiceProviderInterface;
use Slim\App;

class Index
{
    public function __invoke($request, $response) {
        $response->getBody()->write('Hello World.');
        return $response;
    }
}

$container = new Container([
    // Register default slim services
    new SlimServiceProvider,

    // Register own services and configuration
    new class implements ServiceProviderInterface {
        public function getFactories(): array
        {
            return [
                'slim.display_error_details' => function(): bool {
                    return true;
                },
                Index::class => function (ContainerInterface $container): Index {
                    return new Index;
                }
            ];
        }
        public function getExtensions(): array
        {
            return [
                // configure app route and middlewares as extension to the App class
                App::class => function (ContainerInterface $container, App $app): App {
                    $app->add(\Slim\Middleware\ErrorMiddleware::class);

                    $app->get('/', Index::class);

                    return $app;
                },
            ];
        }
    }
]);

$app = $container->get(App::class);
$app->run();