Skip to content

Instantly share code, notes, and snippets.

@sasezaki
Created April 3, 2012 11:00
Show Gist options
  • Select an option

  • Save sasezaki/2291030 to your computer and use it in GitHub Desktop.

Select an option

Save sasezaki/2291030 to your computer and use it in GitHub Desktop.
Zend\Di and bootstrap usage example (Zend\Cache\StorageFactory setting with Zend\Di)
<?php
namespace Miffie;
use Miffie\Runner,
Zend\Config\Config,
Zend\Di\Configuration as DiConfiguration,
Zend\Di\Di;
class Bootstrap
{
private $config;
public function __construct(Config $config)
{
$this->config = $config;
}
public function bootstrap(Runner $runner)
{
$this->setupLocator($runner);
}
protected function setupLocator(Runner $runner)
{
$di = new Di;
$di->instanceManager()->addTypePreference('Zend\Di\Locator', $di);
$diConfig = new DiConfiguration(array(
'definition' => array(
'class' => array(
'Zend\Cache\Storage\Adapter' => array(
'instantiator' => array(
'Zend\Cache\StorageFactory',
'factory'
),
),
'Zend\Cache\StorageFactory' => array(
'methods' => array(
'factory' => array('cfg' => array('required' => true, 'type' => false))
)
)
),
),
'instance' => array(
'alias' => array(
'cache' => 'Zend\Cache\Storage\Adapter',
'scraper' => 'Diggin\Scraper\Scraper',
'wedata-storage' => 'Diggin\Service\Wedata\Storage\Cache'
),
'cache' => array(
'parameters' => array('cfg' => array(
'adapter' => 'Memory'
))
)
)
));
$diConfig->configure($di);
$config = new DiConfiguration($this->config->di);
$config->configure($di);
$runner->setLocator($di);
}
}
<?php
$config = Zend\Config\Factory::fromFile($this->_configFile, true);
$bootstrap = new Miffie\Bootstrap($config);
$bootstrap->bootstrap($runner);
var_dump($runner->getLocator()->get('cache', ($config->cache) ? array('cfg' => $config->cache):array()));
<?php
$config = array(
/** Zend\Cache\CacheFactory::factory() */
'cache' => array(
'adapter' => array(
'name' => 'Filesystem',
'options' => array(
'cache_dir' => dirname($_SERVER['SCRIPT_FILENAME']).'/cache',
'ttl' => 86400
)
),
'plugins' => array('serializer')
),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment