/** * Constructor, called by the front controller. * * @param A_Locator $locator */ public function __construct($locator = null) { if ($locator) { $this->locator = $locator; $this->request = $locator->get('Request'); $this->response = $locator->get('Response'); } }
function testLocatorGetLoadDI() { $locator = new A_Locator(); $config = new A_Collection(array('foo' => 'The value of foo.')); $locator->set('Config', $config); // load a class $inject = array('Example1' => array(), 'Example2' => array('__construct' => array(0 => array('A_Locator' => 'get', 'name' => 'Example1', 'class' => 'Example1')), 'setBoth' => array(0 => array('A_Locator' => 'container', 'name' => 'Config', 'class' => '', 'key' => 'foo'), 1 => array('type' => 'string', 'value' => 'Hello world.')))); $locator->register($inject); $example3 = $locator->get('example', 'Example2'); #dump($example3); // check if class directly loaded exists $this->assertTrue(class_exists('Example2')); // check if dependent class exists $this->assertTrue(class_exists('Example1')); // is object the correct type $this->assertTrue(is_a($example3, 'Example2')); $example4 = $example3->getArgs(); // get constructor args injected inot Example2 $this->assertTrue(is_a($example4, 'Example1')); }
} } class FooModel extends BaseModel { } class BarModel extends BaseModel { } // create a config object to show how injected data can come from a registered container $ConfigObj = new Config(); $ConfigObj->set('db', $config['db']); // create Locator which is Registry + Loader + DI $locator = new A_Locator(); $locator->set('Config', $ConfigObj); // register dependency for database connector to inject config array into contructor // future calls to $locator->get('', 'A_Db_Pdo') will pass array to constructor when instantiating $locator->register(array('A_Db_Pdo' => array('__construct' => array(array('A_Locator' => 'container', 'name' => 'Config', 'class' => '', 'key' => 'db'))))); // register dependencies for classes that will have A_Db_Pdo object injected // Note that A_Db_Pdo object is put in Registry with name 'DB' so later call will just get object from Registry $locator->register(array('BaseModel' => array('__construct' => array(array('A_Locator' => 'get', 'name' => 'DB', 'class' => 'A_Db_Pdo')), 'set' => array('base', 'Data injected into set(base, )')), 'FooModel' => array('__construct' => array(array('A_Locator' => 'get', 'name' => 'DB', 'class' => 'A_Db_Pdo'))), 'BarModel' => array('set' => array('bar', 'Data injected into set(bar, )'), 'setDb' => array(array('A_Locator' => 'get', 'name' => 'DB', 'class' => 'A_Db_Pdo'))))); ?> <html> <body> <?php $FooModel = $locator->get('', 'FooModel', 'BaseModel'); dump($FooModel, 'FooModel: '); $BarModel = $locator->get('', 'BarModel'); dump($BarModel, 'BarModel: '); ?> </body> </html>
/** * Run the form as a Command object passed a Registry containing the Request * * @param A_Locator $locator * @return bool True if error occured */ public function run($locator) { $request = $locator->get('Request'); $this->isValid($request); return $this->error; }