Yii Framework v1.1.10 绫诲弬鑰

CConsoleCommand

system.console
缁ф壙 abstract class CConsoleCommand » CComponent
瀛愮被 CHelpCommand
婧愯嚜 1.0
鐗堟湰 $Id: CConsoleCommand.php 3548 2012-01-24 11:42:59Z mdomba $
婧愮爜 framework/console/CConsoleCommand.php
CConsoleCommand浠h〃涓涓彲鎵ц鐨勬帶鍒跺彴鍛戒护銆

瀹冪殑宸ヤ綔鍘熺悊璺CController涓鏍凤紝閫氳繃瑙f瀽鍛戒护琛岄夐」鍜屾牴鎹傚綋鐨勯夐」鍊煎皢 璇锋眰璋冨害鍒颁竴涓寚瀹氱殑鍔ㄤ綔銆

鐢ㄦ埛閫氳繃浠ヤ笅鍛戒护鏍煎紡鏉ヨ皟鐢ㄦ帶鍒跺彴鍛戒护锛
yiic CommandName ActionName --Option1=Value1 --Option2=Value2 ...


瀛愮被涓昏瀹炵幇鍚勭鍔ㄤ綔鏂规硶锛岃繖涓姩浣滄柟娉曞悕蹇 椤讳互鈥渁ction鈥濅綔涓哄墠缂銆 浼犻掔粰鍔ㄤ綔鏂规硶鐨勫弬鏁拌璁や负鏄寚瀹氬姩浣滅殑閫夐」銆 閫氳繃鎸囧畾defaultAction鏉ュ疄鐜板綋鐢ㄦ埛娌℃湁鍦ㄥ懡浠ら噷闈㈡槑纭皟鐢ㄦ煇涓懡浠ゆ椂 榛樿浼氳皟鐢ㄥ摢涓姩浣溿

閫夐」閫氳繃鍙傛暟鍚嶇О缁戝畾鍒板姩浣滃弬鏁般備緥濡傦紝涓嬮潰 鐨勫姩浣滄柟娉曚細鍏佽鎴戜滑杩愯鍛戒护yiic sitemap --type=News:
class SitemapCommand {
    public function actionIndex($type) {
        ....
    }
}

鍏叡灞炴

闅愯棌缁ф壙灞炴

灞炴绫诲瀷鎻忚堪瀹氫箟鍦
commandRunner CConsoleCommandRunner 鍛戒护鎵ц瀵硅薄鐨勫疄渚 CConsoleCommand
defaultAction string 榛樿鍔ㄤ綔鐨勫悕瀛椼 CConsoleCommand
help string 鎻愪緵鍛戒护鎻忚堪銆 CConsoleCommand
name string 鍛戒护鍚嶅瓧銆 CConsoleCommand
optionHelp array 鎻愪緵鍛戒护甯姪淇℃伅閫夐」銆 CConsoleCommand

