📜 ⬆️ ⬇️

Different configs for production mode and debug mode. Two in one

Hello, dear habrakrady.
From your own experience, you probably already know how important it is to minimize the changes that the debugging mode introduces to your application code. If you are accompanying a fairly large and already running project, then all the more you understand the importance of quick debugging, in which you don’t have to clean up any changes made for ease of development.
Investigating the possibilities of Yii, my friend did not find the functional that realizes this possibility, and set out to fix it. In this topic, we will learn how quickly and safely for your application to change the config (its working version) to the needs of the developer. Details - under the cut.
It just became known that this is in the Yii Kukbook.

Problem


You do not want to have multiple configs. This is inconvenient, because when adding a project, you will have to change all the parameters in the new config that were changed for greater convenience at the design stage each time. It is much more convenient to have one working version of the config, and change it for the time of development.

Decision


And the solution is very simple: we don’t need to create a copy of the config if we can use inheritance by applying it to the array returned by the config file. Each instance of the application, called from the local computer, will be created with the local version of the config, and the instance created by the request from the remote computer will start with the parameters from the working version.
Let us turn to examples. Here is the updated input file of our application - index.php:
<?php $yii=dirname(__FILE__).'/../yii/framework/yii.php'; if($_SERVER['REMOTE_ADDR']=='127.0.0.1') { //   , defined('YII_DEBUG') or define('YII_DEBUG',true); defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3); //     $config=dirname(__FILE__).'/protected/config/develop.php'; } else { //        $config=dirname(__FILE__).'/protected/config/main.php'; } require_once($yii); Yii::createWebApplication($config)->run(); 

Now let's look at the file '/protected/config/develop.php' mentioned above. If you look closely at its logic, you will understand that there is no invention of a bicycle, and the whole idea is very simple and consists of inheriting arrays:
 <?php //    //     ,      function array_extends(array $array1, array $array2) { foreach($array2 as $key=>$value) { if(is_int($key)) $array1[]=$value; else if(empty($array1[$key]) || !is_array($value)) $array1[$key]=$value; else if(is_array($value)) $array1[$key]=array_extends($array1[$key], $value); } return $array1; } //        //,  ,       //         return array_extends(include('main.php'), array( //   'name'=>'Local version', 'import'=>array( //      import 'application.extensions.yiidebugtb.*' 'application.extensions.yiidebugtb.* ), 'modules'=>array( //   gii   ?     'gii'=>array( 'class'=>'system.gii.GiiModule', 'password'=>'password', ... ), ), 'components'=>array( 'db'=>array( //  connectionString      'connectionString' => 'mysql:host=localhost;dbname=mycms', //         . 'username' => 'root', //         . 'password' => '', //    'enableProfiling'=>true, /* 'tablePrefix' => 'tbl_',          */ ), 'log'=>array( //          'routes'=>array( array( 'class'=>'XWebDebugRouter', ... ), array( 'class'=>'CWebLogRoute', ... ), ) ), ), )); 

I think everyone who has come across the Yii Framework has certainly dealt with the protected / config / main.php file, but I will nevertheless explain: this file returns an array of parameters that ultimately apply to the running application instance. For clarity, I will provide a very abbreviated code sample from this file:
 <?php return array( 'name'=>'Working Version', ... 'import'=>array( 'application.models.*', 'application.components.*', ), 'modules'=>array( // uncomment the following to enable the Gii tool 'admin', ), ... // application components 'components'=>array( 'db'=>array( 'connectionString' => 'mysql:host=localhost;dbname=host_db', 'emulatePrepare' => true, 'username' => 'hosh_login', 'password' => 'hosh_password', 'charset' => 'utf8', 'tablePrefix' => 'tbl_', ), 'log'=>array( 'class'=>'CLogRouter', 'routes'=>array( array( 'class'=>'CFileLogRoute', 'levels'=>'error, warning', ), ), ), ), ... ); 

As you can see, the idea is really very simple, but it can be very useful, especially with regard to projects that are already running.

TheAlien suggested that this solution is already implemented link

Thanks for attention.

')

Source: https://habr.com/ru/post/125677/


All Articles