diff --git a/www/index.php b/www/index.php index fdcf65d..7ec7f47 100644 --- a/www/index.php +++ b/www/index.php @@ -1,13 +1,16 @@ getDebug()); +defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', $env->getTraceLevel()); + +// run Yii app +require_once($env->getYiiPath()); +Yii::createWebApplication($env->getConfig())->run(); -// change the following paths if necessary -$yii=dirname(__FILE__).'/../yii/framework/yii.php'; -$config=dirname(__FILE__).'/protected/config/main.php'; - -// remove the following lines when in production mode -defined('YII_DEBUG') or define('YII_DEBUG',true); -// specify how many levels of call stack should be shown in each log message -defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3); - -require_once($yii); -Yii::createWebApplication($config)->run(); +?> \ No newline at end of file diff --git a/www/protected/config/Environment.php b/www/protected/config/Environment.php new file mode 100644 index 0000000..94f18c0 --- /dev/null +++ b/www/protected/config/Environment.php @@ -0,0 +1,209 @@ +getDebug()); + * defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', $env->getTraceLevel()); + * + * // run Yii app + * require_once($env->getYiiPath()); + * Yii::createWebApplication($env->getConfig())->run(); + * + ******************************************************************************* + * + * Modify your config/main.php like this: + * + * // Set yiiPath (add extra ../../) + * $yiiPath = dirname(__FILE__) . '/../../../yii/framework/yii.php'; + * + * // Set YII_DEBUG and YII_TRACE_LEVEL flags + * $debug = true; + * $traceLevel = 0; + * + * // Set config array + * $config = array( + * + ******************************************************************************* + * + * Create config/mode_.php files for the different modes + * These will override or merge attributes + * + * // Set yiiPath (add extra ../../) + * //$yiiPath = dirname(__FILE__) . '/../../../yii/framework/yii.php'; + * + * // Set YII_DEBUG and YII_TRACE_LEVEL flags + * $debug = false; + * $traceLevel = 0; + * + * // Set specific config + * $configSpecific = array( + * + ******************************************************************************* + * + * Usage example for checking environment in code: + * + * // Compare if the current environment is lower than production + * $currentEnvironment = Yii::app()->getParams()->environment; //gets integer + * if ($currentEnvironment < Environment::PRODUCTION) { + * //do this + * } else { + * //do that + * } + * + ******************************************************************************* + * + * Original sources: http://www.yiiframework.com/doc/cookbook/73/ + * + * @name Environment + * @author Marco van 't Wout | Tremani + * @version 1.0 + * @property string mode the current environment mode + * @property boolean debug the debug flag + * @property integer traceLevel the trace level + * @property string yiiPath path to the Yii framework + * @property string config the merged config array + * + */ +class Environment +{ + + // $_SERVER[SERVER_VAR] + const SERVER_VAR = 'YII_ENVIRONMENT'; + const MAIN_CONFIG = 'main.php'; + + // Modes + const DEVELOPMENT = 100; + const TEST = 200; + const STAGE = 300; + const PRODUCTION = 400; + + // Current mode + private $_mode; + + // Environment properties + private $_debug; + private $_traceLevel; + private $_yiiPath; + private $_config; + + /** + * Initilizes the Environment class with the given mode + * @param constant $mode + */ + function __construct($mode = null) + { + $this->_mode = $this->getMode($mode); + $this->setEnvironment(); + } + + /** + * Get current environment mode depending on environment variable + * @param $mode + * @return + */ + private function getMode($mode = null) + { + // If not overriden + if (!isset($mode)) + { + // Return mode based on Apache server var + if (isset($_SERVER[self::SERVER_VAR])) + $mode = $_SERVER[self::SERVER_VAR]; + else + throw new Exception('SetEnv YII_ENVIRONMENT not defined in Apache config.'); + } + + // Check if mode is valid + if (!defined('self::' . $mode)) + throw new Exception('Invalid Environment mode'); + + return $mode; + } + + /** + * Sets the environment and configuration for the chosen mode + * @param constant $mode + */ + private function setEnvironment() + { + // Load main config + $configMainFile = dirname(__FILE__) . DIRECTORY_SEPARATOR . self::MAIN_CONFIG; + if (!file_exists($configMainFile)) + throw new Exception('Cannot find config file "' . $configMainFile . '".'); + require($configMainFile); + + // Load specific config + $configFile = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mode_' . strtolower($this->_mode) . '.php'; + if (!file_exists($configFile)) + throw new Exception('Cannot find config file "' . $configFile . '".'); + require($configFile); + + // Merge config arrays into one + //$this->_config = array_merge_recursive($config, $configSpecific); + require_once(dirname($yiiPath). DIRECTORY_SEPARATOR . 'base' . DIRECTORY_SEPARATOR . 'CComponent.php'); + require_once(dirname($yiiPath). DIRECTORY_SEPARATOR . 'collections' . DIRECTORY_SEPARATOR . 'CMap.php'); + $this->_config = CMap::mergeArray($config, $configSpecific); + + // Set other environment vars + $this->_yiiPath = $yiiPath; + $this->_debug = $debug; + $this->_traceLevel = $traceLevel; + $this->_config['params']['environment'] = constant('self::' . $this->_mode); + } + + /** + * Returns the path to the Yii framework + * @return string + */ + public function getYiiPath() + { + return $this->_yiiPath; + } + + /** + * Returns the debug mode for YII_DEBUG + * @return Bool + */ + public function getDebug() + { + return $this->_debug; + } + + /** + * Returns the trace level for YII_TRACE_LEVEL + * @return int + */ + public function getTraceLevel() + { + return $this->_traceLevel; + } + + /** + * Returns the configuration array + * @return array + */ + public function getConfig() + { + return $this->_config; + } + +} + +?> \ No newline at end of file diff --git a/www/protected/config/main.php b/www/protected/config/main.php index 38b9a74..16eec31 100644 --- a/www/protected/config/main.php +++ b/www/protected/config/main.php @@ -1,13 +1,15 @@ dirname(__FILE__).DIRECTORY_SEPARATOR.'..', - 'name'=>'My Web Application', + 'name'=>'Intranet', 'sourceLanguage' =>'en_US', 'language' =>'es', @@ -22,15 +24,6 @@ return array( ), 'modules'=>array( - // uncomment the following to enable the Gii tool - - 'gii'=>array( - 'class'=>'system.gii.GiiModule', - 'password'=>'password', - // If removed, Gii defaults to localhost only. Edit carefully to taste. - 'ipFilters'=>array('127.0.0.1','::1'), - ), - ), // application components @@ -48,32 +41,10 @@ return array( '/'=>'/', ), ), - 'db'=>array( - 'connectionString' => 'mysql:host=localhost;dbname=intranet_dev', - 'emulatePrepare' => true, - 'username' => 'root', - 'password' => '', - 'charset' => 'utf8', - ), 'errorHandler'=>array( // use 'site/error' action to display errors 'errorAction'=>'site/error', ), - 'log'=>array( - 'class'=>'CLogRouter', - 'routes'=>array( - array( - 'class'=>'CFileLogRoute', - 'levels'=>'error, warning', - ), - // uncomment the following to show log messages on web pages - /* - array( - 'class'=>'CWebLogRoute', - ), - */ - ), - ), 'widgetFactory'=>array( 'widgets'=>array( 'CLinkPager'=>array( @@ -87,7 +58,7 @@ return array( // using Yii::app()->params['paramName'] 'params'=>array( // PageSize extension - 'defaultPageSize' => 10, + 'defaultPageSize' => 50, 'pageSizeOptions' => array(10=>10,20=>20,50=>50,100=>100), ), ); \ No newline at end of file diff --git a/www/protected/config/mode_development.php b/www/protected/config/mode_development.php new file mode 100644 index 0000000..49ffd78 --- /dev/null +++ b/www/protected/config/mode_development.php @@ -0,0 +1,67 @@ + array( + 'gii' => array( + 'class' => 'system.gii.GiiModule', + 'password' => 'password', + ), + ), + + // Application components + 'components' => array( + + // Database + 'db' => array( + 'connectionString' => 'mysql:host=localhost;dbname=intranet_dev', + 'emulatePrepare' => true, + 'username' => 'root', + 'password' => '', + 'charset' => 'utf8', + ), + + // Application Log + 'log' => array( + 'class' => 'CLogRouter', + 'routes' => array( + // Save log messages on file + array( + 'class' => 'CFileLogRoute', + 'levels' => 'error, warning, trace, info', + ), + // Show log messages on web pages + array( + 'class' => 'CWebLogRoute', + //'categories' => 'system.db.CDbCommand', + 'levels' => 'error, warning, trace, info', + //'showInFireBug' => true, + ), + ), + ), + + ), + +); + +?> \ No newline at end of file diff --git a/www/protected/config/mode_production.php b/www/protected/config/mode_production.php new file mode 100644 index 0000000..6a27d56 --- /dev/null +++ b/www/protected/config/mode_production.php @@ -0,0 +1,55 @@ + array( + + // Database + 'db' => array( + 'connectionString' => 'mysql:host=PRODUCTION_HOST;dbname=PRODUCTION_DBNAME', + 'username' => 'USERNAME', + 'password' => 'PASSWORD', + ), + + // Application Log + 'log' => array( + 'class' => 'CLogRouter', + 'routes' => array( + // Save log messages on file + array( + 'class' => 'CFileLogRoute', + 'levels' => 'error, warning, trace, info', + ), + // Send errors via email to the system admin + array( + 'class' => 'CEmailLogRoute', + 'levels' => 'error, warning', + 'emails' => 'marco@example.com', + ), + ), + ), + + ), + +); + +?> \ No newline at end of file diff --git a/www/protected/config/mode_stage.php b/www/protected/config/mode_stage.php new file mode 100644 index 0000000..36bfd44 --- /dev/null +++ b/www/protected/config/mode_stage.php @@ -0,0 +1,49 @@ + array( + + // Database + 'db' => array( + 'connectionString' => 'mysql:host=STAGING_HOST;dbname=STAGING_DBNAME', + 'username' => 'USERNAME', + 'password' => 'PASSWORD', + ), + + // Application Log + 'log' => array( + 'class' => 'CLogRouter', + 'routes' => array( + // Save log messages on file + array( + 'class' => 'CFileLogRoute', + 'levels' => 'error, warning, trace, info', + ), + ), + ), + + ), + +); + +?> \ No newline at end of file diff --git a/www/protected/config/mode_test.php b/www/protected/config/mode_test.php new file mode 100644 index 0000000..c9faee0 --- /dev/null +++ b/www/protected/config/mode_test.php @@ -0,0 +1,63 @@ + array( + + // Database + 'db'=>array( + 'connectionString' => 'mysql:host=localhost;dbname=intranet_test', + 'emulatePrepare' => true, + 'username' => 'root', + 'password' => '', + 'charset' => 'utf8', + ), + + // Fixture Manager for testing + 'fixture' => array( + 'class' => 'system.test.CDbFixtureManager', + ), + + // Application Log + 'log' => array( + 'class' => 'CLogRouter', + 'routes' => array( + // Save log messages on file + array( + 'class' => 'CFileLogRoute', + 'levels' => 'error, warning, trace, info', + ), + // Show log messages on web pages + array( + 'class' => 'CWebLogRoute', + //'categories' => 'system.db.CDbCommand', + 'levels' => 'error, warning', + ), + ), + ), + + ), + +); + + +?> \ No newline at end of file diff --git a/www/protected/config/test.php b/www/protected/config/test.php deleted file mode 100644 index 344ea5a..0000000 --- a/www/protected/config/test.php +++ /dev/null @@ -1,19 +0,0 @@ -array( - 'fixture'=>array( - 'class'=>'system.test.CDbFixtureManager', - ), - 'db'=>array( - 'connectionString' => 'mysql:host=localhost;dbname=intranet_test', - 'emulatePrepare' => true, - 'username' => 'root', - 'password' => '', - 'charset' => 'utf8', - ), - ), - ) -); diff --git a/www/protected/tests/bootstrap.php b/www/protected/tests/bootstrap.php index 165b592..bb0194d 100644 --- a/www/protected/tests/bootstrap.php +++ b/www/protected/tests/bootstrap.php @@ -1,10 +1,14 @@ getConfig(); + +// run Yii app require_once($yiit); require_once(dirname(__FILE__).'/WebTestCase.php'); -Yii::createWebApplication($config); +Yii::createWebApplication($config); \ No newline at end of file