0 follower

CConsoleCommand

Package system.console
Inheritance abstract class CConsoleCommand » CComponent
Subclasses CHelpCommand
Since 1.0
Version $Id$
Source Code framework/console/CConsoleCommand.php
CConsoleCommand represents an executable user command.

The run method must be implemented with the actual command execution logic. You may override getHelp to provide more detailed description of the command.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
commandRunner CConsoleCommandRunner the command runner instance CConsoleCommand
help string Provides the command description. CConsoleCommand
name string the command name. CConsoleCommand

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. CComponent
__construct() Constructor. CConsoleCommand
__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
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
buildFileList() Builds the file list of a directory. CConsoleCommand
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
copyFiles() Copies a list of files from one place to another. CConsoleCommand
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
ensureDirectory() Creates all parent directories if they do not exist. CConsoleCommand
getCommandRunner() Returns the command runner instance CConsoleCommand
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getHelp() Provides the command description. CConsoleCommand
getName() Returns the command name. CConsoleCommand
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
pluralize() Converts a word to its plural form. CConsoleCommand
raiseEvent() Raises an event. CComponent
renderFile() Renders a view file. CConsoleCommand
run() Executes the command. CConsoleCommand
usageError() Displays a usage error. CConsoleCommand

Property Details

commandRunner property read-only

the command runner instance

help property read-only
public string getHelp()

Provides the command description. This method may be overridden to return the actual command description.

name property read-only
public string getName()

the command name.

Method Details

__construct() method
public void __construct(string $name, CConsoleCommandRunner $runner)
$name string name of the command
$runner CConsoleCommandRunner the command runner
Source Code: framework/console/CConsoleCommand.php#38 (show)
public function __construct($name,$runner)
{
    
$this->_name=$name;
    
$this->_runner=$runner;
}

Constructor.

buildFileList() method
public array buildFileList(string $sourceDir, string $targetDir, string $baseDir='')
$sourceDir string the source directory
$targetDir string the target directory
$baseDir string base directory
{return} array the file list (see copyFiles)
Source Code: framework/console/CConsoleCommand.php#191 (show)
public function buildFileList($sourceDir$targetDir$baseDir='')
{
    
$list=array();
    
$handle=opendir($sourceDir);
    while((
$file=readdir($handle))!==false)
    {
        if(
$file==='.' || $file==='..' || $file==='.svn')
            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;
}

Builds the file list of a directory. This method traverses through the specified directory and builds a list of files and subdirectories that the directory contains. The result of this function can be passed to copyFiles.

copyFiles() method
public void copyFiles(array $fileList)
$fileList array the list of files to be copied (name=>spec). The array keys are names displayed during the copy process, and array values are specifications for files to be copied. Each array value must be an array of the following structure:
  • source: required, the full path of the file/directory to be copied from
  • target: required, the full path of the file/directory to be copied to
  • callback: optional, the callback to be invoked when copying a file. The callback function should be declared as follows:
      function foo($source,$params)
      
    where $source parameter is the source file path, and the content returned by the function will be saved into the target file.
  • params: optional, the parameters to be passed to the callback
Source Code: framework/console/CConsoleCommand.php#99 (show)
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);
    }
}

Copies a list of files from one place to another.

See Also

ensureDirectory() method
public void ensureDirectory(string $directory)
$directory string the directory to be checked
Source Code: framework/console/CConsoleCommand.php#214 (show)
public function ensureDirectory($directory)
{
    if(!
is_dir($directory))
    {
        
$this->ensureDirectory(dirname($directory));
        echo 
"      mkdir ".strtr($directory,'\\','/')."\n";
        
mkdir($directory);
    }
}

Creates all parent directories if they do not exist.

getCommandRunner() method
public CConsoleCommandRunner getCommandRunner()
{return} CConsoleCommandRunner the command runner instance
Source Code: framework/console/CConsoleCommand.php#55 (show)
public function getCommandRunner()
{
    return 
$this->_runner;
}

getHelp() method
public string getHelp()
{return} string the command description. Defaults to 'Usage: php entry-script.php command-name'.
Source Code: framework/console/CConsoleCommand.php#65 (show)
public function getHelp()
{
    return 
'Usage: '.$this->getCommandRunner()->getScriptName().' '.$this->getName();
}

Provides the command description. This method may be overridden to return the actual command description.

getName() method
public string getName()
{return} string the command name.
Source Code: framework/console/CConsoleCommand.php#47 (show)
public function getName()
{
    return 
$this->_name;
}

pluralize() method
public string pluralize(string $name)
$name string the word to be pluralized
{return} string the pluralized word
Source Code: framework/console/CConsoleCommand.php#163 (show)
public function pluralize($name)
{
    
$rules=array(
        
'/(x|ch|ss|sh|us|as|is|os)$/i' => '\1es',
        
'/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
        
'/(m)an$/i' => '\1en',
        
'/(child)$/i' => '\1ren',
        
'/(r|t|b|d)y$/i' => '\1ies',
        
'/s$/' => 's',
    );
    foreach(
$rules as $rule=>$replacement)
    {
        if(
preg_match($rule,$name))
            return 
preg_replace($rule,$replacement,$name);
    }
    return 
$name.'s';
}

Converts a word to its plural form.

renderFile() method
public mixed renderFile(string $_viewFile_, array $_data_=NULL, boolean $_return_=false)
$_viewFile_ string view file path
$_data_ array optional data to be extracted as local view variables
$_return_ boolean whether to return the rendering result instead of displaying it
{return} mixed the rendering result if required. Null otherwise.
Source Code: framework/console/CConsoleCommand.php#231 (show)
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_);
}

Renders a view file.

run() method
abstract public void run(array $args)
$args array command line parameters for this command.
Source Code: framework/console/CConsoleCommand.php#31 (show)
public abstract function run($args);

Executes the command.

usageError() method
public void usageError(string $message)
$message string the error message
Source Code: framework/console/CConsoleCommand.php#75 (show)
public function usageError($message)
{
    die(
"Error: $message\n\n".$this->getHelp()."\n");
}

Displays a usage error. This method will then terminate the execution of the current application.