Download the PHP package kilahm/ioc-factory-container without Composer
On this page you can find all versions of the php package kilahm/ioc-factory-container. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kilahm/ioc-factory-container
More information about kilahm/ioc-factory-container
Files in kilahm/ioc-factory-container
Package ioc-factory-container
Short Description Using attributes, compile your factories into a single class
License MIT
Informations about the package ioc-factory-container
IOC Factory Container
Compile a type safe IOC Container from user defined attributes.
Use
This library includes an executable that will scan your project directory for factories then construct a valid hack file that aliases all your factories as public instance methods of a single class. Your factory methods must accept only a single parameter which is the factory container.
The factory container should only be instantiated once in your application's bootstrap. You should then be able to use the container to instantiate your application class and run it.
Mark factories with attributes
Here is an example class with its factory function marked.
The attribute name is provides
and requires one paramter, which is an alias for the factory method. The above definition would compile to:
Note that you are able to retrieve the same instance by calling $factory->getMyA()
any number of times, but calling $factory->makeMyA()
will always return a new instance.
Run the factory container compiler
After defining some factory methods, run the findfactories
executable, which can be found in your vendor/bin
directory.
The executable takes any number of arguments which will be interpreted as base directories to scan.
The executable will also accept any number of --exclude="..."
long options which is interpreted as a directory to ignore.
The command above (if run from your project directory) will recursively scan the src/
and other/path/
directories, except for the src/ignore
, other/path/to/ignore
directories and their children.
The resulting factory container file will be created in the project root and will be named FactoryContainer.php
.
You may optionally provide the path to install FactoryContainer.php
by using the --install-path="dir/to/put/file"
option.
Namespaces
You may use namespaces and use
as normal, and the compiler will expand the class names to include their namespace.
The files above would compile to container methods shown below.
None of the examples above were particularly useful factories. This tool would be more useful when a particular class has many dependencies and you wish to encapsulate instantiation inside of a single method.