2017 © Pedro Peláez
 

library php-easy-container

an easy composer

image

johnroyer/php-easy-container

an easy composer

  • Saturday, February 17, 2018
  • by johnroyer
  • Repository
  • 1 Watchers
  • 0 Stars
  • 24 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 11 Versions
  • 4 % Grown

The README.md

PHP-Easy-Container

Build Status Coverage Status, (*1)

An implement for singleton and some features., (*2)

Requirement

  • PHP >= 5.6

Installation

using composer:, (*3)

composer require johnroyer/php-easy-container

Simple Usage

Put whatever you want into container:, (*4)

use \Zeroplex\Container\Container;

$c = new Container;

// set value as singleton
$c['pi'] = 3.14159;
$c['db'] = new PDO(
    'mysql:dbname=mydb;host=localhost',
    'user',
    'password'
);

Get things out from container:, (*5)

$c['pi'];  // 3.14159
$c['db'];  // object(PDO)

Singleton

If you use ArrayAccess interfaces to access the data from container, container will treat them as sinleton, as example ablove., (*6)

You can also use method singleton() to put data into container:, (*7)

$c->singleton('db') = new PDO(
    'mysql:dbname=mydb;host=localhost',
    'user',
    'password'
);

$c->get('db');  // object(PDO)

You can also put a global variable into container:, (*8)

$c->singleton('config') = new Config(
    APP_ROOT . '/config'
);

$c['config']['app.name'];

Late initialize

Sometimes, you are not sure if an DB connection should initialize while program start up. Maybe connect to DB when it is really needed is a better idea., (*9)

Use a Closure to let container known you want to initialize it later:, (*10)

$c['db'] = function () {
    return new PDO(
        'mysql:dbname=test;host=localhost',
        'user',
        'password'
    );
};

$c->get('db');  // new PDO if and only if someone get it
                // object(PDO)

Provide

Container can contains things with states, can also contains things stateless. provide() method stores bussiness logic or workflow which is stateless., (*11)

$c->provide('pi', 3.14159265359);
$c['pi'];  // 3.14159265359

$c->provide('nowDate', function() {
    // always return special format of date
    return (new DateTime())
        ->format('Y-m-d');
});
$c->['nowDate'];  // "2017-12-01"

The Versions

17/02 2018

dev-master

9999999-dev

an easy composer

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by zero

15/12 2017

v1.0.1

1.0.1.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by zero

09/12 2017

v1.0.0

1.0.0.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by zero

09/12 2017

dev-1.0-rc

dev-1.0-rc

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by zero

09/12 2017

dev-docs

dev-docs

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by zero

27/10 2017

dev-config-fix

dev-config-fix

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by zero

27/10 2017

5.6.x-dev

5.6.9999999.9999999-dev

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by zero

02/10 2017

v0.10.0

0.10.0.0

  Sources   Download

MIT

The Development Requires

by zero

01/10 2017

v0.9.1

0.9.1.0

  Sources   Download

MIT

The Development Requires

by zero

01/10 2017

v0.9.0

0.9.0.0

  Sources   Download

BSD

The Development Requires

by zero

19/09 2017

dev-add-license-1

dev-add-license-1

  Sources   Download

BSD

The Development Requires

by zero