Download the PHP package molajo/ioc without Composer
On this page you can find all versions of the php package molajo/ioc. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package ioc
Short Description Molajo Inversion of Control package offers a full-featured, simple-to-use dependency injection and object construction solution for PHP applications.
License MIT
Homepage http://github.com/Molajo/IoC
Informations about the package ioc
======= Inversion of Control (IoC) Package
The Molajo Inversion of Control (IoC) package offers a full-featured, dependency injection solution and a services layer for PHP applications.
Usage
These are the basic steps to implementing the Inversion of Control (IoC) package:
- Create a Service Folder to store custom dependency injection handlers.
- Update the Front Controller for the Inversion of Control Container (IoCC).
- Request Services from the IoCC within the Application.
- Create Custom Factory Methods for Services.
1. Service Folder
Create a folder within your application to store Service Factory Methods. Each Customer Handler has a folder
containing a class file named ClassnameFactoryMethod.php
. When instantiating the IoCC, you'll provide
the namespace of the Services Folder.
The default namespace for a services folder is Molajo\Services
. Whatever value is used, it will
be passed in as a parameter when instantiating the Container class.
2. Front Controller
The Front Controller should be the only point of entry into the Inversion of Control Container (IoCC).
Use Statement
Add a use statement for the IoC Container class.
Class Property
Define a class property in which to store the IoCC instance.
IoCC Methods
Add these four methods: getService, setService, cloneService, and removeService to the Front Controller.
Instantiate the Container
In the Front Controller boot process, instantiate the Container using the code that begins with $connect and ends before "// Automatically Load These Services". The four closure statements passed into the Container will be used outside of the Front Controller to access the IoCC methods defined in the previous step. Note, also, the location of the $services_folder is passed into the Container.
3. Application Service Requests
The four closures provide a way to access the Front Controller entry points into the IoCC.
- $getService - create the $service using the $options specified, or return an existing service with the same name;
- $setService - replace the existing $service registry in the container with the instance specified;
- $cloneService - clone the container registry entry for a specified service, returning the cloned instance;
- $removeService - remove the container registry entry specified;
When the IoC Container creates a Factory Method instance, it injects it with all four closures before injecting the handler into the DI Adapter. The handler can use those class properties to interact with the IoCC. In this example, the handler requests the dependent Application Service.
getService Parameters
- $service Fully qualified namespace (ex.
Molajo\\Services\\Database\\DatabaseInjector
) or the name of the Services sub-folder (ex.Database
). - $options Optional associative array contain runtime parameters required by the service.
Existing, If Exists, or New Instance
When the Container processes a getService request, it first determines if the named service exists in the
registry. If it is, the existing service is returned. If it does not exist, a new instance will be created unless
the if_exists
$options entry was specified in the request.
Example 1: No fully qualified namespace
When the fully qualified namespace is not defined, the Container looks for a folder with that name in the Services library. In this example, the $options array defines runtime variables for the instantiation process.
Example 2: if_exists Option
This request instructs the Container only return an instance of the User if that instance already exists.
Example 3: Standard Factory Method
A Service Factory Method is not always needed. Classes can be created by the Standard Factory Method. It will match the values defined in the $options associative array with the Constructor parameters and use that data to create the class. For the service name, use the fully qualified class name.
setService
Replaces the existing Container registry entry for this service with the value sent in.
cloneService
Clones the existing Container registry entry for this service and returns the cloned value.
removeService
Removes the existing Container registry entry for this service.
4 - Custom Factory Methods
To create a Custom Dependency Injection Handler:
- Add a folder to the Services Folder defined in Step 1. The folder name is the name of the service.
- Create a PHP file in that folder named
ServiceNameFactoryMethod.php
.
Standard Properties
The Custom Factory Method has access to the following class properties:
- $getService - Closure to request a service of the IoCC, defined above
- $setService - Closure to set a service contained within the IoCC registry, defined above
- $cloneService - Closure to clone a service contained within the IoCC registry, defined above
- $removeService - Closure to remove a service contained within the the IoCC registry, defined above
- $service - The name specified in the getService statement
- $service_namespace - Set the fully qualified namespace for the Class to be instantiated in the constructor
- $store_instance_indicator - defaults to false, set to true in the constructor to store the instance in the IoCC registry
- $static_instance_indicator - defaults to false, set to true in the constructor to request a static instance
- $store_properties_indicator - defaults to false, set to true in the constructor to store the properties, not the instance, in the IoCC registry
- $product_result - populated with the instantiated class
- $options - associative array provided by the getService call
Custom Factory Method Starter
Below is a basic starting pattern for a Custom Dependency Injection Handler. The event methods available to the Factory Method are: onBeforeInstantiation, Instantiate, onAfterInstantiation, initialise, onAfterServiceInitialise, and getProductValue. Each method can be used to inject code at different points in the class creation process. The like-named AbstractHandler method will be used for any omitted methods in the custom class. It is a good idea to become familiar with that class.
The Molajo\Services folder is also a good source of examples of Custom DI Injectors.
Install using Composer from Packagist
Step 1: Install composer in your project
Step 2: Create a composer.json file in your project root
Step 3: Install via composer
Requirements and Compliance
- PHP framework independent, no dependencies
- Requires PHP 5.4, or above
- Semantic Versioning
- Compliant with:
- [phpDocumentor2] (https://github.com/phpDocumentor/phpDocumentor2)
- [phpUnit Testing] (https://github.com/sebastianbergmann/phpunit)
- [Travis Continuous Improvement] (https://travis-ci.org/profile/Molajo)
- Listed on [Packagist] (http://packagist.org) and installed using [Composer] (http://getcomposer.org/)
- Use github to submit pull requests and features
- Author Amy Stephen
- MIT License see the
LICENSE
file for details