Skip to content

Instantly share code, notes, and snippets.

@cornernote
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cornernote/a6d70f0d3a2b71411596 to your computer and use it in GitHub Desktop.
Save cornernote/a6d70f0d3a2b71411596 to your computer and use it in GitHub Desktop.
Yii Base Extended
<?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