From c40a34610a21349f2a4605cfcd30a29524904c66 Mon Sep 17 00:00:00 2001 From: Denis Chenu Date: Wed, 1 Feb 2017 00:15:17 +0100 Subject: [PATCH] Fixed issue #12066: get whole config in command action Dev: near same logic than WebApp --- application/commands/console.php | 27 ++++--------- application/config/console.php | 36 +++++++++++++++++ application/core/ConsoleApplication.php | 54 ++++++++++++++++++++++--- 3 files changed, 92 insertions(+), 25 deletions(-) create mode 100644 application/config/console.php diff --git a/application/commands/console.php b/application/commands/console.php index 6ac80674bc8..b9f95d17110 100644 --- a/application/commands/console.php +++ b/application/commands/console.php @@ -15,8 +15,8 @@ * File edited by Sam Mousa for Marcel Minke. * This loader bypasses the default Yii loader and loads a custom console class instead. */ - if (!isset($argv[0])) die(); - define('BASEPATH','.'); + if (!isset($argv[0])) die(); + define('BASEPATH','.'); /** * Load Psr4 autoloader, should be replaced by composer autoloader at some point. */ @@ -26,29 +26,18 @@ $loader->addNamespace('ls\\pluginmanager', __DIR__ . '/../libraries/PluginManager'); $loader->addNamespace('ls\\pluginmanager', __DIR__ . '/../libraries/PluginManager/Storage'); require_once(dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'framework' . DIRECTORY_SEPARATOR . 'yii.php'); - // Load configuration. - $sCurrentDir=dirname(__FILE__); - $settings=require (dirname($sCurrentDir).DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config-defaults.php'); - $config=require (dirname($sCurrentDir).DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'internal.php'); - $core = dirname($sCurrentDir) . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR; - if(isset($config['config'])){ - $settings=array_merge($settings,$config['config']); - } - unset ($config['defaultController']); - unset ($config['config']); - /* fix runtime path, unsure you can lauch function anywhere (if you use php /var/www/limesurvey/... : can be /root/ for config */ - $runtimePath=$settings['runtimedir']; - if(!is_dir($runtimePath) || !is_writable($runtimePath)){ - $runtimePath=str_replace($settings['rootdir'],dirname(dirname(dirname(__FILE__))),$runtimePath); - } - $config['runtimePath']=$runtimePath; + // Load configuration. + $sCurrentDir=dirname(__FILE__); + $config=require (dirname($sCurrentDir).DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'internal.php'); + $core = dirname($sCurrentDir) . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR; // fix for fcgi defined('STDIN') or define('STDIN', fopen('php://stdin', 'r')); defined('YII_DEBUG') or define('YII_DEBUG',true); - + /* specific for web */ + unset($config['defaultController']); if(isset($config)) { diff --git a/application/config/console.php b/application/config/console.php new file mode 100644 index 00000000000..656c4c91b50 --- /dev/null +++ b/application/config/console.php @@ -0,0 +1,36 @@ +getComponent('session'); } - public function __construct($config = null) { - parent::__construct($config); + public function __construct($aApplicationConfig = null) { + + /* Using some config part for app config, then load it before*/ + $baseConfig = require(__DIR__ . '/../config/config-defaults.php'); + if(file_exists(__DIR__ . '/../config/config.php')) + { + $userConfigs = require(__DIR__ . '/../config/config.php'); + if(is_array($userConfigs['config'])) + { + $baseConfig = array_merge($baseConfig, $userConfigs['config']); + } + } + + /* Set the runtime path according to tempdir if needed */ + if(!isset($aApplicationConfig['runtimePath'])){ + $aApplicationConfig['runtimePath']=$baseConfig['tempdir'] . DIRECTORY_SEPARATOR. 'runtime'; + } /* No need to test runtimePath validity : Yii return an exception without issue */ + + /* Construct CWebApplication */ + parent::__construct($aApplicationConfig); // Set webroot alias. Yii::setPathOfAlias('webroot', realpath(Yii::getPathOfAlias('application') . '/../')); - // Load email settings. - $email = require(Yii::app()->basePath. DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'email.php'); - $this->config = array_merge($this->config, $email); - // Now initialize the plugin manager + /* Because we have app now : we have to call again the config : can be done before : no real usage of url in console, but usage of getPathOfAlias */ + $coreConfig = require(__DIR__ . '/../config/config-defaults.php'); + $consoleConfig = require(__DIR__ . '/../config/console.php'); // Only for console : replace some config-defaults + $emailConfig = require(__DIR__ . '/../config/email.php'); + $versionConfig = require(__DIR__ . '/../config/version.php'); + $updaterVersionConfig = require(__DIR__ . '/../config/updater_version.php'); + $lsConfig = array_merge($coreConfig, $emailConfig, $versionConfig, $updaterVersionConfig); + if(file_exists(__DIR__ . '/../config/config.php')) + { + $userConfigs = require(__DIR__ . '/../config/config.php'); + if(is_array($userConfigs['config'])) + { + $lsConfig = array_merge($lsConfig, $userConfigs['config']); + } + } + $this->config = array_merge($this->config, $lsConfig); + + /* Load the database settings : if available */ + try { + $settingsTableExist = Yii::app()->db->schema->getTable('{{settings_global}}'); + if(is_object($settingsTableExist)){ + $dbConfig=CHtml::listData(SettingGlobal::model()->findAll(), 'stg_name', 'stg_value'); + $this->config = array_merge($this->config,$dbConfig); + } + }catch(Exception $exception) { + // Allow exception (install for example) + } + } /**