Fecshop 2017-04-10 14:43:34 4676次浏览 1条评论 0 1 0

原文链接: yii2-配置加速-n个配置文件生成一个配置文件
yii2的配置方式是N个配置文件,最终通过merge函数合并成一个php数组,如果只有几个配置文件,
不会占用太多资源,如果像fecshop这类电商系统,有大量的配置文件,因此需要合并N个配置文件成一个配置文件,具体实现如下:

首先新建文件@app/merge_config.php 设置可写权限, 然后新建文件 @app/web/index-merge-config.php,
将index.php的内容复制过来,在 代码:$config = yii\helpers\ArrayHelper::merge函数后面,
添加下面的代码,这些代码的用处是将$config数组的内容重新写成php代码保存到文件@app/merge_config.php中

    $str = '<?php '.PHP_EOL;
    $str .= 'return '.PHP_EOL;
    function toPhpCode($arr,$i=0){
      $i++;
      $tb_i = $i*4;
      $tb = ($i-1)*4;
      $tb1_str = '';
      $tb2_str = '';
      for($j=0;$j<$tb;$j++){
        $tb1_str .= ' ';
      }
      for($jj=0;$jj<$tb_i;$jj++){
        $tb2_str .= ' ';
      }
      $mystr = '';
      if(is_array($arr) && !empty($arr)){
        $mystr .= PHP_EOL .$tb1_str.'['.PHP_EOL;
        $t_arr = [];
        foreach($arr as $k => $v){
          $key   = '';
          $value   = '';
          if(is_string($k)){
            $key = "'".$k."'";
          }else{
            $key = $k;
          }
          if(is_array($v) && !empty($v)){
            $value = toPhpCode($v,$i);
          }else if(is_array($v) && empty($v)){
            $value = '[]';
          
          }else{
            if(is_string($v)){
              $value = "'".$v."'";
            }else if(is_bool($v)){
              if($v){
                $value = 'true';
              }else{
                $value = 'false';
              }
            }else{
              if(is_null($v)){
                $v = "''";
              }
              $value = $v;
            }
            
          }
          $t_arr[] = $tb2_str.$key."=>".$value;
        }
        $mystr .= implode(','.PHP_EOL,$t_arr);
        $mystr .= PHP_EOL .$tb1_str.']'. PHP_EOL;
      }
      return $mystr;
    }
    $str .= toPhpCode($config);
    $str .= ';';
    echo 'generate merge config file success';
    file_put_contents('../merge_config.php', $str);
    echo 'generate merge config file success';

执行这个文件后,就会将$config数组的内容,以php代码的方式写入到merge_config.php,这样就生成了单文件配置。

在index.php中加载单文件配置

$use_merge_config_file = true;

    if($use_merge_config_file){
      $config = require('../merge_config.php');
    }else{
      $config = yii\helpers\ArrayHelper::merge(
        require(__DIR__ . '/../../common/config/main.php'),
        require(__DIR__ . '/../../common/config/main-local.php'),
        require(__DIR__ . '/../config/main.php'),
        require(__DIR__ . '/../config/main-local.php'),
        # fecshop services config
        require(__DIR__ . '/../../vendor/fancyecommerce/fecshop/config/fecshop.php'),
        # fecshop module config
        require(__DIR__ . '/../../vendor/fancyecommerce/fecshop/app/appfront/config/appfront.php'),
        
        # thrid part confing
        
        # common modules and services.
        require(__DIR__ . '/../../common/config/fecshop_local.php'),
         
        # appadmin local modules and services.
        require(__DIR__ . '/../config/fecshop_local.php')
        
      );
    }

这样就完成了。

下面fecshop的代码我粘贴一下:(供参考)

