Last active
December 17, 2015 12:09
-
-
Save AmyStephen/5607606 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ConfigurationInjector extends CustomInjector implements InjectorInterface | |
{ | |
/** | |
* Constructor | |
* | |
* @param array $option | |
* | |
* @since 1.0 | |
*/ | |
public function __construct($options = array()) | |
{ | |
$this->service_namespace = 'Molajo\\Application\\Configuration\\Adapter'; | |
$this->store_instance_indicator = true; | |
parent::__construct($options); | |
} | |
/** | |
* Instantiate Class | |
* | |
* @param bool $create_static | |
* | |
* @return object | |
* @since 1.0 | |
* @throws InjectorException | |
*/ | |
public function instantiate($create_static = false) | |
{ | |
$getService = $this->getService; | |
$cache_instance = $getService('Cache'); | |
<snip> | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
<snip> | |
use Molajo\IoC\Container; | |
/** | |
* Front Controller for Molajo | |
* | |
* 1. Initialise | |
* 2. Route | |
* 3. Authorise | |
* 4. Execute (Display or Action) | |
* 5. Response | |
* | |
* In addition, schedules onAfter events after each of the above. | |
* | |
* @author Amy Stephen | |
* @license http://www.opensource.org/licenses/mit-license.html MIT License | |
* @copyright 2013 Amy Stephen. All rights reserved. | |
* @since 1.0 | |
*/ | |
class FrontController implements FrontControllerInterface | |
{ | |
/** | |
* Inversion of Control Container | |
* | |
* @var object | |
* @since 1.0 | |
*/ | |
protected $ioc; | |
<snip> | |
/** | |
* Initialise Application, including invoking Inversion of Control Container and | |
* Services defined in Services.xml | |
* | |
* @return $this | |
* @since 1.0 | |
* @throws FrontControllerException | |
*/ | |
public function initialise() | |
{ | |
<snip> | |
$connect = $this; | |
$getService = function ($service, $options = array()) use ($connect) { | |
return $connect->getService($service, $options); | |
}; | |
$this->ioc = new Container($getService); | |
return; | |
} | |
/** | |
* Get a service instance | |
* | |
* @param string $service | |
* @param array $options | |
* | |
* @results null|object | |
* @since 1.0 | |
* @throws FrontControllerException | |
*/ | |
public function getService($service, $options = array()) | |
{ | |
return $this->ioc->getService($service, $options); | |
} | |
<snip> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(Code above, simplified a bit for purposes of the question.)
The only way to access the Inversion of Control Container is through the Front Controller.
To request a service, the Front Controller getService method must be called. There is no other entry point to get a service than this one.
When I instantiate the IoCC within the FrontController, I create a Closure with a call to the Front Controller getService method.
That closure is passed into the IoCC and used when dependency objects are found to create the class. (Example - "instantiate" method above where $getService is used to retrieve the Cache service.)
The closure is used in other locations (controllers, helpers) throughout the application to instantiate classes.
Is that an acceptable approach? Does it avoid creating a dependency? (Since it's a closure?) Does it turn the IoCC into a Service Container? (Which I am hoping to avoid.)