Download the PHP package everon/factory without Composer
On this page you can find all versions of the php package everon/factory. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package factory
Everon Factory Component
Library to handle dependency injection and instantiation. Allows to produce code that is easy to test.
Versions
- Use v1.x and v2.x with PHP 7.2+
- Use v3.x with PHP 8+
Features
- One line, lazy loaded dependency injection (via setters or constructors)
- Trait based, simple to use and implement, no hassle with config files, pure PHP
- Factory/FactoryWorker gives full control and overview, on how each and every object is created
- FactoryWorker allows for custom implementation of Factory methods
- Full control when a dependency should be reused (via Dependency Container) or created from scratch (eg. Logger vs Value Object)
- Minimized file/memory access/usage due to callbacks and lazy load
- Intuitive Interface: clear, small and simple API
- Convention over configuration
- Clean code
How it works
Every instantiation should happen only inside of the class. Its role is similar of . It sits between Everon Factory Component and the client code that uses it, so there is no direct coupling. It's easy to implement and manage, because injection is cheap and dependency setup happens in one place.
This makes testing much easier, as everything can be easily mocked/stubbed/faked. It's ok to use operator outside of Factory methods for simple value object like classes. For example .
For a particular module or application, the method of is one of a places where you could setup your dependency tree. Or you could use Root Composition pattern and handle your whole dependency graph outside of your application.
Easy Dependency Injection
To use dependency injection, register it with and use one line trait to inject it.
For example, this will auto inject a predefined instance via setter injection into class.
Register with Dependency Container
Use method to register the dependency under name.
Define the traits and interface
Example of dependency trait, which is reused between all of the classes that use trait. The only thing to remember is that, the name of the trait should be the same, as the name under which the dependency was registered with the .
Bonus: You can also define and assign the too all classes that are being injected with instance.
Define the setter injection trait. The only requirement is that the name ends with . You can reuse already defined trait, in every class that implements LoggerAwareInterface.
Register with Factory
Use to register callback which will return instance of required FactoryWorker.
Build with FactoryWorker
To build your dependencies use the classes.
Resolve with Dependency Container
Use to receive dependency defined earlier with or . So you can pass the same instance to another class via constructor injection.
Now and will share the same instance of class.
If you don't do any work in constructors, and you shouldn't, and only require the functionality later, it would be easier to just use the as the infrastructure type dependency and just inject it via setter injection with one line. The end result is the same.
Every required class will be injected with the same instance, that was registered with the and assembled by in .
Ensures Tests Ready Code (TM)
Writing tests of classes that use for the dependency injection and instantiation removes the hassle of dealing with dependency problems since everything is so easy to mock.
Dependency Container, Factory and FactoryWorker
Instantiate new and assign it to . Use to get instance of your specific .
The best thing is, that the classes which are being instantiated with the are not aware about the at all.
It could be in separate files, obviously, split by the application type and the dependencies it needs.
An example, of using the same instance of , in every class, through out whole application, which required dependency.
What's the best way to inject dependencies?
Use constructor for dependencies that are part of what the class is doing, and use setters/getters for infrastructure type dependencies. In general, a or could be good examples of infrastructure type dependencies.
Test Driven
See tests for more examples with trait dependencies.
Example
Check Everon Criteria Builder to see how to use Everon Factory by example.