CConsoleCommandRunner

优质
小牛编辑
121浏览
2023-12-01
所有包 | 属性 | 方法
system.console
继承class CConsoleCommandRunner » CComponent
源自1.0
版本$Id: CConsoleCommandRunner.php 3426 2011-10-25 00:01:09Z alexander.makarow $
源码framework/console/CConsoleCommandRunner.php
CConsoleCommandRunner管理命令和执行请求的命令。

公共属性

隐藏继承属性

属性类型描述定义在
commandsarray所有可用的命令列表(命令名字=>命令配置)。 每一个命令配置可以是字符串或者是数组。 如果是前者,字符串应该是类名字或者是这个命令的 class path alias。 如果是后者,数组必须包含着‘class’元素, 这元素指定了命令的类名或者是YiiBase::getPathOfAlias类的路径。 其它的在数组的数组对用来初始化相关的 命令属性。例如,
array('email'=>array(   'class'=>'path.
CConsoleCommandRunner
scriptNamestring入口文件名字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)
$pathstring包含命令类文件的目录别名。
源码: framework/console/CConsoleCommandRunner.php#101 (显示) publicfunctionaddCommands($path)
{
if(($commands=$this->findCommands($path))!==array())
{
foreach($commandsas$name=>$file)
{
if(!isset($this->commands[$name]))
$this->commands[$name]=$file;
}
}
}

从命令路径添加命令。 如果命令已经存在,会忽略新的命令。

createCommand() 方法
public CConsoleCommand createCommand(string $name)
$namestring命令名字(大小写不敏感)
{return}CConsoleCommand返回命令对象。如果名字不可用则返回 null。
源码: framework/console/CConsoleCommandRunner.php#117 (显示) publicfunctioncreateCommand($name)
{
$name=strtolower($name);
if(isset($this->commands[$name]))
{
if(is_string($this->commands[$name]))//classfilepathoralias
{
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//analias
$className=Yii::import($this->commands[$name]);
returnnew$className($name,$this);
}
else//anarrayconfiguration
returnYii::createComponent($this->commands[$name],$name,$this);
}
elseif($name==='help')
returnnewCHelpCommand('help',$this);
else
returnnull;
}
findCommands() 方法
public array findCommands(string $path)
$pathstring包含命令类文件的目录。
{return}array命令列表(命令名字=》命令类文件)
源码: framework/console/CConsoleCommandRunner.php#81 (显示) publicfunctionfindCommands($path)
{
if(($dir=@opendir($path))===false)
returnarray();
$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 (显示) publicfunctiongetScriptName()
{
return$this->_scriptName;
}
run() 方法
public void run(array $args)
$argsarray用户提供参数列表(包含入口文件名字和命令名字)。
源码: framework/console/CConsoleCommandRunner.php#50 (显示) publicfunctionrun($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);
}

执行请求命令。