鍏叡鏂规硶

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
__call() 濡傛灉绫讳腑娌℃湁璋冪殑鏂规硶鍚嶏紝鍒欒皟鐢ㄨ繖涓柟娉曘 CComponent
__construct() 鏋勯犳柟娉曘 CConsoleCommand
__get() 杩斿洖涓涓睘鎬у笺佷竴涓簨浠跺鐞嗙▼搴忓垪琛ㄦ垨涓涓涓哄悕绉般 CComponent
__isset() 妫鏌ヤ竴涓睘鎬ф槸鍚︿负null銆 CComponent
__set() 璁剧疆涓涓粍浠剁殑灞炴у笺 CComponent
__unset() 璁剧疆涓涓粍浠剁殑灞炴т负null銆 CComponent
asa() 杩斿洖杩欎釜鍚嶅瓧鐨勮涓哄璞° CComponent
attachBehavior() 闄勫姞涓涓涓哄埌缁勪欢銆 CComponent
attachBehaviors() 闄勫姞涓涓涓哄垪琛ㄥ埌缁勪欢銆 CComponent
attachEventHandler() 涓轰簨浠堕檮鍔犱竴涓簨浠跺鐞嗙▼搴忋 CComponent
buildFileList() 寤虹珛鐩綍鐨勬枃浠跺垪琛ㄣ CConsoleCommand
canGetProperty() 纭畾灞炴ф槸鍚﹀彲璇汇 CComponent
canSetProperty() 纭畾灞炴ф槸鍚﹀彲鍐欍 CComponent
confirm() 閫氳繃杈撳叆y鎴栬卬鏉ヨ闂敤鎴风‘瀹氥 CConsoleCommand
copyFiles() 灏嗘枃浠朵粠涓涓湴鏂瑰鍒跺埌鍙︿竴涓湴鏂广 CConsoleCommand
detachBehavior() 浠庣粍浠朵腑鍒嗙涓涓涓恒 CComponent
detachBehaviors() 浠庣粍浠朵腑鍒嗙鎵鏈夎涓恒 CComponent
detachEventHandler() 鍒嗙涓涓瓨鍦ㄧ殑浜嬩欢澶勭悊绋嬪簭銆 CComponent
disableBehavior() 绂佺敤涓涓檮鍔犺涓恒 CComponent
disableBehaviors() 绂佺敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
enableBehavior() 鍚敤涓涓檮鍔犺涓恒 CComponent
enableBehaviors() 鍚敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
ensureDirectory() 鎵鏈夌殑鐖剁洰褰曚腑濡傛灉鍝釜涓嶅瓨鍦紝鍒欏垱寤哄畠銆 CConsoleCommand
evaluateExpression() 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 CComponent
getCommandRunner() 杩斿洖鍛戒护鎵ц瀵硅薄鐨勫疄渚 CConsoleCommand
getEventHandlers() 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 CComponent
getHelp() 鎻愪緵鍛戒护鎻忚堪銆 CConsoleCommand
getName() 杩斿洖鍛戒护鍚嶅瓧銆 CConsoleCommand
getOptionHelp() 鎻愪緵鍛戒护甯姪淇℃伅閫夐」銆 CConsoleCommand
hasEvent() 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 CComponent
hasEventHandler() 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 CComponent
hasProperty() 纭畾灞炴ф槸鍚﹁瀹氫箟銆 CComponent
init() 鍒濆鍖栧懡浠ゅ璞° CConsoleCommand
pluralize() 灏嗗崟璇嶈浆鎹负瀹冪殑澶嶆暟褰㈠紡銆 CConsoleCommand
prompt() 濡傛灉readline杩欎釜PHP鎵╁睍鍙敤锛屽垯閫氳繃瀹冭鍙栬緭鍏ョ殑鍐呭锛屽惁鍒欓氳繃fgets()鏉ヨ鍙栥 CConsoleCommand
raiseEvent() 鍙戣捣涓涓簨浠躲 CComponent
renderFile() 娓叉煋涓涓鍥炬枃浠躲 CConsoleCommand
run() 鎵ц鍛戒护銆 CConsoleCommand
usageError() 鏄剧ず涓涓繍琛岄敊璇 CConsoleCommand

鍙椾繚鎶ゆ柟娉

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
afterAction() 杩欎釜鏂规硶浼氬湪涓涓姩浣滄墽琛屽畬鎴愬悗鍙戣捣銆 CConsoleCommand
beforeAction() 杩欎釜鏂规硶浼氬湪涓涓姩浣滄墽琛屽墠鍙戣捣銆 CConsoleCommand
resolveRequest() 瑙f瀽鍛戒护琛屽弬鏁扮劧鍚庡喅瀹氳杩愯鍝釜鍔ㄤ綔銆 CConsoleCommand

灞炴ц缁

commandRunner 灞炴 鍙

鍛戒护鎵ц瀵硅薄鐨勫疄渚

defaultAction 灞炴 锛堝彲鐢ㄨ嚜 v1.1.5锛
public string $defaultAction;

榛樿鍔ㄤ綔鐨勫悕瀛椼

help 灞炴 鍙
public string getHelp()

鎻愪緵鍛戒护鎻忚堪銆 杩欎釜鏂规硶鍙閲嶅啓锛岀劧鍚庤繑鍥炲疄闄呯殑鍛戒护鎻忚堪銆

name 灞炴 鍙
public string getName()

鍛戒护鍚嶅瓧銆

optionHelp 灞炴 鍙 锛堝彲鐢ㄨ嚜 v1.1.5锛
public array getOptionHelp()

鎻愪緵鍛戒护甯姪淇℃伅閫夐」銆 榛樿瀹炵幇鐨勬槸杩斿洖鎵鏈夊彲鐢ㄧ殑鍔ㄤ綔锛 鍚屾椂杩斿洖鐩稿叧鐨勯夐」淇℃伅銆

鏂规硶璇︾粏

__construct() 鏂规硶
public void __construct(string $name, CConsoleCommandRunner $runner)
$name string 鍛戒护鍚嶅瓧
$runner CConsoleCommandRunner 鍛戒护鎵ц瀵硅薄
婧愮爜锛 framework/console/CConsoleCommand.php#64 (鏄剧ず)
public function __construct($name,$runner)
{
    
$this->_name=$name;
    
$this->_runner=$runner;
}

鏋勯犳柟娉曘

afterAction() 鏂规硶
protected void afterAction(string $action, array $params)
$action string 鍔ㄤ綔鍚嶅瓧
$params array 浼犻掔粰鍔ㄤ綔鏂规硶鐨勫弬鏁般
婧愮爜锛 framework/console/CConsoleCommand.php#164 (鏄剧ず)
protected function afterAction($action,$params)
{
}

杩欎釜鏂规硶浼氬湪涓涓姩浣滄墽琛屽畬鎴愬悗鍙戣捣銆 浣犲彲浠ラ噸鍐欒繖涓柟娉曞湪鍔ㄤ綔涔嬪悗鏉ュ仛涓浜涙渶鍚庡鐞嗐

beforeAction() 鏂规硶
protected boolean beforeAction(string $action, array $params)
$action string 鍔ㄤ綔鍚嶅瓧
$params array 浼犻掔粰鍔ㄤ綔鏂规硶鐨勫弬鏁般
{return} boolean 杩斿洖鍊艰鏄庤繖涓姩浣滄槸鍚︿細鎵ц銆
婧愮爜锛 framework/console/CConsoleCommand.php#153 (鏄剧ず)
protected function beforeAction($action,$params)
{
    return 
true;
}

杩欎釜鏂规硶浼氬湪涓涓姩浣滄墽琛屽墠鍙戣捣銆 浣犲彲浠ラ噸鍐欒繖涓柟娉曪紝浣垮畠鍦ㄥ姩浣滄墽琛屽墠鏉ュ仛涓浜涘鐞嗐

buildFileList() 鏂规硶
public array buildFileList(string $sourceDir, string $targetDir, string $baseDir='')
$sourceDir string 婧愮洰褰
$targetDir string 鐩爣鐩綍
$baseDir string 鏍圭洰褰
{return} array 鏂囦欢鍒楄〃锛堝弬鑰copyFiles
婧愮爜锛 framework/console/CConsoleCommand.php#375 (鏄剧ず)
public function buildFileList($sourceDir$targetDir$baseDir='')
{
    
$list=array();
    
$handle=opendir($sourceDir);
    while((
$file=readdir($handle))!==false)
    {
        if(
$file==='.' || $file==='..' || $file==='.svn' ||$file==='.yii')
            continue;
        
$sourcePath=$sourceDir.DIRECTORY_SEPARATOR.$file;
        
$targetPath=$targetDir.DIRECTORY_SEPARATOR.$file;
        
$name=$baseDir===''?$file $baseDir.'/'.$file;
        
$list[$name]=array('source'=>$sourcePath'target'=>$targetPath);
        if(
is_dir($sourcePath))
            
$list=array_merge($list,$this->buildFileList($sourcePath,$targetPath,$name));
    }
    
closedir($handle);
    return 
$list;
}

寤虹珛鐩綍鐨勬枃浠跺垪琛ㄣ 杩欎釜鏂规硶閬嶅巻鎸囧畾鐨勭洰褰曪紝 鐒跺悗寤虹珛鏂囦欢鍒楄〃鍜屽畠浠寘鍚殑瀛愮洰褰曘 杩欎釜鏂规硶鐨勭粨鏋滃彲浠ヤ紶閫掔粰copyFiles

