caridea / container
A shrimp of a dependency injection library
Requires
- php: >=7.1.0
- caridea/event: ^3.0.0
- psr/container: ^1.0
Requires (Dev)
- phpunit/phpunit: ^6.0.0
Provides
- psr/container-implementation: 1.0.0
README
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.
This is its PSR-11 compliant dependency injection container.
Installation
You can install this library using Composer:
$ 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.
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!
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 implementsCaridea\Event\Listener
.
- It also implements
- 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
Examples
Just a few quick examples.
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.
$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.
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.
class MyContainerAware implements \Caridea\Container\ContainerAware { use \Caridea\Container\ContainerSetter; public function __construct() { $this->container = new \Caridea\Container\EmptyContainer(); } }