An open API service indexing awesome lists of open source software.

https://github.com/libreworks/caridea-container

:fried_shrimp: Caridea is a miniscule PHP application library. This is a shrimpy dependency injection container.
https://github.com/libreworks/caridea-container

Last synced: 11 months ago
JSON representation

:fried_shrimp: Caridea is a miniscule PHP application library. This is a shrimpy dependency injection container.

Awesome Lists containing this project

README

          

# 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.

![](http://libreworks.com/caridea-100.png)

This is its [PSR-11](http://www.php-fig.org/psr/psr-11/) compliant dependency injection container.

[![Packagist](https://img.shields.io/packagist/v/caridea/container.svg)](https://packagist.org/packages/caridea/container)
[![Build Status](https://travis-ci.org/libreworks/caridea-container.svg)](https://travis-ci.org/libreworks/caridea-container)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/libreworks/caridea-container/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/libreworks/caridea-container/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/libreworks/caridea-container/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/libreworks/caridea-container/?branch=master)

## Installation

You can install this library using Composer:

```console
$ 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](http://semver.org).

Our code is intended to comply with [PSR-1](http://www.php-fig.org/psr/psr-1/), [PSR-2](http://www.php-fig.org/psr/psr-2/), and [PSR-4](http://www.php-fig.org/psr/psr-4/). If you find any issues related to standards compliance, please send a pull request!

## 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!

## Documentation

* Head over to [Read the Docs](http://caridea-container.readthedocs.io/en/latest/)

## Examples

Just a few quick examples.

### Configuration and Dependencies
```php
$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.

```php
$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

```php
$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.

### 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.

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

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