confirm() 鏂规硶 锛堝彲鐢ㄨ嚜 v1.1.9锛
public bool confirm(string $message)
$message string 鍦ㄧ敤鎴疯緭鍏ヤ箣鍓嶈緭鍑虹殑鍐呭
{return} bool 杩斿洖鍊艰鏄庣敤鎴锋槸鍚︾‘瀹氥
婧愮爜锛 framework/console/CConsoleCommand.php#497 (鏄剧ず)
public function confirm($message)
{
    echo 
$message.' [yes|no] ';
    return !
strncasecmp(trim(fgets(STDIN)),'y',1);
}

閫氳繃杈撳叆y鎴栬卬鏉ヨ闂敤鎴风‘瀹氥

copyFiles() 鏂规硶
public void copyFiles(array $fileList)
$fileList array 瑕佸鍒剁殑鏂囦欢鍒楄〃锛堝悕瀛楋紳銆嬭鏄庯級銆 鍦ㄥ鍒惰繃绋嬩腑锛屾暟缁勯敭鍚嶆槸鏄剧ず鍑烘潵鐨勫悕瀛楋紝鏁扮粍閿兼槸澶嶅埗鐨 鏂囦欢鐨勮鏍笺傛瘡涓涓暟缁勫间竴瀹氭槸浠ヤ笅鐨勬暟缁勭粨鏋勶細
  • 婧愶細蹇呴』鎻愪緵, 瑕佸鍒剁殑婧愭枃浠/鐩綍鐨勫畬鏁磋矾寰
  • 鐩綍锛氬繀椤绘彁渚涳紝瑕佸鍒跺幓鐨勭洰鏍囨枃浠/鐩綍鐨勫畬鏁磋矾寰
  • 鍥炶皟: 鍙夛紝褰撳鍒舵枃浠剁殑鏃跺欒璋冪敤鐨勫洖璋冨嚱鏁般 杩欎釜鍥炶皟鍑芥暟搴旇濡備笅闈㈠畾涔夛細
      function foo($source,$params)
      
    $source鍙傛暟鏄簮鏂囦欢璺緞锛 鍑芥暟杩斿洖鐨勫唴瀹逛細琚繚瀛樺埌鐩爣鏂囦欢銆
  • 鍙傛暟锛氬彲閫夛紝瑕佷紶閫掔粰鍥炶皟鐨勫弬鏁般
婧愮爜锛 framework/console/CConsoleCommand.php#306 (鏄剧ず)
public function copyFiles($fileList)
{
    
$overwriteAll=false;
    foreach(
$fileList as $name=>$file)
    {
        
$source=strtr($file['source'],'/\\',DIRECTORY_SEPARATOR);
        
$target=strtr($file['target'],'/\\',DIRECTORY_SEPARATOR);
        
$callback=isset($file['callback']) ? $file['callback'] : null;
        
$params=isset($file['params']) ? $file['params'] : null;

        if(
is_dir($source))
        {
            
$this->ensureDirectory($target);
            continue;
        }

        if(
$callback!==null)
            
$content=call_user_func($callback,$source,$params);
        else
            
$content=file_get_contents($source);
        if(
is_file($target))
        {
            if(
$content===file_get_contents($target))
            {
                echo 
"  unchanged $name\n";
                continue;
            }
            if(
$overwriteAll)
                echo 
"  overwrite $name\n";
            else
            {
                echo 
"      exist $name\n";
                echo 
"            ...overwrite? [Yes|No|All|Quit] ";
                
$answer=trim(fgets(STDIN));
                if(!
strncasecmp($answer,'q',1))
                    return;
                else if(!
strncasecmp($answer,'y',1))
                    echo 
"  overwrite $name\n";
                else if(!
strncasecmp($answer,'a',1))
                {
                    echo 
"  overwrite $name\n";
                    
$overwriteAll=true;
                }
                else
                {
                    echo 
"       skip $name\n";
                    continue;
                }
            }
        }
        else
        {
            
$this->ensureDirectory(dirname($target));
            echo 
"   generate $name\n";
        }
        
file_put_contents($target,$content);
    }
}

灏嗘枃浠朵粠涓涓湴鏂瑰鍒跺埌鍙︿竴涓湴鏂广

鍙傝

ensureDirectory() 鏂规硶
public void ensureDirectory(string $directory)
$directory string 瑕佹鏌ョ殑鐩綍
婧愮爜锛 framework/console/CConsoleCommand.php#398 (鏄剧ず)
public function ensureDirectory($directory)
{
    if(!
is_dir($directory))
    {
        
$this->ensureDirectory(dirname($directory));
        echo 
"      mkdir ".strtr($directory,'\\','/')."\n";
        
mkdir($directory);
    }
}

鎵鏈夌殑鐖剁洰褰曚腑濡傛灉鍝釜涓嶅瓨鍦紝鍒欏垱寤哄畠銆

getCommandRunner() 鏂规硶
public CConsoleCommandRunner getCommandRunner()
{return} CConsoleCommandRunner 鍛戒护鎵ц瀵硅薄鐨勫疄渚
婧愮爜锛 framework/console/CConsoleCommand.php#215 (鏄剧ず)
public function getCommandRunner()
{
    return 
$this->_runner;
}

getHelp() 鏂规硶
public string getHelp()
{return} string 鍛戒护鎻忚堪銆傞粯璁ゆ槸鈥楿sage: php entry-script.php command-name鈥欍
婧愮爜锛 framework/console/CConsoleCommand.php#225 (鏄剧ず)
public function getHelp()
{
    
$help='Usage: '.$this->getCommandRunner()->getScriptName().' '.$this->getName();
    
$options=$this->getOptionHelp();
    if(empty(
$options))
        return 
$help;
    if(
count($options)===1)
        return 
$help.' '.$options[0];
    
$help.=" <action>\nActions:\n";
    foreach(
$options as $option)
        
$help.='    '.$option."\n";
    return 
$help;
}

鎻愪緵鍛戒护鎻忚堪銆 杩欎釜鏂规硶鍙閲嶅啓锛岀劧鍚庤繑鍥炲疄闄呯殑鍛戒护鎻忚堪銆

getName() 鏂规硶
public string getName()
{return} string 鍛戒护鍚嶅瓧銆
婧愮爜锛 framework/console/CConsoleCommand.php#207 (鏄剧ず)
public function getName()
{
    return 
$this->_name;
}

getOptionHelp() 鏂规硶 锛堝彲鐢ㄨ嚜 v1.1.5锛
public array getOptionHelp()
{return} array 鍛戒护甯姪淇℃伅閫夐」銆傛瘡涓涓暟缁勫厓绱犳弿杩颁簡 涓涓嫭绔嬬殑鍔ㄤ綔甯姪淇℃伅銆
婧愮爜锛 framework/console/CConsoleCommand.php#247 (鏄剧ず)
public function getOptionHelp()
{
    
$options=array();
    
$class=new ReflectionClass(get_class($this));
    foreach(
$class->getMethods(ReflectionMethod::IS_PUBLIC) as $method)
    {
        
$name=$method->getName();
        if(!
strncasecmp($name,'action',6) && strlen($name)>6)
        {
            
$name=substr($name,6);
            
$name[0]=strtolower($name[0]);
            
$help=$name;

            foreach(
$method->getParameters() as $param)
            {
                
$optional=$param->isDefaultValueAvailable();
                
$defaultValue=$optional $param->getDefaultValue() : null;
                
$name=$param->getName();
                if(
$optional)
                    
$help.=" [--$name=$defaultValue]";
                else
                    
$help.=" --$name=value";
            }
            
$options[]=$help;
        }
    }
    return 
$options;
}

鎻愪緵鍛戒护甯姪淇℃伅閫夐」銆 榛樿瀹炵幇鐨勬槸杩斿洖鎵鏈夊彲鐢ㄧ殑鍔ㄤ綔锛 鍚屾椂杩斿洖鐩稿叧鐨勯夐」淇℃伅銆

init() 鏂规硶 锛堝彲鐢ㄨ嚜 v1.1.6锛
public void init()
婧愮爜锛 framework/console/CConsoleCommand.php#76 (鏄剧ず)
public function init()
{
}

鍒濆鍖栧懡浠ゅ璞° 杩欎釜鏂规硶鏄湪鍛戒护瀵硅薄琚垱寤虹劧鍚庢牴鎹厤缃垵濮嬪寲涔嬪悗鎵ц鐨勩 浣犲彲浠ラ噸鍐欒繖涓柟娉曪紝浣垮畠鍦ㄥ懡浠ゆ墽琛屼箣鍓嶆潵鍋氭洿澶氱殑澶勭悊銆

pluralize() 鏂规硶
public string pluralize(string $name)
$name string 瑕佸彉鎴愬鏁扮殑鍗曡瘝
{return} string 宸茬粡鍙樻垚澶嶆暟鐨勫崟璇
婧愮爜锛 framework/console/CConsoleCommand.php#437 (鏄剧ず)
public function pluralize($name)
{
    
$rules=array(
        
'/move$/i' => 'moves',
        
'/foot$/i' => 'feet',
        
'/child$/i' => 'children',
        
'/human$/i' => 'humans',
        
'/man$/i' => 'men',
        
'/tooth$/i' => 'teeth',
        
'/person$/i' => 'people',
        
'/([m|l])ouse$/i' => '\1ice',
        
'/(x|ch|ss|sh|us|as|is|os)$/i' => '\1es',
        
'/([^aeiouy]|qu)y$/i' => '\1ies',
        
'/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
        
'/(shea|lea|loa|thie)f$/i' => '\1ves',
        
'/([ti])um$/i' => '\1a',
        
'/(tomat|potat|ech|her|vet)o$/i' => '\1oes',
        
'/(bu)s$/i' => '\1ses',
        
'/(ax|test)is$/i' => '\1es',
        
'/s$/' => 's',
    );
    foreach(
$rules as $rule=>$replacement)
    {
        if(
preg_match($rule,$name))
            return 
preg_replace($rule,$replacement,$name);
    }
    return 
$name.'s';
}

灏嗗崟璇嶈浆鎹负瀹冪殑澶嶆暟褰㈠紡銆

prompt() 鏂规硶 锛堝彲鐢ㄨ嚜 v1.1.9锛
public mixed prompt(string $message)
$message string 鍦ㄧ敤鎴疯緭鍏ヤ箣鍓嶈緭鍑虹殑鍐呭
{return} mixed 灏嗘暣琛屽唴瀹瑰綋浣滃瓧绗︿覆杩斿洖锛屽鏋滃凡缁忓仠姝㈣緭鍏ュ垯杩斿洖false銆
婧愮爜锛 framework/console/CConsoleCommand.php#474 (鏄剧ず)
public function prompt($message)
{
    if(
extension_loaded('readline'))
    {
        
$input readline($message.' ');
        
readline_add_history($input);
        return 
$input;
    }
    else
    {
        echo 
$message.' ';
        return 
trim(fgets(STDIN));
    }
}

濡傛灉readline杩欎釜PHP鎵╁睍鍙敤锛屽垯閫氳繃瀹冭鍙栬緭鍏ョ殑鍐呭锛屽惁鍒欓氳繃fgets()鏉ヨ鍙栥

renderFile() 鏂规硶
public mixed renderFile(string $_viewFile_, array $_data_=NULL, boolean $_return_=false)
$_viewFile_ string 瑙嗗浘鏂囦欢璺緞
$_data_ array 瑕佹彁鍙栫殑浣滀负瑙嗗浘鍙傛暟鐨勫彲閫夋暟鎹
$_return_ boolean 瑕佽繑鍥炴覆鏌撶粨鏋滆繕鏄洿鎺ユ樉绀哄畠
{return} mixed 濡傛灉闇瑕佺殑璇濓紝鍒欒繑鍥炴覆鏌撶粨鏋溿傚惁鍒欒繑鍥濶ull銆
婧愮爜锛 framework/console/CConsoleCommand.php#415 (鏄剧ず)
public function renderFile($_viewFile_,$_data_=null,$_return_=false)
{
    if(
is_array($_data_))
        
extract($_data_,EXTR_PREFIX_SAME,'data');
    else
        
$data=$_data_;
    if(
$_return_)
    {
        
ob_start();
        
ob_implicit_flush(false);
        require(
$_viewFile_);
        return 
ob_get_clean();
    }
    else
        require(
$_viewFile_);
}

娓叉煋涓涓鍥炬枃浠躲

