> YII 类库手册 > CConsoleCommandRunner
system.console
继承 class CConsoleCommandRunner » CComponent
源自 1.0
版本 $Id: CConsoleCommandRunner.PHP 3426 2011-10-25 00:01:09Z alexander.makarow $
源码
CConsoleCommandRunner管理命令和执行请求的命令。

公共属性

属性 类型 描述 定义在
commands array 所有可用的命令列表(命令名字=>命令配置)。 每一个命令配置可以是字符串或者是数组。 如果是前者,字符串应该是类名字或者是这个命令的 class path alias。 如果是后者,数组必须包含着‘class’元素, 这元素指定了命令的类名或者是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() 计算一个PHP表达式,或根据组件上下文执行回调。 CComponent
findCommands() 在指定的目录下面搜索命令。 CConsoleCommandRunner
getEventHandlers() 返回一个事件的附加处理程序列表。 CComponent
getScriptName() 返回入口文件名字 CConsoleCommandRunner
hasEvent() 确定一个事件是否定义。 CComponent
hasEventHandler() 检查事件是否有附加的处理程序。 CComponent
hasProperty() 确定属性是否被定义。 CComponent
raiseEvent() 发起一个事件。 CComponent
run() 执行请求命令。 CConsoleCommandRunner

属性详细

commands 属性
public array $commands;

所有可用的命令列表(命令名字=>命令配置)。 每一个命令配置可以是字符串或者是数组。 如果是前者,字符串应该是类名字或者是这个命令的 class path alias。 如果是后者,数组必须包含着‘class’元素, 这元素指定了命令的类名或者是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 包含命令类文件的目录别名。
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。
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 命令列表(命令名字=》命令类文件)
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 入口文件名字
public function getScriptName()
{
    return 
$this->_scriptName;
}

run() 方法
public void run(array $args)
$args array 用户提供参数列表(包含入口文件名字和命令名字)。
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);
}

执行请求命令。

上一篇:
下一篇: