Hi.
I have done a little more work on the code as a bit of fun. The injector now has a Singleton interface, and you have a little more control over parameters and the lifecycle. It still searches by interface, but it now caters for some of the day to day practicalities. Best to look at the tests for the detail, but here are some of the new features:
1) Can instantiate with parameters...
PHP Code:
// Infrastructure...
class Integer implements Number {
function __construct($value) { ... }
}
// Application...
$injector = Injector::instance();
$injector->register('Integer');
// Domain or framework...
$injector = Injector::instance();
$injector->create('Number', 37);
2) Searching by superclass...
PHP Code:
// Infrastructure...
class UnsignedInteger extends Integer {
function __construct($value) { ... }
}
// Application...
$injector = Injector::instance();
$injector->register('UnsignedInteger');
// Domain or framework...
$injector = Injector::instance();
$injector->create('Integer', 37);
3) Singleton life cycle support...
PHP Code:
// Infrastructure...
class Only {
function __construct($message) { ... }
}
// Application...
$injector = Injector::instance();
$injector->registerAsSingleton('Only', 'Hi!');
// Domain or framework...
$injector = Injector::instance();
$injector->create('Only');
Note that parameters have to go in at registration time. There is no error checking for this yet.
4) Session lifecycle support...
PHP Code:
// Infrastructure...
class MyCart implements ShoppingCart { ... }
// Application...
$injector = Injector::instance();
$injector->registerAsSession('MyCart', 'Our wonderful store');
// Domain or framework...
$injector = Injector::instance();
$injector->create('ShopingCart');
This doesn't work out of the box exactly. See 6 below.
5) Ability to use dependency injection to override the service locators...
PHP Code:
// Infrastructure wrapper...
class MySessionLocator implements SessionLocator { ... }
// Framework...
$injector = Injector::instance();
$injector->register('MySessionLocator');
// Application...
$injector = Injector::instance();
$injector->registerAsSession('MyCart', 'Our wonderful store');
This means that you can have your own session handlers for example. Basically the dependency injector uses dependency injection internally. Makes the code a little difficult to understand though, even though it's not much more than 100 lines.
6) Ability to add new registration methods...
PHP Code:
// Infrastructure...
class DataMapperLocator implements PersistenceLocator { ... }
// Framework...
$injector = Injector::instance();
$injector->addRegistrationMethod(
'registerAsPersistent',
'PersistenceLocator');
// Application...
$injector = Injector::instance();
$injector->register('DataMapperLocator');
$injector->registerAsPersistent('Person');
// Domain...
$injector = Injector::instance();
$person = $injector->create('Person');
In fact the session system in the injector uses that technique. To install it you do...
PHP Code:
require_once('phemto/injector.php');
require_once('phemto/session.php');
...and it appears by magic. This extra flexibility offsets the disadvantage of the injector being a Singleton somewhat.
Have fun
.
yours, Marcus
Bookmarks