resolveRequest() 鏂规硶 锛堝彲鐢ㄨ嚜 v1.1.5锛
protected array resolveRequest(array $args)
$args array 鍛戒护琛屽弬鏁
{return} array 鍔ㄤ綔鍚嶅瓧锛屽凡缁忓懡鍚嶇殑閫夐」锛堝悕瀛楋紳銆嬪硷級锛岃繕鏈夋病鏈夊懡鍚嶇殑閫夐」
婧愮爜锛 framework/console/CConsoleCommand.php#174 (鏄剧ず)
protected function resolveRequest($args)
{
    
$options=array();    // named parameters
    
$params=array();    // unnamed parameters
    
foreach($args as $arg)
    {
        if(
preg_match('/^--(\w+)(=(.*))?$/',$arg,$matches))  // an option
        
{
            
$name=$matches[1];
            
$value=isset($matches[3]) ? $matches[3] : true;
            if(isset(
$options[$name]))
            {
                if(!
is_array($options[$name]))
                    
$options[$name]=array($options[$name]);
                
$options[$name][]=$value;
            }
            else
                
$options[$name]=$value;
        }
        else if(isset(
$action))
            
$params[]=$arg;
        else
            
$action=$arg;
    }
    if(!isset(
$action))
        
$action=$this->defaultAction;

    return array(
$action,$options,$params);
}

瑙f瀽鍛戒护琛屽弬鏁扮劧鍚庡喅瀹氳杩愯鍝釜鍔ㄤ綔銆

run() 鏂规硶
public void run(array $args)
$args array 杩欎釜鍛戒护鐨勫懡浠よ鍙傛暟銆
婧愮爜锛 framework/console/CConsoleCommand.php#87 (鏄剧ず)
public function run($args)
{
    list(
$action$options$args)=$this->resolveRequest($args);
    
$methodName='action'.$action;
    if(!
preg_match('/^\w+$/',$action) || !method_exists($this,$methodName))
        
$this->usageError("Unknown action: ".$action);

    
$method=new ReflectionMethod($this,$methodName);
    
$params=array();
    
// named and unnamed options
    
foreach($method->getParameters() as $i=>$param)
    {
        
$name=$param->getName();
        if(isset(
$options[$name]))
        {
            if(
$param->isArray())
                
$params[]=is_array($options[$name]) ? $options[$name] : array($options[$name]);
            else if(!
is_array($options[$name]))
                
$params[]=$options[$name];
            else
                
$this->usageError("Option --$name requires a scalar. Array is given.");
        }
        else if(
$name==='args')
            
$params[]=$args;
        else if(
$param->isDefaultValueAvailable())
            
$params[]=$param->getDefaultValue();
        else
            
$this->usageError("Missing required option --$name.");
        unset(
$options[$name]);
    }

    
// try global options
    
if(!empty($options))
    {
        
$class=new ReflectionClass(get_class($this));
        foreach(
$options as $name=>$value)
        {
            if(
$class->hasProperty($name))
            {
                
$property=$class->getProperty($name);
                if(
$property->isPublic() && !$property->isStatic())
                {
                    
$this->$name=$value;
                    unset(
$options[$name]);
                }
            }
        }
    }

    if(!empty(
$options))
        
$this->usageError("Unknown options: ".implode(', ',array_keys($options)));

    if(
$this->beforeAction($action,$params))
    {
        
$method->invokeArgs($this,$params);
        
$this->afterAction($action,$params);
    }
}

鎵ц鍛戒护銆 榛樿瀹炵幇鐨勬槸瑙f瀽杈撳叆鐨勫弬鏁帮紝 鐒跺悗鏍规嵁鍛戒护璇锋眰鐩稿叧鐨勯夐」鍊 璋冨害瀵瑰簲鐨勫姩浣溿

usageError() 鏂规硶
public void usageError(string $message)
$message string 閿欒淇℃伅
婧愮爜锛 framework/console/CConsoleCommand.php#281 (鏄剧ず)
public function usageError($message)
{
    echo 
"Error: $message\n\n".$this->getHelp()."\n";
    exit(
1);
}

鏄剧ず涓涓繍琛岄敊璇 杩欎釜鏂规硶浼氱粓姝㈠綋鍓嶅簲鐢ㄧ殑鎵ц銆

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.