Skip to content

Instantly share code, notes, and snippets.

@raykolbe
Last active December 13, 2015 20:39
Show Gist options
  • Save raykolbe/4972035 to your computer and use it in GitHub Desktop.
Save raykolbe/4972035 to your computer and use it in GitHub Desktop.
<?php
namespace Application\Cache;
use Zend\Cache\Storage\StorageInterface as Storage;
interface CacheAwareInterface
{
public function setCache(Storage $cache);
}
<?php
namespace Application\Service;
use Application\Cache\CacheAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\InitializerInterface;
class CacheInitializer implements InitializerInterface
{
public function initialize($instance, ServiceLocatorInterface $serviceLocator)
{
if ($instance instanceof CacheAwareInterface) {
$instance->setCache($serviceLocator->get('cache');
}
}
}
<?php
namespace Application;
use Zend\ModuleManager\Feature\ServiceProviderInterface;
use Zend\ModuleManager\Feature\ControllerProviderInterface;
class Module implements ServiceProviderInterface, ControllerProviderInterface
{
public function getServiceConfig()
{
return array(
'initializers' => array(
'cache_init' => 'Application\Service\CacheInitializer'
),
'factories' => array(
'cache' => 'Zend\Cache\Service\StorageCacheFactory', // Add a key 'cache' to your config. See https://github.com/zendframework/zf2/blob/master/library/Zend/Cache/Service/StorageCacheFactory.php
'Application\Form\Thread' => function($sm) {
$form = new Form\Thread('thread');
$form->setInputFilter(new Form\ThreadFilter());
return $form;
},
'Application\Service\MessageBoard' => function($sm) {
// Hard dependencies
return new Service\MessageBoard(
$sm->get('cache'),
$sm->get('Application\Form\Thread')
);
}
)
);
}
public function getControllerConfig()
{
return array(
'factories' => array(
'Application\Controller\ThreadController' => function($controllers) {
$sl = $controllers->getServiceLocator();
$controller = new Controller\ThreadController();
$controller->setThreadForm($sl->get('Application\Form\Thread'));
$controller->setCache($sl->get('cache'));
return $controller;
}
),
'initializers' => array(
'cache_init' => 'Application\Service\CacheInitializer'
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment