2017 © Pedro Peláez
 

library acne

Simple DI Container for PHP < 5.3

image

acne/acne

Simple DI Container for PHP < 5.3

  • Sunday, September 2, 2012
  • by yuya-takeyama
  • Repository
  • 2 Watchers
  • 3 Stars
  • 17 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Acne

Acne is simple DI Container for PHP < 5.2, (*1)

Heavily inspired by fabpot/Pimple, (*2)

Usage

Defining parameters

You can use Acne_Container like an associative array., (*3)

Just set value with key., (*4)

<?php
$container = new Acne_Container;

$container['session.cookie_name'] = 'PHPSESSID';

Defining service providers

Service provider is factory which constructs an object., (*5)

Closure can be a service provider and it is the most prefferable way for PHP >= 5.3, (*6)

<?php
$container['session'] = function ($c) {
    return new Session($c['session.cookie_name']);
};
$container['session.cookie_name'] = 'PHPSESSID';

But if you're using PHP < 5.3, you can't use Closure., (*7)

This is why I create Acne., (*8)

Also the other callable values can be set as service provider.
But notice that string can't be a service provider., (*9)

<?php
class ServiceProviderCollection
{
    public function provideSession($c)
    {
        return new Session($c['session.cookie_name']);
    }
}

$providerCollection = new ServiceProviderCollection;

$container['session'] = array($providerCollection, 'provideSession');
$container['session.cookie_name'] = 'PHPSESSID';

Getting object from container

After you defined service provider, you can get object which the provider constructed., (*10)

<?php
$session = $container['session'];
$name = $session->get('name');

Defining shared service providers

If you want to share objects like Singleton pattern, you can use shared service provider., (*11)

Shared service provider constructs object only once., (*12)

<?php
$container->share('session', function ($c) {
    return new Session($c['session.cookie_name']);
});

Pimple-like way is also available., (*13)

<?php
$container['session'] = $container->share(function ($c) {
    return new Session($c['session.cookie_name']);
});

Author

Yuya Takeyama, (*14)

The Versions

02/09 2012

dev-develop

dev-develop

Simple DI Container for PHP < 5.3

  Sources   Download

MIT

The Requires

  • php >=5.2.14

 

di container

02/09 2012

dev-master

9999999-dev

Simple DI Container for PHP < 5.3

  Sources   Download

MIT

The Requires

  • php >=5.2.14

 

di container

14/07 2012

0.0.0

0.0.0.0

Simple DI Container for PHP < 5.3

  Sources   Download

MIT

The Requires

  • php >=5.2.14

 

di container