PHP's efficient IOC framework--canoedi
A very simple and practical IOC framework that has the following characteristics compared to other IOC frameworks:
- Efficient: The framework uses a very practical and efficient algorithm that makes the framework itself less impact on applications, and the framework provides a C extension to maximize performance.
- Simple configuration: Almost no additional configuration is required in most cases
- Automatic assembly: Automatic assembly based on Phpdocument property properties
- Lazy Loading: All instances of injected variables are ready-to-use and do not generate memory garbage
- IDE-friendly: Because of the PHP standard specification, compatible with most IDE
Installation
Compile and install to get maximum efficiency:
|
$ git clone https://github.com/dustinfog/canoe-di.git$ cdcanoe-di/ext$ phpize$ ./configure$ make$ sudomakeinstall |
Then edit php.ini
|
[canoe-di]extension=canoe_di.so |
Composer installation (production environment if the extension has been compiled and installed, this step can be omitted.) In the development environment, the PHP source can let the IDE provide code completion hints, so it is still recommended to perform this step):
1 |
composer require dustinfog/canoe-di |
Using the Get instance
|
The classClassA{}//DI container is instantiated the first time it is processed, and is used as a singleton in future use. $a= \Canoe\DI\Context::get(ClassA::class); |
Labeling-based Assembly
classClassC{}use\Canoe\DI\DITrait;use\Canoe\DI\Context;/** * @property ClassC $c */classClassA{ //Need to introduce a trait to handle the acquisition of $c
useDITrait;
Publicfunctiontest() {
//This can be used directly
Print_r($this->c);
}}
$a= Context::get(ClassA::class);$a->test(); //Try what happens
@uses Annotations:
Uses can specify the class that the attribute uses or the instance in the container
interfaceInterfaceC {
publicfunctionsayHello();
}
classClassWorld implementsInterfaceC{
publicfunctionsayHello() {
Echo"hello, world!\n";
}}
classClassC implementsInterfaceC{
Private$name;
Publicfunction__construct($name) {
$this->name = $name;
}
publicfunctionsayHello() {
Echo"hello, $name!\n"; }}use\Canoe\DI\DITrait;use\Canoe\DI\Context;/** * @property InterfaceC $c1 {
@uses ClassWorld} //Use class name *
@property InterfaceC $c2 {@uses c2} //Use the ID inside the container */classClassA{
/ / Need to introduce a trait to handle the acquisition of $ c
useDITrait; publicfunctiontest() {
Print_r($this->c1); print_r($this->c2);
}}Context::set("c2", newClassC("Bob"));// Better choice: Context::registerDefinition("c2", function(){new ClassC("Bob")})$a = Context::get(ClassA::class);$a->test(); //Try what happens
Singleton
Sometimes we need a limited use of Di in a non-DI environment, when the first borrowing point of each system and Di container is called context::get () is ugly, and the framework provides a more pro-people calling method:
Use\Canoe\DI\SingletonTrait; class ClassA{
Use SingletonTrait;
}
$a= ClassA::getInstance();// is equivalent to Context::get(ClassA::class) but hides the Context call.
$a= ClassA::getInstance("a1");// is equivalent to Context::get("a1"), but does a further type check, ie the instance f1 must have "is a" with ClassA Relationship.
Pre-defined
The example above is implemented automatically at runtime, but at some point it may be necessary to manually pre-create some definitions in case the framework provides simple support.
|
//Register class Canoe\DI\Context::registerDefinition(‘a‘, ClassA::class); //Register callback Canoe\DI\Context::registerDefinition( ‘b‘, function() { returnnewClassB(); }); //Register the instance Canoe\DI\Context::set(‘c‘, newClassC()); |
Configuration
Most of the time, pre-defined is written in the configuration file, you can load the configuration in the following ways:
|
\Canoe\DI\Context::loadConfig([ ‘definitions‘=> [ //This is the definition of ClassB::class, ], ‘beans‘=> [ // Here you can predefine some actual values ‘uid‘=> 5, ],]); |
Forwarding Project Address: Https://github.com/dustinfog/canoe-di
PHP's efficient IOC framework--canoedi