Created
June 3, 2015 06:37
-
-
Save mnorkin/0ef42850fe8f7dde955b to your computer and use it in GitHub Desktop.
How to solve zend array config merge
This file contains 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 | |
/** | |
* @author: Maksim Norkin | |
* @email: maksim.norkin@ruptela.lt | |
*/ | |
namespace ZR\Cache\Adapter\Redis; | |
use Zend\Config\Reader\Json; | |
use Zend\EventManager\EventInterface; | |
use Zend\ModuleManager\ModuleEvent; | |
use Zend\ModuleManager\ModuleManager; | |
use Zend\Mvc\ModuleRouteListener; | |
use Zend\Mvc\MvcEvent; | |
/** | |
* Class Module | |
* | |
* @package ZR\Cache\Adapter\Redis | |
*/ | |
class Module | |
{ | |
/** | |
* @param EventInterface|MvcEvent $e | |
*/ | |
public function onBootstrap(EventInterface $e) | |
{ | |
$eventManager = $e->getApplication()->getEventManager(); | |
$moduleRouteListener = new ModuleRouteListener(); | |
$moduleRouteListener->attach($eventManager); | |
} | |
public function init(ModuleManager $moduleManager) | |
{ | |
$events = $moduleManager->getEventManager(); | |
// Registering a listener at default priority, 1, which will trigger | |
// after the ConfigListener merges config. | |
$events->attach(ModuleEvent::EVENT_MERGE_CONFIG, array($this, 'onMergeConfig')); | |
} | |
/** | |
* @param ModuleEvent $e | |
*/ | |
public function onMergeConfig(ModuleEvent $e) | |
{ | |
$configListener = $e->getConfigListener(); | |
$config = $configListener->getMergedConfig(false); | |
$localConfig = $this->getConfig(); | |
$adapterName = key($localConfig['zr-cache']['adapter']['redis']); | |
$systemAdapterConnectionCollection = $config['zr-cache']['adapter']['redis'][$adapterName]['connections']; | |
$localAdapterConnectionCollection = $localConfig['zr-cache']['adapter']['redis'][$adapterName]['connections']; | |
$mergedAdapterConnectionCollection = new \ArrayIterator(\Functional\filter( | |
$systemAdapterConnectionCollection, | |
function($systemAdapterConnection) use ($localAdapterConnectionCollection) { | |
return !\Functional\contains($localAdapterConnectionCollection, $systemAdapterConnection); | |
} | |
)); | |
$config['zr-cache']['adapter']['redis'][$adapterName]['connections'] = | |
$mergedAdapterConnectionCollection->getArrayCopy(); | |
$configListener->setMergedConfig($config); | |
} | |
/** | |
* @return array | |
*/ | |
public function getConfig() | |
{ | |
/** @var Json $reader */ | |
$reader = new Json(); | |
return $reader->fromFile(__DIR__ . '/config/module.config.json'); | |
} | |
/** | |
* @return array | |
*/ | |
public function getAutoloaderConfig() | |
{ | |
return array( | |
'Zend\Loader\StandardAutoloader' => array( | |
'namespaces' => array( | |
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment