2017 © Pedro Peláez
 

library container

Yet another service container

image

artoodetoo/container

Yet another service container

  • Sunday, January 29, 2017
  • by artoodetoo
  • Repository
  • 1 Watchers
  • 1 Stars
  • 21 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Container

Yet another service container, (*1)

Container DOES NOT use reflection and type hint information in any form., (*2)

Features

Container keep two kind of values: services and parameters., (*3)

Service will instantiated when you call call it by name. Usually service is object type, but not nesessary. Mind it as "product" of some routine., (*4)

Methods: get() and set()., (*5)

Parameters are not instantiating, they just stored and retrieved., (*6)

Methods: getParameter() and setParameter()., (*7)

Install

To install with composer:, (*8)

composer require artoodetoo/container

Basic Usage

Configuration

There are special configuraton sections:, (*9)

  • root used to store basic values, which can be substituted into service definition.
  • shared node defines shared services. after it created once it can be retrieved many times.
  • multiple node defines new instance created on every get() call

In other sections you can store any kind of information and retrieve it in dot notation (see below)., (*10)

Simple service definition

use R2\DependencyInjection\Container;

$config = [
  'shared' => [
    'view' => R2\Templating\Dirk::class
  ]
];
$c = new Container($config);
$c->get('view')->render('index');

Parameters substitution

$config = [
  'ROOT'   => '\var\www\mysite',
  'PUBLIC' => '\var\www\mysite\public',
  'shared' => [
    'view' => [
      'class' => R2\Templating\Dirk::class,
      'options' => [
          'views' => '%ROOT%/views',
          'cache' => '%ROOT%/cache',
      ],
      ...
  ]
];
...
$c->get('view')->render('index');

Factory method

$config = [
  'shared' => [
    'userManager' => App\UserManager::class,
    'user' => '@userManager:getCurrentUser',
    ...
  ]
]
...
echo $c->get('user')->username;

Container injection

There are two ways to inject container into service instance:
1. Add interface R2\DependencyInjection\ContainerAwareInterface to service class and define setContainer() method. 2. Substitute container in constructor parameters. It is special name CONTAINER:, (*11)

$config = [
  'shared' => [
    'example' => [
        'class' => Example::class,
        'container' => '%CONTAINER%',
    ],
  ]
];

Parameters and dot notation:

$config = [
  'options' => [
    'cookie' => [
      'name' => 'the-cookie',
      'domain' => '.example.com'
    ]
];
...
setcookie(
  $c->getParameter('options.cookie.name'),
  $value,
  0,
  '/',
  $c->getParameter('options.cookie.domain')
);

Notes and Limits

Any part of configuration can be read by getParameter, including special sections shared and multiple., (*12)

As for now, substitution patterns work in service production only., (*13)

Only parameters from config root can be used in substitution patterns., (*14)

License

The Container is open-source software, licensed under the MIT license, (*15)

The Versions

29/01 2017

dev-master

9999999-dev

Yet another service container

  Sources   Download

MIT

The Requires

  • php >=5.6

 

by Avatar artoodetoo

service container

27/01 2017

v0.1.3

0.1.3.0

Yet another service container

  Sources   Download

MIT

The Requires

  • php >=5.6

 

by Avatar artoodetoo

service container

17/12 2016

v0.1.2

0.1.2.0

Yet another service container

  Sources   Download

MIT

The Requires

  • php >=5.6

 

by Avatar artoodetoo

service container

02/04 2016

v0.1.1

0.1.1.0

Yet another service container

  Sources   Download

MIT

The Requires

  • php >=5.6

 

by Avatar artoodetoo

service container

01/04 2016

v0.1.0

0.1.0.0

Yet another service container

  Sources   Download

MIT

The Requires

  • php >=5.6

 

by Avatar artoodetoo

service container