index.php:

    <?php
    error_reporting(E_ALL || ~E_NOTICE); //除去 E_NOTICE 之外的所有错误信息
    ini_set('session.cookie_domain', '.fancyecommerce.com'); //初始化域名,
    $http = ($_SERVER['SERVER_PORT'] == 443) ? 'https' : 'http';
    $homeUrl = $http.'://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['SCRIPT_NAME']), '\\/');
    /**
     * fecshop 使用合并配置(config)数组进行加速,true 代表打开。
     * 打开配置加速开关前,您需要执行 http://domain/index-merge-config.php 进行生成单文件配置数组。
     * 注意:打开后,当您修改了配置,都需要访问一次上面的链接,重新生成单文件配置数组,否则修改的配置不会生效
     * 建议:本地开发环境关闭,开发环境如果访问量不大,关闭也行,如果访问量大,建议打开
     * 
     */
    $use_merge_config_file = false; 
     
    defined('YII_DEBUG') or define('YII_DEBUG', true);
    defined('YII_ENV') or define('YII_ENV', 'dev');
    require(__DIR__ . '/../../vendor/autoload.php');
    require(__DIR__ . '/../../vendor/fancyecommerce/fecshop/yii/Yii.php');
    require(__DIR__ . '/../../common/config/bootstrap.php');
    require(__DIR__ . '/../config/bootstrap.php');
    if($use_merge_config_file){
      $config = require('../merge_config.php');
    }else{
      $config = yii\helpers\ArrayHelper::merge(
        require(__DIR__ . '/../../common/config/main.php'),
        require(__DIR__ . '/../../common/config/main-local.php'),
        require(__DIR__ . '/../config/main.php'),
        require(__DIR__ . '/../config/main-local.php'),
        # fecshop services config
        require(__DIR__ . '/../../vendor/fancyecommerce/fecshop/config/fecshop.php'),
        # fecshop module config
        require(__DIR__ . '/../../vendor/fancyecommerce/fecshop/app/appfront/config/appfront.php'),
        
        # thrid part confing
        
        # common modules and services.
        require(__DIR__ . '/../../common/config/fecshop_local.php'),
         
        # appadmin local modules and services.
        require(__DIR__ . '/../config/fecshop_local.php')
        
      );
    }
    $config['homeUrl'] = $homeUrl;
    /**
     * yii class Map Custom
     * 
     */ 
    $yiiClassMap = require(__DIR__ . '/../config/YiiClassMap.php');
    if(is_array($yiiClassMap) && !empty($yiiClassMap)){
      foreach($yiiClassMap as $namespace => $filePath){
        Yii::$classMap[$namespace] = $filePath;
      }
    }
    /**
     * 添加fecshop的服务 ,Yii::$service  ,  将services的配置添加到这个对象。
     * 使用方法:Yii::$service->cms->article;
     * 上面的例子就是获取cms服务的子服务article。
     */
    new fecshop\services\Application($config['services']);
    unset($config['services']);
    $application = new yii\web\Application($config);
    $application->run();

index-merge-config.php

    <?php
    error_reporting(E_ALL ); //除去 E_NOTICE 之外的所有错误信息
    ini_set('session.cookie_domain', '.fancyecommerce.com'); //初始化域名,
    $http = ($_SERVER['SERVER_PORT'] == 443) ? 'https' : 'http';
    defined('YII_DEBUG') or define('YII_DEBUG', true);
    defined('YII_ENV') or define('YII_ENV', 'dev');
    require(__DIR__ . '/../../vendor/autoload.php');
    require(__DIR__ . '/../../vendor/fancyecommerce/fecshop/yii/Yii.php');
    require(__DIR__ . '/../../common/config/bootstrap.php');
    require(__DIR__ . '/../config/bootstrap.php');
    $config = yii\helpers\ArrayHelper::merge(
        require(__DIR__ . '/../../common/config/main.php'),
        require(__DIR__ . '/../../common/config/main-local.php'),
        require(__DIR__ . '/../config/main.php'),
        require(__DIR__ . '/../config/main-local.php'),
      # fecshop services config
      require(__DIR__ . '/../../vendor/fancyecommerce/fecshop/config/fecshop.php'),
      # fecshop module config
      require(__DIR__ . '/../../vendor/fancyecommerce/fecshop/app/appfront/config/appfront.php'),
      
      # thrid part confing
      
      # common modules and services.
      require(__DIR__ . '/../../common/config/fecshop_local.php'),
       
      # appadmin local modules and services.
      require(__DIR__ . '/../config/fecshop_local.php')
        
    );
    $str = '<?php '.PHP_EOL;
    $str .= 'return '.PHP_EOL;
    function toPhpCode($arr,$i=0){
      $i++;
      $tb_i = $i*4;
      $tb = ($i-1)*4;
      $tb1_str = '';
      $tb2_str = '';
      for($j=0;$j<$tb;$j++){
        $tb1_str .= ' ';
      }
      for($jj=0;$jj<$tb_i;$jj++){
        $tb2_str .= ' ';
      }
      $mystr = '';
      if(is_array($arr) && !empty($arr)){
        $mystr .= PHP_EOL .$tb1_str.'['.PHP_EOL;
        $t_arr = [];
        foreach($arr as $k => $v){
          $key   = '';
          $value   = '';
          if(is_string($k)){
            $key = "'".$k."'";
          }else{
            $key = $k;
          }
          if(is_array($v) && !empty($v)){
            $value = toPhpCode($v,$i);
          }else if(is_array($v) && empty($v)){
            $value = '[]';
          
          }else{
            if(is_string($v)){
              $value = "'".$v."'";
            }else if(is_bool($v)){
              if($v){
                $value = 'true';
              }else{
                $value = 'false';
              }
            }else{
              if(is_null($v)){
                $v = "''";
              }
              $value = $v;
            }
            
          }
          $t_arr[] = $tb2_str.$key."=>".$value;
        }
        $mystr .= implode(','.PHP_EOL,$t_arr);
        $mystr .= PHP_EOL .$tb1_str.']'. PHP_EOL;
      }
      return $mystr;
    }
    $str .= toPhpCode($config);
    $str .= ';';
    echo 'generate merge config file success';
    file_put_contents('../merge_config.php', $str);
    echo 'generate merge config file success';
觉得很赞
您需要登录后才可以评论。登录 | 立即注册