PHP code example of container-interop / service-provider

1. Go to this page and download the library: Download container-interop/service-provider library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

container-interop / service-provider example snippets


use Psr\Container\ServiceProviderInterface;

class MyServiceProvider implements ServiceProviderInterface
{
    public function getFactories()
    {
        return [
            'my_service' => function(ContainerInterface $container) {
                $dependency = $container->get('my_other_service');
                return new MyService($dependency);
            }
        ];
    }
    
    public function getExtensions()
    {
        return [
            'my_extended_service' => function(ContainerInterface $container, $extendedService) {
                $extendedService->registerExtension($container->get('my_service'));
                return $extendedService;
            }
        ];
    }
}

class MyServiceProvider implements ServiceProviderInterface
{
    public function getFactories()
    {
        return [
            'my_service' => fn() => new MyService(),
            'my_alias' => fn(ContainerInterface $container) => $container->get('my_service'),
        ];
    }

    // ...
}

class A implements ServiceProviderInterface
{
    public function getFactories()
    {
        return [
            'foo' => fn() => 'abc',
        ];
    }

    // ...
}

class B implements ServiceProviderInterface
{
    public function getFactories()
    {
        return [
            'foo' => fn() => 'def',
        ];
    }

    // ...
}

class A implements ServiceProviderInterface
{
    public function getFactories()
    {
        return [
            'logger' => fn() => new MyLogger(),
        ];
    }
    
    // ...
}

class B implements ServiceProviderInterface
{
    public function getExtensions()
    {
        return [
            'logger' => function (ContainerInterface $container, MyLogger $logger) {
                $logger->addHandler(new MyHandler());

                return $logger;
            },
        ];
    }

    // ...
}

function (ContainerInterface $container, MyLogger $logger)

function (ContainerInterface $container, ?MyLogger $logger)

class B implements ServiceProviderInterface
{
    public function getExtensions()
    {
        return [
            'logger' => function (ContainerInterface $container, ?MyLogger $logger) {
                if ($logger) {
                    $logger->addHandler(new MyHandler());
                }

                return $logger; // if `logger` is `null`, the extension will simply return `null`
            },
        ];
    }

    // ...
}

class MyServiceProvider implements ServiceProviderInterface
{
    public function getFactories()
    {
        return [
            'logger' => [ MyServiceProvider::class, 'createLogger' ],
        ];
    }
    
    public function getExtensions()
    {
        return [];
    }

    public static function createLogger(ContainerInterface $container)
    {
        // The path to the log file is fetched from the container, not from the service provider state.
        return new FileLogger($this->container->get('logFilePath'));
    }
}