Download the PHP package pugx/godfather without Composer
On this page you can find all versions of the php package pugx/godfather. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download pugx/godfather
More information about pugx/godfather
Files in pugx/godfather
Package godfather
Short Description Strategy pattern component as library and symfony2 bundle
License MIT
Homepage http://github.com/liuggio/godfather
Informations about the package godfather
Godfather
A library for the strategy pattern in PHP, if you use Symfony2 you could easily integrate Godfather with the bundle. |
---|
- The Strategy pattern
- Installation
- Symfony2 bundle
- Contribution
The Strategy Pattern
http://en.wikipedia.org/wiki/Strategy_pattern
Intent
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
TL;DR
Given an object, you want to know its service.
eg. Entity\Mug
has a MugService
and Entity\Tshirt
has a TshirtService
Sandbox
A working example is at example/godfather.php
When do you need a strategist as Godfather?
- If you have a lot of classes that differs by their behaviour...
- If you have multiple conditional statements in order to define different behaviours...
- Given an object you want to know its manager/service/handler/provider/repository/...
Installation
composer require pugx/godfather ~0.1
A simple use case
The problem is that you have an object and you want to handle it properly.
How it works
This library does not try to duplicate the services, or to create a new container, but uses aliases in order to have a mapping between services and names.
An object is converted by the Context::getStrategyName
more info at changing the Context::Converter.
The smelling code
If your code look like this you will need the godfather's protection :)
The strategist
GodFather and an array as DIC
GodFather and Symfony Dependency Injection Container
Using the Symfony2 Bundle
Install the Bundle
Add the bundle in the app/AppKernel.php
Configuring app/config/config.yml
Minimal Configuration
With the fallback strategy
Set your strategies:
Using in the controller:
Advanced with multiple instances
Instead of default you could configure your strategy in different godfather instances.
the strategies:
and then the code with multiple instances
Changing the Context::Converter
The Godfather\Context\Context::getStrategyName
transforms an object into a strategy name,
the default one just extract from the $object the short class name.
If you want another converter create your class extends the ContextInterface and then:
Contribution
Active contribution and patches are very welcome. To keep things in shape we have quite a bunch of unit tests. If you're submitting pull requests please make sure that they are still passing and if you add functionality please take a look at the coverage as well it should be pretty high :)
License
The license is visible here.