Last active
August 29, 2015 14:07
-
-
Save cornernote/a6d70f0d3a2b71411596 to your computer and use it in GitHub Desktop.
Yii Base Extended
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Include the Yii Framework | |
*/ | |
require_once(YII_PATH . DS . 'YiiBase.php'); | |
/** | |
* Yii is a helper class serving common framework functionalities. | |
*/ | |
class Yii extends YiiBase | |
{ | |
/** | |
* Creates a CWebApplication, in addition it will remove config items | |
* that are incompatibale with CWebApplication. | |
* @param null $config | |
* @return CWebApplication | |
* @throws CException if it is called from CLI | |
*/ | |
public static function createWebApplication($config = null) | |
{ | |
// load the config array | |
$config = self::loadConfig($config); | |
// remove incompatibale items | |
$excludeItems = array('commandMap'); | |
foreach ($excludeItems as $excludeItem) | |
if (array_key_exists($excludeItem, $config)) | |
unset($config[$excludeItem]); | |
return self::createApplication('CWebApplication', $config); | |
} | |
/** | |
* Creates a CConsoleApplication, in addition it will remove config items | |
* that are incompatibale with CConsoleApplication. | |
* @param null $config | |
* @return CConsoleApplication | |
* @throws CException if it is not called from CLI | |
*/ | |
public static function createConsoleApplication($config = null) | |
{ | |
// load the config array | |
$config = self::loadConfig($config); | |
// remove incompatibale items | |
$excludeItems = array('theme', 'controllerMap'); | |
foreach ($excludeItems as $excludeItem) | |
if (array_key_exists($excludeItem, $config)) | |
unset($config[$excludeItem]); | |
// create app | |
$app = self::createApplication('CConsoleApplication', $config); | |
// fix for absolute url | |
//$app->getRequest()->setBaseUrl(WWW_URL); | |
// add Yii commands | |
$app->commandRunner->addCommands(YII_PATH . '/cli/commands'); | |
$env = @getenv('YII_CONSOLE_COMMANDS'); | |
if (!empty($env)) | |
$app->commandRunner->addCommands($env); | |
return $app; | |
} | |
/** | |
* Config can be a string, in which case a file and optional local file override are loaded. | |
* The files should return arrays. | |
* @param $config | |
* @return array|mixed | |
*/ | |
public static function loadConfig($config) | |
{ | |
if (!is_string($config)) | |
return $config; | |
return require($config); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment