数据库中的ZF2/3加载模块 [英] ZF2/3 Load Modules from Database

查看:63
本文介绍了数据库中的ZF2/3加载模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以从zend Framework 2(最好是3)的数据库表中加载模块?我希望能够根据数据库表内的状态列动态禁用或启用模块

I would like to know if there is a way to load modules from a database table in zend framework 2 preferable 3? I want to be able to dynamically disable or enable modules based on a status column inside a database table

推荐答案

我前一段时间是通过以下方式完成的:

I have done this some time ago by:

  1. 创建一个核心"模块,负责从数据库中获取模块.
    1.1在Module.php中添加模块侦听器

  1. Create a "Core" Module responsible with fetching modules from database.
    1.1 In the Module.php add module listener

public function init(ModuleManagerInterface $manager)
{
  $sharedEventManger = $manager->getEventManager()->getSharedManager();
  $sharedEventManger->attach(ModuleManager::class, ModuleEvent::EVENT_LOAD_MODULES_POST, new ModuleListener(), 10000); 
}

我创建的

  • 模块侦听器类似于:

  • Module Listener created by me was something like:

      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);
    
    }
    

  • 希望它很有用.

    这篇关于数据库中的ZF2/3加载模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