ddrv/container

Simple PSR-11 Container

Fund package maintenance!
Other

v2.1.0 2023-12-12 19:20 UTC

This package is auto-updated.

Last update: 2024-11-12 21:18:23 UTC


README

Latest Stable Version Total Downloads License PHP

ddrv/container

Simple PSR-11 Container

Install

composer require ddrv/container:^2.1

Using

<?php

use Ddrv\Container\Container;
use Psr\Container\ContainerInterface;

$container = new Container();

/*
 * Set value as is
 */
$container->value('var', 'value1');
$container->get('var'); // returns 'value1'

/*
 * Set factory for service
 */
$container->service('service', function(ContainerInterface $container) {
    $service = new stdClass();
    $service->var = $container->get('var');
    return $service;
});
$service1 = $container->get('service');
$service2 = $container->get('service');
$equal = $service1 === $service2; // true
$service1->var; // 'value1'
$service2->var; // 'value1'
$service1->var = 'value2';
$service1->var; // 'value2'
$service2->var; // 'value2'

/*
 * Set factory for recreated instance
 */
$container->instance('instance', function(ContainerInterface $container) {
    $instance = new stdClass();
    $instance->var = $container->get('var');
    return $instance;
});
$instance1 = $container->get('instance');
$instance2 = $container->get('instance');
$equal = $service1 === $service2; // false
$instance1->var; // 'value1'
$instance2->var; // 'value1'
$instance1->var = 'value2';
$instance1->var; // 'value2'
$instance2->var; // 'value1'

/*
 * Aliasing
 */
$container->service('some-service', function() {
    return new ArrayObject();
});
$container->bind('alias', 'some-service');
$service = $container->get('alias');

/*
 * Delegating to other containers
 */

/** @var ContainerInterface $delegate1 */
/** @var ContainerInterface $delegate2 */
$delegate1->has('key-1'); // true
$delegate1->has('key-2'); // false

$delegate2->has('key-1'); // false
$delegate2->has('key-2'); // true

$container->has('key-1'); // false
$container->has('key-2'); // false

$container->delegate($delegate1);
$container->has('key-1'); // true
$container->has('key-2'); // false

$container->delegate($delegate2);
$container->has('key-1'); // true
$container->has('key-2'); // true