【问题标题】:ZF2/3 Load Modules from DatabaseZF2/3 从数据库加载模块
【发布时间】:2017-11-16 21:22:53
【问题描述】:

我想知道是否有办法从zend framework 2优选3中的数据库表中加载模块?我希望能够根据数据库表中的状态列动态禁用或启用模块

【问题讨论】:

    标签: zend-framework2 zend-framework3


    【解决方案1】:

    我很确定您可以通过将侦听器附加到某些ModuleManager 事件来完成此操作。 有 v3 https://docs.zendframework.com/zend-modulemanager/module-manager/ 和 v2 https://framework.zend.com/manual/2.1/en/modules/zend.module-manager.module-manager.html 的文档

    别忘了 v3 的自动加载

    【讨论】:

      【解决方案2】:

      通过阅读您的问题tom_cruz,我意识到我有完全相同的问题;-)

      我浏览了 ModuleManager、ModuleManagerFactory、ModuleEvent 和一些监听器的 ZF2 源代码。分析流程后,我的新问题是:

      “我对活动/非活动模块有什么期望?”

      几乎所有重要的事情由提到的Nemutaisama 事件完成。即通过将 getConfig() 方法添加到 Module.php 类来加载配置。

      ATM 我无法回答上述问题。我稍后会回到这个。但现在,我认为这是一个应用程序问题,而不是框架问题。

      【讨论】:

        【解决方案3】:

        前段时间我已经这样做了:

        1. 创建一个“核心”模块,负责从数据库中获取模块。
          1.1 在Module.php中添加模块监听

          public function init(ModuleManagerInterface $manager)
          {
            $sharedEventManger = $manager->getEventManager()->getSharedManager();
            $sharedEventManger->attach(ModuleManager::class, ModuleEvent::EVENT_LOAD_MODULES_POST, new ModuleListener(), 10000); 
          }
          
        2. 我创建的模块监听器类似于:

            public function __invoke(ModuleEvent $event)
            {
             $target = $event->getTarget(); 
             $serverName = $_SERVER['SERVER_NAME'];
          
             if(! $serverName) { return; }
          
             //module ok 
             if(! $target instanceof ModuleManagerInterface) { return; }
          
             //config data
             $configListener = $event->getConfigListener(); 
             $config = $configListener->getMergedConfig(false); 
          
             //app modules
             $modules  = $target->getModules(); 
          
             //select active modules
             $adapter = new Adapter($config['db']); 
             $sql = new Sql($adapter); 
             $select = $sql->select(['c' => 'customers'])
                        ->join(['cm' => 'customers_modules'], 'cm.customer_id = c.id', ['module' => 'module'])
                        ->where(['c.domain' => $serverName])
                        ->where(['cm.active' => 1]); 
          
             $statement = $sql->prepareStatementForSqlObject($select);
             $result    = $statement->execute();
          
             if($result instanceof ResultInterface && $result->isQueryResult() && $result->getAffectedRows()) {
          
              //change db connection params here (if you use different db for customers)
          
              while ($current = $result->current()) {
          
                  if (! in_array($current['module'], $modules)) {
                      try {
                          $target->loadModule($current['module']) ;
                      } catch (\RuntimeException $e) { 
                          $target->loadModule('WQ' . str_replace($current['prefix'], '', $current['module']));
                      }
                      $modules[] = $current['module'];
          
                      $module = $target->getModule($current['module']);
          
                      if (($module instanceof ConfigProviderInterface) || (is_callable([$module, 'getConfig']))) {
                          $moduleConfig = $module->getConfig();
          
                          $config = ArrayUtils::merge($config, $moduleConfig);
                      }
                  }
          
                  $result->next(); 
              }
          }
          
          $target->setModules($modules);
          $configListener->setMergedConfig($config);
          
          }
          

        希望它有用。

        【讨论】:

          猜你喜欢
          • 2013-10-22
          • 1970-01-01
          • 1970-01-01
          • 2014-07-13
          • 2012-10-19
          • 1970-01-01
          • 2023-03-24
          • 1970-01-01
          • 2016-04-21
          相关资源
          最近更新 更多