├── src ├── NotFoundException.php └── Container.php ├── composer.json ├── LICENSE.md └── README.md /src/NotFoundException.php: -------------------------------------------------------------------------------- 1 | =5.4.0", 15 | "psr/container": "^1.0" 16 | }, 17 | "provide": { 18 | "psr/container-implementation": "^1.0" 19 | }, 20 | "require-dev": { 21 | "phpunit/phpunit" : "4.*", 22 | "squizlabs/php_codesniffer": "~2.3" 23 | }, 24 | "autoload": { 25 | "psr-4": { 26 | "Yuloh\\Container\\": "src" 27 | } 28 | }, 29 | "autoload-dev": { 30 | "psr-4": { 31 | "Yuloh\\Container\\Tests\\": "tests" 32 | } 33 | }, 34 | "scripts": { 35 | "test": "phpunit", 36 | "cs": "phpcs --standard=psr2 src/" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Matthew Allan matthew.james.allan@gmail.com 4 | 5 | > Permission is hereby granted, free of charge, to any person obtaining a copy 6 | > of this software and associated documentation files (the "Software"), to deal 7 | > in the Software without restriction, including without limitation the rights 8 | > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | > copies of the Software, and to permit persons to whom the Software is 10 | > furnished to do so, subject to the following conditions: 11 | > 12 | > The above copyright notice and this permission notice shall be included in 13 | > all copies or substantial portions of the Software. 14 | > 15 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/Container.php: -------------------------------------------------------------------------------- 1 | has($id)) { 17 | throw NotFoundException::create($id); 18 | } 19 | 20 | return $this->definitions[$id]($this); 21 | } 22 | 23 | public function has($id) 24 | { 25 | return isset($this->definitions[$id]); 26 | } 27 | 28 | /** 29 | * Adds an entry to the container. 30 | * 31 | * @param string $id Identifier of the entry. 32 | * @param \Closure $value The closure to invoke when this entry is resolved. 33 | * The closure will be given this container as the only 34 | * argument when invoked. 35 | */ 36 | public function set($id, \Closure $value) 37 | { 38 | $this->definitions[$id] = function ($container) use ($value) { 39 | static $object; 40 | 41 | if (is_null($object)) { 42 | $object = $value($container); 43 | } 44 | 45 | return $object; 46 | }; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Container 2 | 3 | [![Latest Version on Packagist][ico-version]][link-packagist] 4 | [![Software License][ico-license]](LICENSE.md) 5 | [![Build Status][ico-travis]][link-travis] 6 | 7 | Container is a lightweight dependency injection container. It's compatible with the [PSR-11 container interface](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-11-container.md), so you can use it with lots of different projects out of the box. 8 | 9 | The container is really simple; It's basically an array of identifier => callable mappings, and the callable is invoked to get the resulting object. 10 | 11 | New to dependency injection and containers? I wrote a [blog post](http://mattallan.org/2016/dependency-injection-containers/) explaining dependency injection and how this container works. 12 | 13 | This package is compliant with PSR-1, PSR-2, PSR-4, and PSR-11. 14 | 15 | ## Install 16 | 17 | Via Composer 18 | 19 | ``` bash 20 | $ composer require yuloh/container 21 | ``` 22 | 23 | ## Usage 24 | 25 | ### Adding Entries 26 | 27 | Adding an entry to the container is really simple. Just specify the identifier as the first argument, and a callable as the second argument. 28 | 29 | ``` php 30 | use Yuloh\Container\Container; 31 | 32 | $container = new Container(); 33 | 34 | $container->set(Psr\Log\LoggerInterface::class, function () { 35 | $logger = new Monolog\Logger(); 36 | $logger->pushHandler(new StreamHandler('error.log')); 37 | return $logger; 38 | }); 39 | ``` 40 | 41 | The closure will receive the container as it's only argument, so you can use the container to resolve the dependencies of your entry. 42 | 43 | ```php 44 | $container->set('db', function ($container) { 45 | $db = new Database(); 46 | $logger = $container->get(Psr\Log\LoggerInterface::class); 47 | $db->setLogger($logger); 48 | return $db; 49 | }); 50 | ``` 51 | 52 | All entries are shared (singletons), which means an entry will be resolved once and reused for subsequent calls. 53 | 54 | ### Getting Entries 55 | 56 | To check if an entry exists, use `has`. To get an entry, use `get`. If you are just retrieving entries you can typehint `Psr\Container\ContainerInterface` instead of the actual Container. 57 | 58 | ```php 59 | if ($container->has('db')) { 60 | $db = $container->get('db'); 61 | } 62 | ``` 63 | ## Why Another Container? 64 | 65 | There are **a lot** of containers out there. I was working on a project and wanted a lightweight default container and couldn't find what I wanted. This container: 66 | 67 | - Implements container-interop. 68 | - Supports PHP 5.4+ 69 | - Supports adding entries at runtime. 70 | - Is incredibly lightweight, with the bare minimum of code to support the first 3 goals. 71 | 72 | ## Testing 73 | 74 | ``` bash 75 | $ composer test 76 | $ composer cs 77 | ``` 78 | 79 | [ico-version]: https://img.shields.io/packagist/v/yuloh/container.svg?style=flat-square 80 | [ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square 81 | [ico-travis]: https://img.shields.io/travis/yuloh/container/master.svg?style=flat-square 82 | 83 | [link-packagist]: https://packagist.org/packages/yuloh/container 84 | [link-travis]: https://travis-ci.org/yuloh/container 85 | --------------------------------------------------------------------------------