Skip to content

Instantly share code, notes, and snippets.

@EvanDotPro EvanDotPro/Di.php
Created Feb 24, 2012

Embed
What would you like to do?
Static DI locator replace for ZendSkeletonApplication for benchmarking purposes.
<?php
namespace Application\Di;
use Zend\Di\Locator;
class Di implements Locator
{
protected $classes = array();
public function get($name, array $params = array())
{
if ($instance = $this->getInstance($name)) {
return $instance;
}
switch ($name) {
case 'Zend\Mvc\Router\RouteStack':
return \Zend\Mvc\Router\Http\TreeRouteStack::factory()->addRoutes(array(
'default' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Application\Controller\IndexController',
'action' => 'index',
),
),
),
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\IndexController',
'action' => 'index',
),
),
),
));
break;
case 'Zend\Mvc\View\RouteNotFoundStrategy':
$return = new $name;
$return->setNotFoundTemplate('error/404');
return $this->setInstance($name, $return);
break;
case 'Zend\Mvc\View\ExceptionStrategy':
$return = new $name;
$return->setDisplayExceptions(true);
$return->setErrorTemplate('error/404');
return $this->setInstance($name, $return);
break;
case 'Application\Controller\IndexController':
$return = new $name;
$return->setBroker($this->get('Zend\Mvc\Controller\PluginBroker'));
return $this->setInstance($name, $return);
break;
case 'Zend\Mvc\Controller\PluginBroker':
$return = new $name;
$return->setClassLoader($this->get('Zend\Mvc\Controller\PluginLoader'));
return $this->setInstance($name, $return);
break;
case 'Zend\Mvc\View\DefaultRenderingStrategy':
$return = new $name($this->get('Zend\View\View'));
$return->setBaseTemplate('layout/layout');
return $this->setInstance($name, $return);
break;
case 'Zend\View\Strategy\PhpRendererStrategy':
return $this->setInstance($name, new $name($this->get('Zend\View\Renderer\PhpRenderer')));
break;
case 'Zend\View\Renderer\PhpRenderer':
$return = new $name;
$return->setResolver($this->get('Zend\View\Resolver\AggregateResolver'));
return $this->setInstance($name, $return);
break;
case 'Zend\View\Resolver\AggregateResolver':
$return = new $name;
$return->attach($this->get('Zend\View\Resolver\TemplatePathStack'));
return $this->setInstance($name, $return);
break;
case 'Zend\View\Resolver\TemplatePathStack':
$return = new $name;
$return->setPaths(array(
'application' => __DIR__ . '/../../../view'
));
return $this->setInstance($name, $return);
break;
default:
return $this->setInstance($name, new $name);
break;
}
}
protected function setInstance($name, $instance)
{
$this->instances[$name] = $instance;
return $instance;
}
protected function getInstance($name)
{
return isset($this->instances[$name]) ? $this->instances[$name] : false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.