phapi / di
Phapi Dependency Injection Container
Installs: 3 802
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: >=5.6.0
- phapi/contract: 1.*
Requires (Dev)
- codeclimate/php-test-reporter: dev-master
- mockery/mockery: 0.9.*
- phpunit/phpunit: 4.*
This package is not auto-updated.
Last update: 2021-02-05 21:36:37 UTC
README
Phapi has a built in Dependency Injector Container that can be used to store both objects and parameters. It's easy to use and it gives the opportunity to replace many of the built in functionalities since Phapi uses the Container to store most of it's objects, dependencies and parameters.
The container can be accessed in an endpoint by using the $this->container
parameter.
Installation
The package is installed by default by the Phapi framework. Installing the package to use is separately can be done by using composer:
$ composer require phapi/di:1.*
Defining parameters
The easiest example is to store parameters in the container:
<?php // Set parameter $container['param'] = 'value'; // Get parameter echo $container['param']; // Output: value
Defining objects and dependencies
Objects and its dependencies are defined by anonymous functions that returns an instance of an object, you have two options you can use to define them. Either by using:
<?php // Define database configuration $container['dbUserName'] = 'root'; $container['dbPassword'] = '1234'; // Add DB connection to container $container['dbConn'] = function ($container) { return new PDO( 'mysql:host=localhost;dbname=test', $container['dbUserName'], $container['dbPassword'] ); };
Or by using the bind()
method:
<?php // Define database configuration $container['dbUserName'] = 'root'; $container['dbPassword'] = '1234'; // Add DB connection to container $container->bind('dbConn', function ($container) { return new PDO( 'mysql:host=localhost;dbname=test', $container['dbUserName'], $container['dbPassword'] ); });
Notice that the function has access to the container instance which makes it possible to fetch parameters and dependencies from the container.
By default the container will return the same instance of the object each time you get it. The bind()
method takes a third optional parameter for defining if the singleton or multiton pattern should be used while creating the object. The default is singleton. Change it to multiton and each call to $container['dbConn']
will now create and return a new instance.Example:
<?php // Define database configuration $container['dbUserName'] = 'root'; $container['dbPassword'] = '1234'; // Add DB connection to container $container->bind('dbConn', function ($container) { return new PDO( 'mysql:host=localhost;dbname=test', $container['dbUserName'], $container['dbPassword'] ); }, \Phapi::TYPE_MULTITON);
The order of the definitions doesn't matter since the objects are created when you get them.
Retrieving objects and/or parameters
Using the object is easy:
<?php // Use DB connection $container['dbConn']->query('SELECT ...');
Or use the make()
method:
<?php // Get DB connection $db = $container->make('dbConn'); // Use the connection $db->query('Select ...');
Removing objects and/or parameters
The container implements the \ArrayAccess
interface which makes it possible to use as a regular array. Removing an object or a parameter is just a matter of unsetting it:
<?php unset($container['dbConn']);
Validators
In some cases you might want to ensure a certain key has a specific type of value. Phapi needs the $container['log']
to be a PSR-3 compatible logger. To enforce this a validator is assigned to the log key.
<?php // Register validator $container->addValidator('log', new \Phapi\Container\Validator\Log($this));
The validators validate()
method is a simple check if the provided logger is PSR-3 compatible:
<?php /** * Validates the configured logger. If no logger is configured or * if the configured logger isn't PSR-3 compliant an instance of * NullLogger will be used instead. * * The PSR-3 package includes a NullLogger that doesn't do * anything with the input but it also prevents the application * from failing. * * This simplifies the development since we don't have to check * if there actually are a valid cache to use. We can just ask * the Cache (even if its a NullCache) and we will get a response. */ public function validate($logger) { $original = $logger; if (is_callable($logger)) { $logger = $logger($this->container); } // Check if logger is an instance of the PSR-3 logger interface if ($logger instanceof LoggerInterface) { return $original; } // A PSR-3 compatible log writer hasn't been configured so we // don't know if it is compatible with Phapi. Therefore we // create an instance of the NullLogger instead return function ($container) { return new NullLogger(); }; }
As you can see in this example we don't raise an error even if the provided logger is invalid. This is done since we don't want the application to break because of a faulty logger. In other cases it's probably more suitable to throw an exception.
License
Phapi Di Container is licensed under the MIT License - see the license.md file for details
Contribute
Contribution, bug fixes etc are always welcome.