PHP code example of pine3ree / pine3ree-mezzio-pimple-container

1. Go to this page and download the library: Download pine3ree/pine3ree-mezzio-pimple-container 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/ */

    

pine3ree / pine3ree-mezzio-pimple-container example snippets




use pine3ree\Mezzio\Pimple\ContainerFactory;

$dependencies = [
    'services'          => [], // Resolved objects/services
    'invokables'        => [], // Simple constructor-less classes
    'factories'         => [], // Callable factories or callable factory classes for complex objects
    'aliases'           => [], // Aliases for other services
    'delegators'        => [], // Delegator factories (callables or classes indexed by sevice-id/class-string)
    'extensions'        => [], // Pimple-like extension factories (callables or classes indexed by sevice-id/class-string)
    'shared'            => [], // Per-class overrides of the default sharing mode
    'shared_by_default' => true, // Optional, defaults to TRUE if omitted
];

$factory = new ContainerFactory();
$container = $factory($dependencies); // Psr\Container\ContainerInterface|Pimple\Psr11\Container



$config = [
    // Configuration key related to the container
    'dependencies' => [
        'services'          => [], // Resolved objects/services
        'invokables'        => [], // Simple constructor-less classes
        'factories'         => [], // Callable factories or callable factory classes for complex objects
        'aliases'           => [], // Aliases for other services
        'delegators'        => [], // Delegator factories (callables or classes indexed by sevice-id/class-string)
        'extensions'        => [], // Pimple-like extension factories (callables or classes indexed by sevice-id/class-string)
        'shared'            => [], // Per-class overrides of the default sharing mode
        'shared_by_default' => true, // Defaults to `true` if omitted
    ],
    // ... other configuration
];

$factory = new ContainerFactory();
$container = $factory($config['dependencies'], $config);

> $config = $container->get('config');
> 

use Psr\Container\ContainerInterface;

public function __invoke(
    $service,
    ContainerInterface $container,
    $name
);



use App\Service\MyService; // implements MyServiceInterface
use App\Service\MyServiceInterface;
use Psr\Container\ContainerInterface;

class ExtensionFactory
{
    public function __invoke(MyService $service, ContainerInterface $container, $name): MyService
    {
        // do something with $service

        return $service;
    }
}

use App\Service\MyService; // implements MyServiceInterface
use App\Service\MyServiceDecorator; // implements MyServiceInterface
use App\Service\MyServiceInterface;
use Psr\Container\ContainerInterface;

class ExtensionFactory
{
    public function __invoke(MyServiceInterface $service, ContainerInterface $container, $name): MyServiceInterface
    {
        return new MyServiceDecorator($service);
    }
}

$dependencies = [
    'invokables' => [
        'my-service' => MyInvokable\Service::class,
    ],
    'extensions' => [
        'my-service' => [
            Extension1Factory::class,
            Extension2Factory::class,
            // ...
        ],
    ],
];


// file: config/container.php

use pine3ree\Mezzio\Pimple\ContainerFactory;

$config  =