Yii Framework v1.1.10 绫诲弬鑰

CConsoleCommandRunner

system.console
缁ф壙 class CConsoleCommandRunner » CComponent
婧愯嚜 1.0
鐗堟湰 $Id: CConsoleCommandRunner.php 3426 2011-10-25 00:01:09Z alexander.makarow $
婧愮爜 framework/console/CConsoleCommandRunner.php
CConsoleCommandRunner绠$悊鍛戒护鍜屾墽琛岃姹傜殑鍛戒护銆

鍏叡灞炴

闅愯棌缁ф壙灞炴

灞炴绫诲瀷鎻忚堪瀹氫箟鍦
commands array 鎵鏈夊彲鐢ㄧ殑鍛戒护鍒楄〃锛堝懡浠ゅ悕瀛=>鍛戒护閰嶇疆锛夈 姣忎竴涓懡浠ら厤缃彲浠ユ槸瀛楃涓叉垨鑰呮槸鏁扮粍銆 濡傛灉鏄墠鑰咃紝瀛楃涓插簲璇ユ槸绫诲悕瀛楁垨鑰呮槸杩欎釜鍛戒护鐨 class path alias銆 濡傛灉鏄悗鑰咃紝鏁扮粍蹇呴』鍖呭惈鐫鈥榗lass鈥欏厓绱狅紝 杩欏厓绱犳寚瀹氫簡鍛戒护鐨勭被鍚嶆垨鑰呮槸YiiBase::getPathOfAlias绫荤殑璺緞銆 鍏跺畠鐨勫湪鏁扮粍鐨勬暟缁勫鐢ㄦ潵鍒濆鍖栫浉鍏崇殑 鍛戒护灞炴с備緥濡傦紝
array(
  'email'=>array(
     'class'=>'path.
CConsoleCommandRunner
scriptName string 鍏ュ彛鏂囦欢鍚嶅瓧 CConsoleCommandRunner

鍏叡鏂规硶

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
__call() 濡傛灉绫讳腑娌℃湁璋冪殑鏂规硶鍚嶏紝鍒欒皟鐢ㄨ繖涓柟娉曘 CComponent
__get() 杩斿洖涓涓睘鎬у笺佷竴涓簨浠跺鐞嗙▼搴忓垪琛ㄦ垨涓涓涓哄悕绉般 CComponent
__isset() 妫鏌ヤ竴涓睘鎬ф槸鍚︿负null銆 CComponent
__set() 璁剧疆涓涓粍浠剁殑灞炴у笺 CComponent
__unset() 璁剧疆涓涓粍浠剁殑灞炴т负null銆 CComponent
addCommands() 浠庡懡浠よ矾寰勬坊鍔犲懡浠ゃ CConsoleCommandRunner
asa() 杩斿洖杩欎釜鍚嶅瓧鐨勮涓哄璞° CComponent
attachBehavior() 闄勫姞涓涓涓哄埌缁勪欢銆 CComponent
attachBehaviors() 闄勫姞涓涓涓哄垪琛ㄥ埌缁勪欢銆 CComponent
attachEventHandler() 涓轰簨浠堕檮鍔犱竴涓簨浠跺鐞嗙▼搴忋 CComponent
canGetProperty() 纭畾灞炴ф槸鍚﹀彲璇汇 CComponent
canSetProperty() 纭畾灞炴ф槸鍚﹀彲鍐欍 CComponent
createCommand() CConsoleCommandRunner
detachBehavior() 浠庣粍浠朵腑鍒嗙涓涓涓恒 CComponent
detachBehaviors() 浠庣粍浠朵腑鍒嗙鎵鏈夎涓恒 CComponent
detachEventHandler() 鍒嗙涓涓瓨鍦ㄧ殑浜嬩欢澶勭悊绋嬪簭銆 CComponent
disableBehavior() 绂佺敤涓涓檮鍔犺涓恒 CComponent
disableBehaviors() 绂佺敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
enableBehavior() 鍚敤涓涓檮鍔犺涓恒 CComponent
enableBehaviors() 鍚敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
evaluateExpression() 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 CComponent
findCommands() 鍦ㄦ寚瀹氱殑鐩綍涓嬮潰鎼滅储鍛戒护銆 CConsoleCommandRunner
getEventHandlers() 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 CComponent
getScriptName() 杩斿洖鍏ュ彛鏂囦欢鍚嶅瓧 CConsoleCommandRunner
hasEvent() 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 CComponent
hasEventHandler() 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 CComponent
hasProperty() 纭畾灞炴ф槸鍚﹁瀹氫箟銆 CComponent
raiseEvent() 鍙戣捣涓涓簨浠躲 CComponent
run() 鎵ц璇锋眰鍛戒护銆 CConsoleCommandRunner

灞炴ц缁

commands 灞炴
public array $commands;

鎵鏈夊彲鐢ㄧ殑鍛戒护鍒楄〃锛堝懡浠ゅ悕瀛=>鍛戒护閰嶇疆锛夈 姣忎竴涓懡浠ら厤缃彲浠ユ槸瀛楃涓叉垨鑰呮槸鏁扮粍銆 濡傛灉鏄墠鑰咃紝瀛楃涓插簲璇ユ槸绫诲悕瀛楁垨鑰呮槸杩欎釜鍛戒护鐨 class path alias銆 濡傛灉鏄悗鑰咃紝鏁扮粍蹇呴』鍖呭惈鐫鈥榗lass鈥欏厓绱狅紝 杩欏厓绱犳寚瀹氫簡鍛戒护鐨勭被鍚嶆垨鑰呮槸YiiBase::getPathOfAlias绫荤殑璺緞銆 鍏跺畠鐨勫湪鏁扮粍鐨勬暟缁勫鐢ㄦ潵鍒濆鍖栫浉鍏崇殑 鍛戒护灞炴с備緥濡傦紝

array(
  'email'=>array(
     'class'=>'path.to.Mailer',
     'interval'=>3600,
  ),
  'log'=>'path.to.LoggerCommand',
)

scriptName 灞炴 鍙
public string getScriptName()

鍏ュ彛鏂囦欢鍚嶅瓧

鏂规硶璇︾粏

addCommands() 鏂规硶
public void addCommands(string $path)
$path string 鍖呭惈鍛戒护绫绘枃浠剁殑鐩綍鍒悕銆
婧愮爜锛 framework/console/CConsoleCommandRunner.php#101 (鏄剧ず)
public function addCommands($path)
{
    if((
$commands=$this->findCommands($path))!==array())
    {
        foreach(
$commands as $name=>$file)
        {
            if(!isset(
$this->commands[$name]))
                
$this->commands[$name]=$file;
        }
    }
}

浠庡懡浠よ矾寰勬坊鍔犲懡浠ゃ 濡傛灉鍛戒护宸茬粡瀛樺湪锛屼細蹇界暐鏂扮殑鍛戒护銆

createCommand() 鏂规硶
public CConsoleCommand createCommand(string $name)
$name string 鍛戒护鍚嶅瓧锛堝ぇ灏忓啓涓嶆晱鎰燂級
{return} CConsoleCommand 杩斿洖鍛戒护瀵硅薄銆傚鏋滃悕瀛椾笉鍙敤鍒欒繑鍥 null銆
婧愮爜锛 framework/console/CConsoleCommandRunner.php#117 (鏄剧ず)
public function createCommand($name)
{
    
$name=strtolower($name);
    if(isset(
$this->commands[$name]))
    {
        if(
is_string($this->commands[$name]))  // class file path or alias
        
{
            if(
strpos($this->commands[$name],'/')!==false || strpos($this->commands[$name],'\\')!==false)
            {
                
$className=substr(basename($this->commands[$name]),0,-4);
                if(!
class_exists($className,false))
                    require_once(
$this->commands[$name]);
            }
            else 
// an alias
                
$className=Yii::import($this->commands[$name]);
            return new 
$className($name,$this);
        }
        else 
// an array configuration
            
return Yii::createComponent($this->commands[$name],$name,$this);
    }
    else if(
$name==='help')
        return new 
CHelpCommand('help',$this);
    else
        return 
null;
}

findCommands() 鏂规硶
public array findCommands(string $path)
$path string 鍖呭惈鍛戒护绫绘枃浠剁殑鐩綍銆
{return} array 鍛戒护鍒楄〃锛堝懡浠ゅ悕瀛楋紳銆嬪懡浠ょ被鏂囦欢锛
婧愮爜锛 framework/console/CConsoleCommandRunner.php#81 (鏄剧ず)
public function findCommands($path)
{
    if((
$dir=@opendir($path))===false)
        return array();
    
$commands=array();
    while((
$name=readdir($dir))!==false)
    {
        
$file=$path.DIRECTORY_SEPARATOR.$name;
        if(!
strcasecmp(substr($name,-11),'Command.php') && is_file($file))
            
$commands[strtolower(substr($name,0,-11))]=$file;
    }
    
closedir($dir);
    return 
$commands;
}

鍦ㄦ寚瀹氱殑鐩綍涓嬮潰鎼滅储鍛戒护銆

getScriptName() 鏂规硶
public string getScriptName()
{return} string 鍏ュ彛鏂囦欢鍚嶅瓧
婧愮爜锛 framework/console/CConsoleCommandRunner.php#71 (鏄剧ず)
public function getScriptName()
{
    return 
$this->_scriptName;
}

run() 鏂规硶
public void run(array $args)
$args array 鐢ㄦ埛鎻愪緵鍙傛暟鍒楄〃锛堝寘鍚叆鍙f枃浠跺悕瀛楀拰鍛戒护鍚嶅瓧锛夈
婧愮爜锛 framework/console/CConsoleCommandRunner.php#50 (鏄剧ず)
public function run($args)
{
    
$this->_scriptName=$args[0];
    
array_shift($args);
    if(isset(
$args[0]))
    {
        
$name=$args[0];
        
array_shift($args);
    }
    else
        
$name='help';

    if((
$command=$this->createCommand($name))===null)
        
$command=$this->createCommand('help');
    
$command->init();
    
$command->run($args);
}

鎵ц璇锋眰鍛戒护銆

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