2017 © Pedro Peláez
 

library container

Irfan's Container: An abstraction for many types of containers with a single API.

image

irfantoor/container

Irfan's Container: An abstraction for many types of containers with a single API.

  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 15 Versions
  • 9 % Grown

The README.md

IrfanTOOR\Container

Note: this Readme will be updated shortly, it reflects the functionality of v1.0.4, (*1)

Container

Container implementing Psr\ContainerInterface, ArrayAccess, Countable and IteratorAggregate., (*2)

The identifiers can use dot notation to access an identifier down a hierarchical level, e.g. to access $container['environment']['headers']['host'] you can code in doted notation as: $config['environment.headers.host'], (*3)

Initializing

You can initialize by passing an array of key value pairs while creating a new instance, it will use array in memory for this case., (*4)

<?php

use IrfanTOOR\Container;

$container = new IrfanTOOR\Container(
  # id => values
  'app' => [
    'name'    => 'My App'
    'version' => '1.1',
  ]
);

Setting

You can by set a service in the Container by using the method 'set':, (*5)

use IrfanTOOR\Container;

$container = new IrfanTOOR\Container();

# setting hello => world
$container->set('hello', 'world');

# using an array notation
$container->set(['hello' => 'world']);

# defining multiple
$container->set([
  'hello'     => 'world',
  'box'       => 'empty',
  'something' => null,
  'array'     => [
    'action'       => 'go',
    'step'         => 1,
  ],
]);

# defining sub values
$container->set('app.name', 'Another App');
$container->set('debug.level', 2);

# defining a factory service
$container->factory('hash', function(){
    $salt = rand(0, 10000);
    return md5($salt . time());
});

using array access mechanism:, (*6)

$container['hello'] = 'world';
$container['debug.log.file'] = '/my/debug/log/file.log';

Getting

You can get the stored value or the result from a service in the container by its identifier using the method 'get':, (*7)

# returns a random hash
$hash = $container->get('hash');

you can also use the array access:, (*8)

$hash = $container['hash'];

Checking if a value is present in the container

You can use the method 'has' to check if the container has an entry identified with the identifier id:, (*9)

if ($container->has('hash')) {
  # this will be executed even if the given identifier has the value NULL, 0
  # or false
  echo 'random hash : ' . $container->get('hash');
}

using the array access the above code will become:, (*10)

if (isset($container['hash']) {
  # this will be executed even if the given identifier has the value NULL, 0
  # or false
  echo 'random hash : ' . $container['hash'];
}

Removing an entry

You can use the method 'remove' or unset on the element:, (*11)

# using method remove
$container->remove('hash');

# using unset
unset($container['hash']);

Container to Array

The method 'toArray' can be used to convert the container into an array:, (*12)

$array = $container->toArray();

Array of service identifiers

The array of services identifiers can be retrieved by using the method 'keys':, (*13)

$services = $container->keys();

Count

The number of items present in the container can be retrieved using the method 'count'. Note that it will return the count of the items at base level., (*14)

# will return 1 for the Collection defined in initialization section
$count = $container->count();

Iteration

The container can directly be used in a foreach loop or wherever an iterator is used. for example the code:, (*15)

foreach($container->toArray() as $k => $v)
{
    $v->close();
}

can be simplified as:, (*16)

foreach($container as $k => $v)
{
    $v->close();
}

The Versions

04/01 2018

dev-master

9999999-dev

Irfan's Container: An abstraction for many types of containers with a single API.

  Sources   Download

MIT

The Requires

 

The Development Requires

file container config array abstraction case-insensitive

04/01 2018

v0.9.1

0.9.1.0

Irfan's Container: An abstraction for many types of containers with a single API.

  Sources   Download

MIT

The Requires

 

The Development Requires

file container config array abstraction case-insensitive

25/12 2017

v0.9

0.9.0.0

Irfan's Container: An abstraction for many types of containers with a single API.

  Sources   Download

MIT

The Requires

 

The Development Requires

file container config array abstraction case-insensitive

13/10 2017

v0.8

0.8.0.0

Irfan's Container: An abstraction for many types of containers with a single API.

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

file container config array abstraction case-insensitive

30/05 2017

v0.7.3

0.7.3.0

Irfan's Container: An abstraction for many types of containers with a single API.

  Sources   Download

MIT

The Requires

 

The Development Requires

file container config array abstraction case-insensitive

29/05 2017

v0.7.2

0.7.2.0

Irfan's Container: An abstraction for many types of containers with a single API.

  Sources   Download

MIT

The Requires

 

The Development Requires

file container config array abstraction case-insensitive

15/05 2017

v0.7.1

0.7.1.0

Irfan's Container: An abstraction for many types of containers with a single API.

  Sources   Download

MIT

The Requires

 

The Development Requires

file container config array abstraction case-insensitive

11/05 2017

v0.7

0.7.0.0

Irfan's Container: An abstraction for many types of containers with a single API.

  Sources   Download

MIT

The Requires

 

The Development Requires

file container config array abstraction case-insensitive

10/05 2017

v0.6

0.6.0.0

Irfan's Container: An abstraction for many types of containers with a single API.

  Sources   Download

MIT

The Requires

 

The Development Requires

file container config array abstraction case-insensitive

02/05 2017

v0.5.1

0.5.1.0

Irfan's Container: An abstraction for many types of containers with a single API.

  Sources   Download

MIT

The Requires

 

The Development Requires

file container config array abstraction case-insensitive

01/05 2017

v0.5

0.5.0.0

Irfan's Container: An abstraction for many types of containers with a single API.

  Sources   Download

MIT

The Requires

 

The Development Requires

file container config array abstraction case-insensitive

01/05 2017

v0.4

0.4.0.0

Irfan's Container: An abstraction for many types of containers with a single API.

  Sources   Download

MIT

The Requires

 

The Development Requires

file container config array abstraction case-insensitive

01/05 2017

v0.3

0.3.0.0

Irfan's Container: An abstraction for many types of containers with a single API.

  Sources   Download

MIT

The Requires

 

The Development Requires

file container config array abstraction case-insensitive

01/05 2017

v0.2

0.2.0.0

Irfan's Container: An abstraction for many types of containers with a single API.

  Sources   Download

MIT

The Requires

 

The Development Requires

file container config array abstraction case-insensitive

01/05 2017

v0.1

0.1.0.0

Irfan's Container: An abstraction for many types of containers with a single API.

  Sources   Download

MIT

The Requires

 

The Development Requires

file container config array abstraction case-insensitive