bulldog / container
PSR-11 Container.
Requires
- php: ^7.1
- psr/container: ^1.0
Requires (Dev)
- php-coveralls/php-coveralls: ^2.1
- phpunit/phpunit: ^7.3
Provides
This package is auto-updated.
Last update: 2024-11-05 19:48:17 UTC
README
Aka, the dog house. This container implements PSR-11 and ArrayAccess.
Installation
composer require bulldog/container
Usage
You should check to see if the container has
an entry for that ID before
you try to get
that entry.
Key / Value
<?php include 'vendor/autoload.php'; $container = new Bulldog\Container; $value = $container->set('id', 'value'); echo $value; // value $result = $container->has('id'); var_dump($result); // bool(true) $value = $container->get('id'); echo $value; // value
Key / Closure
If the value associated with the key is callable, then the container will call it for you, returning the contents of the closure. This allows you to lazy load classes or services.
<?php include 'vendor/autoload.php'; $container = new Bulldog\Container; class Example { public function test() { echo 'it works!'; } } // Using a closure $container['service'] = function() { return new Example; }; $service = $container['service']; $service->test(); // it works! // Storing an object (not lazy loaded) $container['service'] = new Example; $service = $container['service']; $service->test(); // it works!
Dependency Injection Container
<?php include 'vendor/autoload.php'; $container = new Bulldog\Container; class Required { private $test = "Hello"; public function getTest() { return $this->test; } } class Example { private $required; public function __construct(Required $required) { $this->required = $required; } public function run() { echo $this->required->getTest(); } } $container['required'] = function() { return new Required; }; $container['example'] = function($c) { return new Example($c['required']); }; $e = $container['example']; $e->run();
Available Methods
All parameters are required.
Users SHOULD NOT pass a container into an object so that the object can retrieve its own dependencies. Please refer to the Meta Document provided by PHP-FIG.
Contributing
All contributions welcome! Please first create an issue if something is wrong and let us know if you intend to fix it. Then fork the repo, create a new branch, and work on the issue. The branch name should be relevant to the issue.
Style
Run php-cs-fixer
with the default rules.
php-cs-fixer fix ./src php-cs-fixer fix ./tests