将动态配置注入配置数组的正确方法
Proper way to inject dynamic configuration into configuration array
我想知道在 Zend Framework 2 中将动态配置(例如从 db 检索)注入配置数组的最佳方法是什么?在 Module.php 我有:
public function onBootstrap(MvcEvent $e) {
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$eventManager->attach('route', array($this, 'mergeDynamicConfig'));
}
public function mergeDynamicConfig(EventInterface $e) {
$application = $e->getApplication();
$sm = $application->getServiceManager();
$configurationTable = $sm->get('DynamicConfiguration\Model\Table\ConfigurationTable');
$dynamicConfig = $configurationTable->fetchAllConfig();
//Configuration array from db
//Array
//(
// [config] => 'Test1',
// [config2] => 'Test2',
// [config3] => 'Test3',
//)
//What to do here?
//I want to use the configurations above like $sm->get('Config')['dynamic_config']['config3'];
}
出于某些原因,我不会使用此方法。
在您的 Module.php 中放置 SQL 查询意味着它们将在每个用户的 EVERY 请求上执行,从而使您的应用程序慢,很慢。
如果您的数据库遭到破坏,所有配置也将被盗。
解决方案是通过带键的数组移动 config/autoload/my_custom_config.local.php
中的所有配置。从那里您始终可以加载它而无需发出单个数据库请求。它将更快更安全,因为该文件将位于您的根文件夹之外,并且攻击服务器总是比攻击数据库难得多。
如果您仍然希望允许用户编辑选项,您可以简单地 include
文件中的一个操作,然后用 foreach 显示它。要保存信息,您可以这样做:
file_put_contents("my_custom_config.local.php", '<?php return ' . var_export($config, true).';');
另一个优点是,如果您按照上面描述的方式加载配置,您还可以通过 $sm->get('Config')['dynamic_config']['config3']
检索所需的配置
文档中有一节解释 how to manipulate the merged configuration using the specific event ModuleEvent::EVENT_MERGE_CONFIG
Zend\ModuleManager\Listener\ConfigListener
triggers a special event, Zend\ModuleManager\ModuleEvent::EVENT_MERGE_CONFIG
, after merging all configuration, but prior to it being passed to the ServiceManager. By listening to this event, you can inspect the merged configuration and manipulate it.
问题在于服务管理器此时不可用,因为侦听器事件是模块管理器以优先级 1000 触发的首批事件之一。
这意味着您无法执行查询并将之前的配置合并到传递给服务管理器的配置,您需要在之后这样做.
也许我误解了您的要求,但我会以不同的方式处理。
您可以将需要配置 $serviceManager->get('config')
的任何调用替换为 $serviceManager->get('MyApplicationConfig');
,这将是您自己的配置服务,该服务使用合并的应用程序配置,然后添加到其中。
例如,您可以在 module.config.php
.
中注册此配置服务
return [
'service_manager' => [
'factories' => [
'MyApplicationConfig' => 'MyApplicationConfig\Factory\MyApplicationConfigFactory',
]
],
];
并创建一个工厂来加载合并的模块配置,进行任何数据库调用或缓存等。
class MyApplicationConfigFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $sm)
{
$config = $sm->get('config');
$dbConfig = $this->getDatabaseConfigAsArray($sm);
return array_replace_recursive($config, $dbConfig);
}
protected function getDatabaseConfigAsArray(ServiceLocatorInterface $sm)
{}
}
您还有一个额外的好处,即服务是延迟加载的。
我想知道在 Zend Framework 2 中将动态配置(例如从 db 检索)注入配置数组的最佳方法是什么?在 Module.php 我有:
public function onBootstrap(MvcEvent $e) {
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$eventManager->attach('route', array($this, 'mergeDynamicConfig'));
}
public function mergeDynamicConfig(EventInterface $e) {
$application = $e->getApplication();
$sm = $application->getServiceManager();
$configurationTable = $sm->get('DynamicConfiguration\Model\Table\ConfigurationTable');
$dynamicConfig = $configurationTable->fetchAllConfig();
//Configuration array from db
//Array
//(
// [config] => 'Test1',
// [config2] => 'Test2',
// [config3] => 'Test3',
//)
//What to do here?
//I want to use the configurations above like $sm->get('Config')['dynamic_config']['config3'];
}
出于某些原因,我不会使用此方法。
在您的 Module.php 中放置 SQL 查询意味着它们将在每个用户的 EVERY 请求上执行,从而使您的应用程序慢,很慢。
如果您的数据库遭到破坏,所有配置也将被盗。
解决方案是通过带键的数组移动 config/autoload/my_custom_config.local.php
中的所有配置。从那里您始终可以加载它而无需发出单个数据库请求。它将更快更安全,因为该文件将位于您的根文件夹之外,并且攻击服务器总是比攻击数据库难得多。
如果您仍然希望允许用户编辑选项,您可以简单地 include
文件中的一个操作,然后用 foreach 显示它。要保存信息,您可以这样做:
file_put_contents("my_custom_config.local.php", '<?php return ' . var_export($config, true).';');
另一个优点是,如果您按照上面描述的方式加载配置,您还可以通过 $sm->get('Config')['dynamic_config']['config3']
文档中有一节解释 how to manipulate the merged configuration using the specific event ModuleEvent::EVENT_MERGE_CONFIG
Zend\ModuleManager\Listener\ConfigListener
triggers a special event,Zend\ModuleManager\ModuleEvent::EVENT_MERGE_CONFIG
, after merging all configuration, but prior to it being passed to the ServiceManager. By listening to this event, you can inspect the merged configuration and manipulate it.
问题在于服务管理器此时不可用,因为侦听器事件是模块管理器以优先级 1000 触发的首批事件之一。
这意味着您无法执行查询并将之前的配置合并到传递给服务管理器的配置,您需要在之后这样做.
也许我误解了您的要求,但我会以不同的方式处理。
您可以将需要配置 $serviceManager->get('config')
的任何调用替换为 $serviceManager->get('MyApplicationConfig');
,这将是您自己的配置服务,该服务使用合并的应用程序配置,然后添加到其中。
例如,您可以在 module.config.php
.
return [
'service_manager' => [
'factories' => [
'MyApplicationConfig' => 'MyApplicationConfig\Factory\MyApplicationConfigFactory',
]
],
];
并创建一个工厂来加载合并的模块配置,进行任何数据库调用或缓存等。
class MyApplicationConfigFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $sm)
{
$config = $sm->get('config');
$dbConfig = $this->getDatabaseConfigAsArray($sm);
return array_replace_recursive($config, $dbConfig);
}
protected function getDatabaseConfigAsArray(ServiceLocatorInterface $sm)
{}
}
您还有一个额外的好处,即服务是延迟加载的。