Last active
December 13, 2015 20:39
-
-
Save raykolbe/4972035 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 | |
namespace Application\Cache; | |
use Zend\Cache\Storage\StorageInterface as Storage; | |
interface CacheAwareInterface | |
{ | |
public function setCache(Storage $cache); | |
} |
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 | |
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'); | |
} | |
} | |
} |
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 | |
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