library airlock
IoC for non interface conforming dependencies.
divsmith/airlock
IoC for non interface conforming dependencies.
- Tuesday, September 2, 2014
- by divsmith
- Repository
- 1 Watchers
- 0 Stars
- 13 Installations
- PHP
- 1 Dependents
- 0 Suggesters
- 0 Forks
- 0 Open issues
- 3 Versions
- 0 % Grown
Airlock
Airlock is a simple abstract adapter class to provide decoupling to dependencies that don't specify their own interfaces., (*1)
Installation
Install Airlock by adding the following to your composer.json file, (*2)
{
"require": {
"divsmith/airlock": "1.*"
}
}
Usage
-
Create an interface specifying the methods of the concrete dependency you are going to depend on., (*3)
interface ExampleInterface {
public function method1($args);
public function method2($args);
etc...
}
-
Create an adapter class that extends Airlock and implements the interface., (*4)
class ExampleInterfaceAdapter extends \Airlock\Airlock implements ExampleInterface {
public function method1($args)
{
return $this->delegate(__FUNCTION__, func_get_args());
}
public function method2($args)
{
return $this->delegate(__FUNCTION__, func_get_args());
}
}
Each method needs to contain, (*5)
return $this->delegate(__FUNCTION__, func_get_args());
or it will not behave as expected., (*6)
-
Inject the concrete dependency via the adapter constructor. This can either be done by using
the predefined Airlock constructor, (*7)
$adapter = new ExampleInterfaceAdapter(new ConcreteDependency());
or typehinted and injected via an IoC container, (*8)
class ExampleInterfaceAdapter extends\Airlock\Airlock implements ExampleInterface {
public function __construct(\Namespace\ConcreteDependency $dependency)
{
$this->locker = $dependency;
}
...
}
-
Enjoy! You can now typehint the interface and inject the adapter instead of the concrete dependency
(assuming appropriate IoC bindings). Use the adapter exactly as you would the concrete dependency and
sleep better at night knowing that your code is decoupled from it., (*9)
Other Usage
You can use Airlock with empty interfaces that simply provide latches for your IoC container to resolve.
Just inject your concrete dependency into your adapter and let Airlock do the rest. Not as architecturally
sound as a well defined interface, but it gets you the benefits of not being tied to the concrete dependency
while not requiring you to define each method in the adapter., (*10)
interface ExampleInterface {};
class ExampleAdapter extends \Airlock\Airlock implements ExampleInterface{
public function __construct(\Namespace\ConcreteDependency $dependency)
{
$this->locker = $dependency;
}
}
dev-master
9999999-dev
IoC for non interface conforming dependencies.
Sources
Download
The Development Requires
by
Parker Smith
1.0.1
1.0.1.0
IoC for non interface conforming dependencies.
Sources
Download
The Development Requires
by
Parker Smith
1.0.0
1.0.0.0
IoC for non interface conforming dependencies.
Sources
Download
The Development Requires
by
Parker Smith