PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/demo/yii/console/CConsoleCommand.php

https://bitbucket.org/negge/yii-bootstrap-new
PHP | 502 lines | 324 code | 25 blank | 153 comment | 46 complexity | b3ec58e42229ed3acc2ae4eaca4144c6 MD5 | raw file
Possible License(s): BSD-2-Clause, Apache-2.0, BSD-3-Clause, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * CConsoleCommand class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright &copy; 2008-2011 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CConsoleCommand represents an executable console command.
  12. *
  13. * It works like {@link CController} by parsing command line options and dispatching
  14. * the request to a specific action with appropriate option values.
  15. *
  16. * Users call a console command via the following command format:
  17. * <pre>
  18. * yiic CommandName ActionName --Option1=Value1 --Option2=Value2 ...
  19. * </pre>
  20. *
  21. * Child classes mainly needs to implement various action methods whose name must be
  22. * prefixed with "action". The parameters to an action method are considered as options
  23. * for that specific action. The action specified as {@link defaultAction} will be invoked
  24. * when a user does not specify the action name in his command.
  25. *
  26. * Options are bound to action parameters via parameter names. For example, the following
  27. * action method will allow us to run a command with <code>yiic sitemap --type=News</code>:
  28. * <pre>
  29. * class SitemapCommand {
  30. * public function actionIndex($type) {
  31. * ....
  32. * }
  33. * }
  34. * </pre>
  35. *
  36. * @property string $name The command name.
  37. * @property CConsoleCommandRunner $commandRunner The command runner instance.
  38. * @property string $help The command description. Defaults to 'Usage: php entry-script.php command-name'.
  39. * @property array $optionHelp The command option help information. Each array element describes
  40. * the help information for a single action.
  41. *
  42. * @author Qiang Xue <qiang.xue@gmail.com>
  43. * @version $Id: CConsoleCommand.php 3548 2012-01-24 11:42:59Z mdomba $
  44. * @package system.console
  45. * @since 1.0
  46. */
  47. abstract class CConsoleCommand extends CComponent
  48. {
  49. /**
  50. * @var string the name of the default action. Defaults to 'index'.
  51. * @since 1.1.5
  52. */
  53. public $defaultAction='index';
  54. private $_name;
  55. private $_runner;
  56. /**
  57. * Constructor.
  58. * @param string $name name of the command
  59. * @param CConsoleCommandRunner $runner the command runner
  60. */
  61. public function __construct($name,$runner)
  62. {
  63. $this->_name=$name;
  64. $this->_runner=$runner;
  65. }
  66. /**
  67. * Initializes the command object.
  68. * This method is invoked after a command object is created and initialized with configurations.
  69. * You may override this method to further customize the command before it executes.
  70. * @since 1.1.6
  71. */
  72. public function init()
  73. {
  74. }
  75. /**
  76. * Executes the command.
  77. * The default implementation will parse the input parameters and
  78. * dispatch the command request to an appropriate action with the corresponding
  79. * option values
  80. * @param array $args command line parameters for this command.
  81. */
  82. public function run($args)
  83. {
  84. list($action, $options, $args)=$this->resolveRequest($args);
  85. $methodName='action'.$action;
  86. if(!preg_match('/^\w+$/',$action) || !method_exists($this,$methodName))
  87. $this->usageError("Unknown action: ".$action);
  88. $method=new ReflectionMethod($this,$methodName);
  89. $params=array();
  90. // named and unnamed options
  91. foreach($method->getParameters() as $i=>$param)
  92. {
  93. $name=$param->getName();
  94. if(isset($options[$name]))
  95. {
  96. if($param->isArray())
  97. $params[]=is_array($options[$name]) ? $options[$name] : array($options[$name]);
  98. else if(!is_array($options[$name]))
  99. $params[]=$options[$name];
  100. else
  101. $this->usageError("Option --$name requires a scalar. Array is given.");
  102. }
  103. else if($name==='args')
  104. $params[]=$args;
  105. else if($param->isDefaultValueAvailable())
  106. $params[]=$param->getDefaultValue();
  107. else
  108. $this->usageError("Missing required option --$name.");
  109. unset($options[$name]);
  110. }
  111. // try global options
  112. if(!empty($options))
  113. {
  114. $class=new ReflectionClass(get_class($this));
  115. foreach($options as $name=>$value)
  116. {
  117. if($class->hasProperty($name))
  118. {
  119. $property=$class->getProperty($name);
  120. if($property->isPublic() && !$property->isStatic())
  121. {
  122. $this->$name=$value;
  123. unset($options[$name]);
  124. }
  125. }
  126. }
  127. }
  128. if(!empty($options))
  129. $this->usageError("Unknown options: ".implode(', ',array_keys($options)));
  130. if($this->beforeAction($action,$params))
  131. {
  132. $method->invokeArgs($this,$params);
  133. $this->afterAction($action,$params);
  134. }
  135. }
  136. /**
  137. * This method is invoked right before an action is to be executed.
  138. * You may override this method to do last-minute preparation for the action.
  139. * @param string $action the action name
  140. * @param array $params the parameters to be passed to the action method.
  141. * @return boolean whether the action should be executed.
  142. */
  143. protected function beforeAction($action,$params)
  144. {
  145. return true;
  146. }
  147. /**
  148. * This method is invoked right after an action finishes execution.
  149. * You may override this method to do some postprocessing for the action.
  150. * @param string $action the action name
  151. * @param array $params the parameters to be passed to the action method.
  152. */
  153. protected function afterAction($action,$params)
  154. {
  155. }
  156. /**
  157. * Parses the command line arguments and determines which action to perform.
  158. * @param array $args command line arguments
  159. * @return array the action name, named options (name=>value), and unnamed options
  160. * @since 1.1.5
  161. */
  162. protected function resolveRequest($args)
  163. {
  164. $options=array(); // named parameters
  165. $params=array(); // unnamed parameters
  166. foreach($args as $arg)
  167. {
  168. if(preg_match('/^--(\w+)(=(.*))?$/',$arg,$matches)) // an option
  169. {
  170. $name=$matches[1];
  171. $value=isset($matches[3]) ? $matches[3] : true;
  172. if(isset($options[$name]))
  173. {
  174. if(!is_array($options[$name]))
  175. $options[$name]=array($options[$name]);
  176. $options[$name][]=$value;
  177. }
  178. else
  179. $options[$name]=$value;
  180. }
  181. else if(isset($action))
  182. $params[]=$arg;
  183. else
  184. $action=$arg;
  185. }
  186. if(!isset($action))
  187. $action=$this->defaultAction;
  188. return array($action,$options,$params);
  189. }
  190. /**
  191. * @return string the command name.
  192. */
  193. public function getName()
  194. {
  195. return $this->_name;
  196. }
  197. /**
  198. * @return CConsoleCommandRunner the command runner instance
  199. */
  200. public function getCommandRunner()
  201. {
  202. return $this->_runner;
  203. }
  204. /**
  205. * Provides the command description.
  206. * This method may be overridden to return the actual command description.
  207. * @return string the command description. Defaults to 'Usage: php entry-script.php command-name'.
  208. */
  209. public function getHelp()
  210. {
  211. $help='Usage: '.$this->getCommandRunner()->getScriptName().' '.$this->getName();
  212. $options=$this->getOptionHelp();
  213. if(empty($options))
  214. return $help;
  215. if(count($options)===1)
  216. return $help.' '.$options[0];
  217. $help.=" <action>\nActions:\n";
  218. foreach($options as $option)
  219. $help.=' '.$option."\n";
  220. return $help;
  221. }
  222. /**
  223. * Provides the command option help information.
  224. * The default implementation will return all available actions together with their
  225. * corresponding option information.
  226. * @return array the command option help information. Each array element describes
  227. * the help information for a single action.
  228. * @since 1.1.5
  229. */
  230. public function getOptionHelp()
  231. {
  232. $options=array();
  233. $class=new ReflectionClass(get_class($this));
  234. foreach($class->getMethods(ReflectionMethod::IS_PUBLIC) as $method)
  235. {
  236. $name=$method->getName();
  237. if(!strncasecmp($name,'action',6) && strlen($name)>6)
  238. {
  239. $name=substr($name,6);
  240. $name[0]=strtolower($name[0]);
  241. $help=$name;
  242. foreach($method->getParameters() as $param)
  243. {
  244. $optional=$param->isDefaultValueAvailable();
  245. $defaultValue=$optional ? $param->getDefaultValue() : null;
  246. $name=$param->getName();
  247. if($optional)
  248. $help.=" [--$name=$defaultValue]";
  249. else
  250. $help.=" --$name=value";
  251. }
  252. $options[]=$help;
  253. }
  254. }
  255. return $options;
  256. }
  257. /**
  258. * Displays a usage error.
  259. * This method will then terminate the execution of the current application.
  260. * @param string $message the error message
  261. */
  262. public function usageError($message)
  263. {
  264. echo "Error: $message\n\n".$this->getHelp()."\n";
  265. exit(1);
  266. }
  267. /**
  268. * Copies a list of files from one place to another.
  269. * @param array $fileList the list of files to be copied (name=>spec).
  270. * The array keys are names displayed during the copy process, and array values are specifications
  271. * for files to be copied. Each array value must be an array of the following structure:
  272. * <ul>
  273. * <li>source: required, the full path of the file/directory to be copied from</li>
  274. * <li>target: required, the full path of the file/directory to be copied to</li>
  275. * <li>callback: optional, the callback to be invoked when copying a file. The callback function
  276. * should be declared as follows:
  277. * <pre>
  278. * function foo($source,$params)
  279. * </pre>
  280. * where $source parameter is the source file path, and the content returned
  281. * by the function will be saved into the target file.</li>
  282. * <li>params: optional, the parameters to be passed to the callback</li>
  283. * </ul>
  284. * @see buildFileList
  285. */
  286. public function copyFiles($fileList)
  287. {
  288. $overwriteAll=false;
  289. foreach($fileList as $name=>$file)
  290. {
  291. $source=strtr($file['source'],'/\\',DIRECTORY_SEPARATOR);
  292. $target=strtr($file['target'],'/\\',DIRECTORY_SEPARATOR);
  293. $callback=isset($file['callback']) ? $file['callback'] : null;
  294. $params=isset($file['params']) ? $file['params'] : null;
  295. if(is_dir($source))
  296. {
  297. $this->ensureDirectory($target);
  298. continue;
  299. }
  300. if($callback!==null)
  301. $content=call_user_func($callback,$source,$params);
  302. else
  303. $content=file_get_contents($source);
  304. if(is_file($target))
  305. {
  306. if($content===file_get_contents($target))
  307. {
  308. echo " unchanged $name\n";
  309. continue;
  310. }
  311. if($overwriteAll)
  312. echo " overwrite $name\n";
  313. else
  314. {
  315. echo " exist $name\n";
  316. echo " ...overwrite? [Yes|No|All|Quit] ";
  317. $answer=trim(fgets(STDIN));
  318. if(!strncasecmp($answer,'q',1))
  319. return;
  320. else if(!strncasecmp($answer,'y',1))
  321. echo " overwrite $name\n";
  322. else if(!strncasecmp($answer,'a',1))
  323. {
  324. echo " overwrite $name\n";
  325. $overwriteAll=true;
  326. }
  327. else
  328. {
  329. echo " skip $name\n";
  330. continue;
  331. }
  332. }
  333. }
  334. else
  335. {
  336. $this->ensureDirectory(dirname($target));
  337. echo " generate $name\n";
  338. }
  339. file_put_contents($target,$content);
  340. }
  341. }
  342. /**
  343. * Builds the file list of a directory.
  344. * This method traverses through the specified directory and builds
  345. * a list of files and subdirectories that the directory contains.
  346. * The result of this function can be passed to {@link copyFiles}.
  347. * @param string $sourceDir the source directory
  348. * @param string $targetDir the target directory
  349. * @param string $baseDir base directory
  350. * @return array the file list (see {@link copyFiles})
  351. */
  352. public function buildFileList($sourceDir, $targetDir, $baseDir='')
  353. {
  354. $list=array();
  355. $handle=opendir($sourceDir);
  356. while(($file=readdir($handle))!==false)
  357. {
  358. if($file==='.' || $file==='..' || $file==='.svn' ||$file==='.yii')
  359. continue;
  360. $sourcePath=$sourceDir.DIRECTORY_SEPARATOR.$file;
  361. $targetPath=$targetDir.DIRECTORY_SEPARATOR.$file;
  362. $name=$baseDir===''?$file : $baseDir.'/'.$file;
  363. $list[$name]=array('source'=>$sourcePath, 'target'=>$targetPath);
  364. if(is_dir($sourcePath))
  365. $list=array_merge($list,$this->buildFileList($sourcePath,$targetPath,$name));
  366. }
  367. closedir($handle);
  368. return $list;
  369. }
  370. /**
  371. * Creates all parent directories if they do not exist.
  372. * @param string $directory the directory to be checked
  373. */
  374. public function ensureDirectory($directory)
  375. {
  376. if(!is_dir($directory))
  377. {
  378. $this->ensureDirectory(dirname($directory));
  379. echo " mkdir ".strtr($directory,'\\','/')."\n";
  380. mkdir($directory);
  381. }
  382. }
  383. /**
  384. * Renders a view file.
  385. * @param string $_viewFile_ view file path
  386. * @param array $_data_ optional data to be extracted as local view variables
  387. * @param boolean $_return_ whether to return the rendering result instead of displaying it
  388. * @return mixed the rendering result if required. Null otherwise.
  389. */
  390. public function renderFile($_viewFile_,$_data_=null,$_return_=false)
  391. {
  392. if(is_array($_data_))
  393. extract($_data_,EXTR_PREFIX_SAME,'data');
  394. else
  395. $data=$_data_;
  396. if($_return_)
  397. {
  398. ob_start();
  399. ob_implicit_flush(false);
  400. require($_viewFile_);
  401. return ob_get_clean();
  402. }
  403. else
  404. require($_viewFile_);
  405. }
  406. /**
  407. * Converts a word to its plural form.
  408. * @param string $name the word to be pluralized
  409. * @return string the pluralized word
  410. */
  411. public function pluralize($name)
  412. {
  413. $rules=array(
  414. '/move$/i' => 'moves',
  415. '/foot$/i' => 'feet',
  416. '/child$/i' => 'children',
  417. '/human$/i' => 'humans',
  418. '/man$/i' => 'men',
  419. '/tooth$/i' => 'teeth',
  420. '/person$/i' => 'people',
  421. '/([m|l])ouse$/i' => '\1ice',
  422. '/(x|ch|ss|sh|us|as|is|os)$/i' => '\1es',
  423. '/([^aeiouy]|qu)y$/i' => '\1ies',
  424. '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
  425. '/(shea|lea|loa|thie)f$/i' => '\1ves',
  426. '/([ti])um$/i' => '\1a',
  427. '/(tomat|potat|ech|her|vet)o$/i' => '\1oes',
  428. '/(bu)s$/i' => '\1ses',
  429. '/(ax|test)is$/i' => '\1es',
  430. '/s$/' => 's',
  431. );
  432. foreach($rules as $rule=>$replacement)
  433. {
  434. if(preg_match($rule,$name))
  435. return preg_replace($rule,$replacement,$name);
  436. }
  437. return $name.'s';
  438. }
  439. /**
  440. * Reads input via the readline PHP extension if that's available, or fgets() if readline is not installed.
  441. *
  442. * @param string $message to echo out before waiting for user input
  443. * @return mixed line read as a string, or false if input has been closed
  444. *
  445. * @since 1.1.9
  446. */
  447. public function prompt($message)
  448. {
  449. if(extension_loaded('readline'))
  450. {
  451. $input = readline($message.' ');
  452. readline_add_history($input);
  453. return $input;
  454. }
  455. else
  456. {
  457. echo $message.' ';
  458. return trim(fgets(STDIN));
  459. }
  460. }
  461. /**
  462. * Asks user to confirm by typing y or n.
  463. *
  464. * @param string $message to echo out before waiting for user input
  465. * @return bool if user confirmed
  466. *
  467. * @since 1.1.9
  468. */
  469. public function confirm($message)
  470. {
  471. echo $message.' [yes|no] ';
  472. return !strncasecmp(trim(fgets(STDIN)),'y',1);
  473. }
  474. }