minphp / container
A Dependency Injection Container based on Pimple
2.1.0
2016-04-01 22:49 UTC
Requires
- php: >=5.3.0
- container-interop/container-interop: ^1.0
- pimple/pimple: ^3.0
Requires (Dev)
- phpunit/phpunit: ^4.0
- satooshi/php-coveralls: ^1.0
- squizlabs/php_codesniffer: ^2.2
This package is auto-updated.
Last update: 2024-11-23 10:00:13 UTC
README
A standards compliant Pimple-based container.
Installation
Install via composer:
composer require minphp/container:~2.0
Basic Usage
<?php use Minphp\Container\Container; $container = new Container(); // Add/Update $container->set('queue', function($c) { return new \SplQueue(); }); // Verify $queue_exists = $container->has('queue'); // Fetch $queue = $container->get('queue'); // Remove $container->remove('queue');
You can also use the container as an array, as per Pimple. Though, using the ContainerInterface defined methods is preferable as it eases switching to a different container if you ever need to.
<?php use Minphp\Container\Container; $container = new Container(); $container['queue'] = function($c) { return new \SplQueue(); }; $queue = $container['queue'];
ContainerInterface
interface ContainerInterface { // inherited from Interop\Container\ContainerInterface public function get($id); // inherited from Interop\Container\ContainerInterface public function has($id); public function set($id, $value); public function remove($id); }
ContainerAwareInterface
Also provided is the ContainerAwareInterface, which allows your classes to declare that they support container injection. Nifty for granting objects access to your container (you weren't thinking of using a static class, were you?).
interface ContainerAwareInterface { public function setContainer(Minphp\Container\ContainerInterface $container = null); public function getContainer(); }
Usage
<?php namespace myApp\Controller; use Minphp\Container\ContainerAwareInterface; use Minphp\Container\ContainerInterface; class MyController implements ContainerAwareInterface { private $container = null; public function setContainer(ContainerInterface $container = null) { $this->container = $container; } public function getContainer() { return $this->container; } }