2017 © Pedro Peláez
 

library container

A shrimp of a dependency injection library

image

caridea/container

A shrimp of a dependency injection library

  • Saturday, January 6, 2018
  • by doublecompile
  • Repository
  • 1 Watchers
  • 0 Stars
  • 595 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 13 Versions
  • 1 % Grown

The README.md

caridea-container

Caridea is a miniscule PHP application library. This shrimpy fellow is what you'd use when you just want some helping hands and not a full-blown framework., (*1)

, (*2)

This is its PSR-11 compliant dependency injection container., (*3)

Packagist Build Status Scrutinizer Code Quality Code Coverage, (*4)

Installation

You can install this library using Composer:, (*5)

$ composer require caridea/container
  • The master branch (version 3.x) of this project requires PHP 7.1 and depends on caridea/event.
  • Version 2.x of this project requires PHP 7.0 and depends on caridea/event.
  • Version 1.x of this project requires PHP 5.5 and depends on caridea/event.

Compliance

Releases of this library will conform to Semantic Versioning., (*6)

Our code is intended to comply with PSR-1, PSR-2, and PSR-4. If you find any issues related to standards compliance, please send a pull request!, (*7)

Overview

  • The Caridea\Container\Properties class is intended for scalar configuration values that might be used as settings for other components.
  • The Caridea\Container\Objects class allows for eager, lazy, and prototype objects.
    • It also implements Caridea\Event\Publisher and will broadcast events to any managed object which implements Caridea\Event\Listener.
  • The Caridea\Container\EmptyContainer class is an empty, no-op container.

You can retrieve contained objects both by name and by type!, (*8)

Documentation

Examples

Just a few quick examples., (*9)

Configuration and Dependencies

$config = [
    'db.uri' => 'mongodb://localhost:27017',
    'mail.host' => '192.168.1.100'
];
$properties = new \Caridea\Container\Properties($config);
$objects = \Caridea\Container\Objects::builder()
    ->eager('mongoClient', 'MongoClient', function($c){
        return new \MongoClient($c->get('db.uri'));
    })
    ->lazy('mailService', 'My\Mail\Service', function($c){
        return new \My\Mail\Service($c->('mail.host'));
    })
    ->lazy('userService', 'My\User\Service', function($c){
        return new \My\User\Service($c->get('mongoClient'), $c->get('objectStorage'));
    })
    ->proto('objectStorage', 'SplObjectStorage', function($c){
        return new \SplObjectStorage();
    })
    ->build($properties);

$userService = $objects->get('userService');

Parent Delegation

You can nest Objects containers. For example, you can have a container with service objects and a child container with web controllers., (*10)

$services = \Caridea\Container\Objects::builder()
    ->eager('blogService', 'My\Blog\Service', function($c){
        return new \My\Blog\Service();
    })
    ->build();
$controllers = \Caridea\Container\Objects::builder()
    ->eager('blogController', 'My\Blog\Controller', function($c){
        return new \My\Blog\Controller($c->get('blogService'));
    })
    ->build($services);

$controllers = $controllers->getByType('My\Blog\Controller'); // ['blogController' => BlogController]

Events

$objects = \Caridea\Container\Objects::builder()
    ->eager('eventListener', 'My\Cool\EventListener', function($c){
        // we are assuming that this class implements Caridea\Event\Listener
        return new \My\Cool\EventListener();
    })
    ->build();

// assuming that CustomEvent implements Caridea\Event\Event
$objects->publish(new CustomEvent());
// Here, the eventListener object will have its ->notify() method invoked with the CustomEvent

Any objects returned from an Objects container that implement \Caridea\Event\PublisherAware will receive the container via the setPublisher method., (*11)

ContainerAware

Any objects returned from an Objects container that implement \Caridea\Container\ContainerAware will receive the container via the setContainer method. We provide a trait to make this easier., (*12)

class MyContainerAware implements \Caridea\Container\ContainerAware
{
    use \Caridea\Container\ContainerSetter;

    public function __construct()
    {
        $this->container = new \Caridea\Container\EmptyContainer();
    }
}

The Versions

06/01 2018

dev-master

9999999-dev http://github.com/libreworks/caridea-container

A shrimp of a dependency injection library

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

dependency injection ioc inversion of control di psr-11

06/01 2018

3.0.1

3.0.1.0 http://github.com/libreworks/caridea-container

A shrimp of a dependency injection library

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

dependency injection ioc inversion of control di psr-11

02/01 2018

3.0.0

3.0.0.0 http://github.com/libreworks/caridea-container

A shrimp of a dependency injection library

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

dependency injection ioc inversion of control di psr-11

23/11 2016

2.2.1

2.2.1.0 http://github.com/libreworks/caridea-container

A shrimp of a dependency injection library

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

dependency injection ioc inversion of control di

23/11 2016

2.2.x-dev

2.2.9999999.9999999-dev http://github.com/libreworks/caridea-container

A shrimp of a dependency injection library

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

dependency injection ioc inversion of control di

19/09 2016

2.2.0

2.2.0.0 http://github.com/libreworks/caridea-container

A shrimp of a dependency injection library

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

dependency injection ioc inversion of control di

28/03 2016

2.1.0

2.1.0.0 http://github.com/libreworks/caridea-container

A shrimp of a dependency injection library

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

dependency injection ioc inversion of control di

28/03 2016

2.1.x-dev

2.1.9999999.9999999-dev http://github.com/libreworks/caridea-container

A shrimp of a dependency injection library

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

dependency injection ioc inversion of control di

22/03 2016

2.0.0

2.0.0.0 http://github.com/libreworks/caridea-container

A shrimp of a dependency injection library

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

dependency injection ioc inversion of control di

22/03 2016

2.0.x-dev

2.0.9999999.9999999-dev http://github.com/libreworks/caridea-container

A shrimp of a dependency injection library

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

dependency injection ioc inversion of control di

21/03 2016

1.x-dev

1.9999999.9999999.9999999-dev http://github.com/libreworks/caridea-container

A shrimp of a dependency injection library

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

dependency injection ioc inversion of control di

21/03 2016

1.0.0

1.0.0.0 http://github.com/libreworks/caridea-container

A shrimp of a dependency injection library

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

dependency injection ioc inversion of control di

03/06 2015

0.1.0

0.1.0.0 http://github.com/libreworks/caridea-container

A shrimp of a dependency injection library

  Sources   Download

Apache-2.0

The Requires

 

dependency injection ioc inversion of control di