inxilpro / zit
Zit is a simple dependency injector based heavily on Pimple. It aims to provide the same simplicity as Pimple while offering a slightly more robust object interface.
Requires
- php: >=5.4.0
- container-interop/container-interop: ^1.1
This package is auto-updated.
Last update: 2021-08-05 19:06:46 UTC
README
Zit is a simple dependency injector based heavily on Pimple. It aims to provide the same simplicity as Pimple while offering a slightly more robust object interface.
⚠️ Use syberisle/zit
Instead
This package has been updated and replaced by syberisle/zit
. Please upgrade to that to get the latest updates and improvements.
Original README
Installation
Zit is available via Composer:
composer require inxilpro/zit
Usage
Zit is simple to use. Like Pimple, objects are defined through anonymous functions that return an instance of the object:
$container = new \Zit\Container(); $container->set('auth', function() { return new Auth(); });
All instantiation functions are passed the container as the first argument, making dependency injection possible:
$container->set('auth', function($container) { return new Auth($container->get('db')); });
Zit also provides convenient magic methods for setting instantiation functions:
$container->setAuth(function() { /* ... */ }); // Or: $container->set_auth(function() { /* ... */ });
Getting Objects
Getting objects are as simple as:
$container->get('auth');
Or, if you prefer the shorthand:
$container->getAuth(); // Or: $container->get_auth();
Getting Fresh Objects
By default, all objects are shared in Zit. That is, once an object is created, that same exact object is
returned for each additional get()
. If you need a fresh object, you can do so with:
$container->fresh('auth'); // Or: $container->freshAuth(); // Or: $container->fresh_auth(); // Or: $container->newAuth(); // Or: $container->new_auth();
Note that because the 'new' keyword is reserved, you can only use it if you're using the magic methods.
Constructor Parameters
Sometimes you need to pass parameters to the constructor of an object, while still also injecting dependencies. Zit automatically passes all parameters on to your instantiation function:
$container->setUser(function($c, $id)) { $user = new User($id); $user->setDb($c->getDb()); return $user; }); $user = $container->newUser(1); // Parameters are taken into account when caching results: $user2 = $container->getUser(1); // $user2 === $user;
Storing Static Content
You can also use Zit to store strings/objects/etc. Just pass it instead of a generator:
$container->set('api_key', 'abcd1234567890'); $key = $container->get('api_key');
Please note: You must wrap callables with an instantiation function if you want the callable returned rather than the return value of the callable.
Custom Container
Most projects will benefit from a custom container that sets up its own injection rules. This is as simple as extending Zit:
namespace MyApp\Di; class Container extends \Zit\Container { public function __construct() { $this->setAuth(function() { /* ... */ }); $this->setUser(function() { /* ... */ }); } }
Change Log
Version 3.0.0
- Implemented Container Interoperability. Zit was already
container-interop
compatible, but it now implements the interface and throws an exception that implementsInterop\Container\Exception\NotFoundException
when a item is not found. This exception extends\InvalidArgumentException
, so 3.0.0 should be nearly 100% backwards-compatible, but I'm bumping the major version just in case. - Dropped support for PHP 5.3
- Removed deprecated function
setParam
setFactory()
now accepts anycallable
instead of specifically aClosure
- Fixed a typo in the exception message thrown from
__call
if a method does not exist set()
is now fluent (returns the container for chaining)- Switched to md4 hashing for speed improvements (we don't need the security of md5)
- Added DocBlocks throughout the code
Version 2.0
- Removed the
setParam
method in favor of checking whether the parameter passed toset
is callable. - Added the "Factory" variant. This could cause backwards compatibility issues if you have set up objects that end with the word "factory".
- Updated the
delete
method so that it clears out objects, callbacks, and factories, which could have some abnormal BC issues as well.