CConsoleCommandRunner
包 | system.console |
---|---|
继承 | class CConsoleCommandRunner » CComponent |
可用自 | 1.0 |
源码 | framework/console/CConsoleCommandRunner.php |
CConsoleCommandRunner manages commands and executes the requested command.
公共属性
属性 | 类型 | 描述 | 被定义在 |
---|---|---|---|
command | CConsoleCommand|null | Returns the currently running command. | CConsoleCommandRunner |
commands | array | list of all available commands (command name=>command configuration). | CConsoleCommandRunner |
scriptName | string | the entry script name | CConsoleCommandRunner |
公共方法
方法 | 描述 | 被定义在 |
---|---|---|
__call() | Calls the named method which is not a class method. | CComponent |
__get() | Returns a property value, an event handler list or a behavior based on its name. | CComponent |
__isset() | Checks if a property value is null. | CComponent |
__set() | Sets value of a component property. | CComponent |
__unset() | Sets a component property to be null. | CComponent |
addCommands() | Adds commands from the specified command path. | CConsoleCommandRunner |
asa() | Returns the named behavior object. | CComponent |
attachBehavior() | Attaches a behavior to this component. | CComponent |
attachBehaviors() | Attaches a list of behaviors to the component. | CComponent |
attachEventHandler() | Attaches an event handler to an event. | CComponent |
canGetProperty() | Determines whether a property can be read. | CComponent |
canSetProperty() | Determines whether a property can be set. | CComponent |
createCommand() | CConsoleCommandRunner | |
detachBehavior() | Detaches a behavior from the component. | CComponent |
detachBehaviors() | Detaches all behaviors from the component. | CComponent |
detachEventHandler() | Detaches an existing event handler. | CComponent |
disableBehavior() | Disables an attached behavior. | CComponent |
disableBehaviors() | Disables all behaviors attached to this component. | CComponent |
enableBehavior() | Enables an attached behavior. | CComponent |
enableBehaviors() | Enables all behaviors attached to this component. | CComponent |
evaluateExpression() | Evaluates a PHP expression or callback under the context of this component. | CComponent |
findCommands() | Searches for commands under the specified directory. | CConsoleCommandRunner |
getCommand() | Returns the currently running command. | CConsoleCommandRunner |
getEventHandlers() | Returns the list of attached event handlers for an event. | CComponent |
getScriptName() | Returns the entry script name | CConsoleCommandRunner |
hasEvent() | Determines whether an event is defined. | CComponent |
hasEventHandler() | Checks whether the named event has attached handlers. | CComponent |
hasProperty() | Determines whether a property is defined. | CComponent |
raiseEvent() | Raises an event. | CComponent |
run() | Executes the requested command. | CConsoleCommandRunner |
setCommand() | Sets the currently active command. | CConsoleCommandRunner |
属性详情
command
属性
(自版本 v1.1.14 可用)
Returns the currently running command.
commands
属性
public array $commands;
list of all available commands (command name=>command configuration). Each command configuration can be either a string or an array. If the former, the string should be the class name or class path alias of the command. If the latter, the array must contain a 'class' element which specifies the command's class name or class path alias. The rest name-value pairs in the array are used to initialize the corresponding command properties. For example,
array( 'email'=>array( 'class'=>'path.to.Mailer', 'interval'=>3600, ), 'log'=>'path.to.LoggerCommand', )
scriptName
属性
只读
public string getScriptName()
the entry script name
方法详情
addCommands()
方法
public void addCommands(string $path)
| ||
$path | string | the alias of the directory containing the command class files. |
源码: framework/console/CConsoleCommandRunner.php#128 (显示)
public function addCommands($path)
{
if(($commands=$this->findCommands($path))!==array())
{
foreach($commands as $name=>$file)
{
if(!isset($this->commands[$name]))
$this->commands[$name]=$file;
}
}
}
Adds commands from the specified command path. If a command already exists, the new one will be ignored.
createCommand()
方法
public CConsoleCommand createCommand(string $name)
| ||
$name | string | command name (case-insensitive) |
{return} | CConsoleCommand | the command object. Null if the name is invalid. |
源码: framework/console/CConsoleCommandRunner.php#144 (显示)
public function createCommand($name)
{
$name=strtolower($name);
$command=null;
if(isset($this->commands[$name]))
$command=$this->commands[$name];
else
{
$commands=array_change_key_case($this->commands);
if(isset($commands[$name]))
$command=$commands[$name];
}
if($command!==null)
{
if(is_string($command)) // class file path or alias
{
if(strpos($command,'/')!==false || strpos($command,'\\')!==false)
{
$className=substr(basename($command),0,-4);
if(!class_exists($className,false))
require_once($command);
}
else // an alias
$className=Yii::import($command);
return new $className($name,$this);
}
else // an array configuration
return Yii::createComponent($command,$name,$this);
}
elseif($name==='help')
return new CHelpCommand('help',$this);
else
return null;
}
findCommands()
方法
public array findCommands(string $path)
| ||
$path | string | the directory containing the command class files. |
{return} | array | list of commands (command name=>command class file) |
源码: framework/console/CConsoleCommandRunner.php#108 (显示)
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;
}
Searches for commands under the specified directory.
getCommand()
方法
(自版本 v1.1.14 可用)
public CConsoleCommand|null getCommand()
| ||
{return} | CConsoleCommand|null | the currently active command. |
源码: framework/console/CConsoleCommandRunner.php#89 (显示)
public function getCommand()
{
return $this->_command;
}
Returns the currently running command.
getScriptName()
方法
public string getScriptName()
| ||
{return} | string | the entry script name |
源码: framework/console/CConsoleCommandRunner.php#79 (显示)
public function getScriptName()
{
return $this->_scriptName;
}
run()
方法
public integer|null run(array $args)
| ||
$args | array | list of user supplied parameters (including the entry script name and the command name). |
{return} | integer|null | application exit code returned by the command. if null is returned, application will not exit explicitly. See also CConsoleApplication::processRequest(). (return value is available since version 1.1.11) |
源码: framework/console/CConsoleCommandRunner.php#54 (显示)
public function run($args)
{
$this->_scriptName=$args[0];
array_shift($args);
if(isset($args[0]))
{
$name=$args[0];
array_shift($args);
}
else
$name='help';
$oldCommand=$this->_command;
if(($command=$this->createCommand($name))===null)
$command=$this->createCommand('help');
$this->_command=$command;
$command->init();
$exitCode=$command->run($args);
$this->_command=$oldCommand;
return $exitCode;
}
Executes the requested command.
setCommand()
方法
(自版本 v1.1.14 可用)
public void setCommand(CConsoleCommand $value)
| ||
$value | CConsoleCommand | the currently active command. |
源码: framework/console/CConsoleCommandRunner.php#98 (显示)
public function setCommand($value)
{
$this->_command=$value;
}