Skip to content
This repository
Newer
Older
100644 178 lines (168 sloc) 5.714 kb
55bdd955 » qiang.xue
2008-10-13 removed carriage returns from files.
1 <?php
2 /**
3 * CConsoleApplication class file.
4 *
5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link http://www.yiiframework.com/
4be0af69 » qiang.xue
2011-01-01 changed copyright year.
7 * @copyright Copyright &copy; 2008-2011 Yii Software LLC
55bdd955 » qiang.xue
2008-10-13 removed carriage returns from files.
8 * @license http://www.yiiframework.com/license/
9 */
10
11 /**
12 * CConsoleApplication represents a console application.
13 *
14 * CConsoleApplication extends {@link CApplication} by providing functionalities
15 * specific to console requests. In particular, it deals with console requests
16 * through a command-based approach:
17 * <ul>
18 * <li>A console application consists of one or several possible user commands;</li>
19 * <li>Each user command is implemented as a class extending {@link CConsoleCommand};</li>
20 * <li>User specifies which command to run on the command line;</li>
21 * <li>The command processes the user request with the specified parameters.</li>
22 * </ul>
23 *
24 * The command classes reside in the directory {@link getCommandPath commandPath}.
25 * The name of the class follows the pattern: &lt;command-name&gt;Command, and its
0f3ab344 » cebe
2012-04-27 fixed typos and whitespace
26 * file name is the same as the class name. For example, the 'ShellCommand' class defines
55bdd955 » qiang.xue
2008-10-13 removed carriage returns from files.
27 * a 'shell' command and the class file name is 'ShellCommand.php'.
28 *
29 * To run the console application, enter the following on the command line:
30 * <pre>
31 * php path/to/entry_script.php <command name> [param 1] [param 2] ...
32 * </pre>
33 *
34 * You may use the following to see help instructions about a command:
35 * <pre>
36 * php path/to/entry_script.php help <command name>
37 * </pre>
38 *
646cbb2b » alexander.makarow
2011-10-25 Documented component accessors with @property for significantly bette…
39 * @property string $commandPath The directory that contains the command classes. Defaults to 'protected/commands'.
40 * @property CConsoleCommandRunner $commandRunner The command runner.
41 *
55bdd955 » qiang.xue
2008-10-13 removed carriage returns from files.
42 * @author Qiang Xue <qiang.xue@gmail.com>
43 * @package system.console
44 * @since 1.0
45 */
46 class CConsoleApplication extends CApplication
47 {
48 /**
8a610c51 » qiang.xue
2009-08-29 merge from 1.0
49 * @var array mapping from command name to command configurations.
55bdd955 » qiang.xue
2008-10-13 removed carriage returns from files.
50 * Each command configuration can be either a string or an array.
51 * If the former, the string should be the file path of the command class.
52 * If the latter, the array must contain a 'class' element which specifies
53 * the command's class name or {@link YiiBase::getPathOfAlias class path alias}.
54 * The rest name-value pairs in the array are used to initialize
55 * the corresponding command properties. For example,
56 * <pre>
57 * array(
58 * 'email'=>array(
59 * 'class'=>'path.to.Mailer',
60 * 'interval'=>3600,
61 * ),
62 * 'log'=>'path/to/LoggerCommand.php',
63 * )
64 * </pre>
65 */
66 public $commandMap=array();
67
68 private $_commandPath;
69 private $_runner;
70
71 /**
72 * Initializes the application by creating the command runner.
73 */
74 protected function init()
75 {
76 parent::init();
69949a36 » qiang.xue
2008-11-26
77 if(!isset($_SERVER['argv'])) // || strncasecmp(php_sapi_name(),'cli',3))
55bdd955 » qiang.xue
2008-10-13 removed carriage returns from files.
78 die('This script must be run from the command line.');
79 $this->_runner=$this->createCommandRunner();
80 $this->_runner->commands=$this->commandMap;
81 $this->_runner->addCommands($this->getCommandPath());
82 }
83
84 /**
85 * Processes the user request.
a6d8222d » cebe
2012-04-27 for consistency: alwas return integer exit code
86 * This method uses a console command runner to handle the particular user command.
86b75f31 » cebe
2012-04-27 Allowed returning exit code in console actions
87 * Since version 1.1.11 this method will exit application with an exit code if one is returned by the user command.
55bdd955 » qiang.xue
2008-10-13 removed carriage returns from files.
88 */
89 public function processRequest()
90 {
86b75f31 » cebe
2012-04-27 Allowed returning exit code in console actions
91 $exitCode=$this->_runner->run($_SERVER['argv']);
92 if(is_int($exitCode))
2d1aa75b » cebe
2012-04-27 Yii::app()->end() is better than exit()
93 $this->end($exitCode);
55bdd955 » qiang.xue
2008-10-13 removed carriage returns from files.
94 }
95
96 /**
97 * Creates the command runner instance.
98 * @return CConsoleCommandRunner the command runner
99 */
100 protected function createCommandRunner()
101 {
102 return new CConsoleCommandRunner;
103 }
104
105 /**
106 * Displays the captured PHP error.
107 * This method displays the error in console mode when there is
108 * no active error handler.
72b97456 » mdomba
2010-09-23 Updated PHPDoc @param parameter names
109 * @param integer $code error code
110 * @param string $message error message
111 * @param string $file error file
112 * @param string $line error line
55bdd955 » qiang.xue
2008-10-13 removed carriage returns from files.
113 */
114 public function displayError($code,$message,$file,$line)
115 {
116 echo "PHP Error[$code]: $message\n";
bbfd0eef » qiang.xue
2010-11-29 Improved the error display in console command mode
117 echo " in file $file at line $line\n";
118 $trace=debug_backtrace();
119 // skip the first 4 stacks as they do not tell the error position
120 if(count($trace)>4)
121 $trace=array_slice($trace,4);
122 foreach($trace as $i=>$t)
123 {
124 if(!isset($t['file']))
125 $t['file']='unknown';
126 if(!isset($t['line']))
127 $t['line']=0;
128 if(!isset($t['function']))
129 $t['function']='unknown';
130 echo "#$i {$t['file']}({$t['line']}): ";
131 if(isset($t['object']) && is_object($t['object']))
132 echo get_class($t['object']).'->';
133 echo "{$t['function']}()\n";
134 }
55bdd955 » qiang.xue
2008-10-13 removed carriage returns from files.
135 }
136
137 /**
138 * Displays the uncaught PHP exception.
139 * This method displays the exception in console mode when there is
140 * no active error handler.
72b97456 » mdomba
2010-09-23 Updated PHPDoc @param parameter names
141 * @param Exception $exception the uncaught exception
55bdd955 » qiang.xue
2008-10-13 removed carriage returns from files.
142 */
143 public function displayException($exception)
144 {
145 echo $exception;
146 }
147
148 /**
149 * @return string the directory that contains the command classes. Defaults to 'protected/commands'.
150 */
151 public function getCommandPath()
152 {
589fb58f » alexander.makarow
2011-05-12 (Fixes issue 2382) Fixed the bug that yiic wasn't able to run if ther…
153 $applicationCommandPath = $this->getBasePath().DIRECTORY_SEPARATOR.'commands';
154 if($this->_commandPath===null && file_exists($applicationCommandPath))
155 $this->setCommandPath($applicationCommandPath);
55bdd955 » qiang.xue
2008-10-13 removed carriage returns from files.
156 return $this->_commandPath;
157 }
158
159 /**
72b97456 » mdomba
2010-09-23 Updated PHPDoc @param parameter names
160 * @param string $value the directory that contains the command classes.
55bdd955 » qiang.xue
2008-10-13 removed carriage returns from files.
161 * @throws CException if the directory is invalid
162 */
163 public function setCommandPath($value)
164 {
165 if(($this->_commandPath=realpath($value))===false || !is_dir($this->_commandPath))
cf9b8dec » qiang.xue
2008-10-24 Changed Yii::t() and added a category parameter as its first parameter
166 throw new CException(Yii::t('yii','The command path "{path}" is not a valid directory.',
55bdd955 » qiang.xue
2008-10-13 removed carriage returns from files.
167 array('{path}'=>$value)));
168 }
169
170 /**
171 * Returns the command runner.
172 * @return CConsoleCommandRunner the command runner.
173 */
174 public function getCommandRunner()
175 {
176 return $this->_runner;
177 }
178 }
Something went wrong with that request. Please try again.