Skip to content

Instantly share code, notes, and snippets.

@AmyStephen
Last active December 17, 2015 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AmyStephen/5607606 to your computer and use it in GitHub Desktop.
Save AmyStephen/5607606 to your computer and use it in GitHub Desktop.
<?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>
}
}
<?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>
}
@AmyStephen
Copy link
Author

(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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment