PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/yii/framework/console/CConsoleCommand.php

https://github.com/ashie1287/headfirst
PHP | 247 lines | 158 code | 12 blank | 77 comment | 15 complexity | c3122eac83a7fd8b694d809d5b48cb55 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, 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-2010 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CConsoleCommand represents an executable user command.
  12. *
  13. * The {@link run} method must be implemented with the actual command execution logic.
  14. * You may override {@link getHelp} to provide more detailed description of the command.
  15. *
  16. * @author Qiang Xue <qiang.xue@gmail.com>
  17. * @version $Id: CConsoleCommand.php 1832 2010-02-20 03:22:45Z qiang.xue $
  18. * @package system.console
  19. * @since 1.0
  20. */
  21. abstract class CConsoleCommand extends CComponent
  22. {
  23. private $_name;
  24. private $_runner;
  25. /**
  26. * Executes the command.
  27. * @param array command line parameters for this command.
  28. */
  29. public abstract function run($args);
  30. /**
  31. * Constructor.
  32. * @param string name of the command
  33. * @param CConsoleCommandRunner the command runner
  34. */
  35. public function __construct($name,$runner)
  36. {
  37. $this->_name=$name;
  38. $this->_runner=$runner;
  39. }
  40. /**
  41. * @return string the command name.
  42. */
  43. public function getName()
  44. {
  45. return $this->_name;
  46. }
  47. /**
  48. * @return CConsoleCommandRunner the command runner instance
  49. */
  50. public function getCommandRunner()
  51. {
  52. return $this->_runner;
  53. }
  54. /**
  55. * Provides the command description.
  56. * This method may be overridden to return the actual command description.
  57. * @return string the command description. Defaults to 'Usage: php entry-script.php command-name'.
  58. */
  59. public function getHelp()
  60. {
  61. return 'Usage: '.$this->getCommandRunner()->getScriptName().' '.$this->getName();
  62. }
  63. /**
  64. * Displays a usage error.
  65. * This method will then terminate the execution of the current application.
  66. * @param string the error message
  67. */
  68. public function usageError($message)
  69. {
  70. die("Error: $message\n\n".$this->getHelp()."\n");
  71. }
  72. /**
  73. * Copies a list of files from one place to another.
  74. * @param array the list of files to be copied (name=>spec).
  75. * The array keys are names displayed during the copy process, and array values are specifications
  76. * for files to be copied. Each array value must be an array of the following structure:
  77. * <ul>
  78. * <li>source: required, the full path of the file/directory to be copied from</li>
  79. * <li>target: required, the full path of the file/directory to be copied to</li>
  80. * <li>callback: optional, the callback to be invoked when copying a file. The callback function
  81. * should be declared as follows:
  82. * <pre>
  83. * function foo($source,$params)
  84. * </pre>
  85. * where $source parameter is the source file path, and the content returned
  86. * by the function will be saved into the target file.</li>
  87. * <li>params: optional, the parameters to be passed to the callback</li>
  88. * </ul>
  89. * @see buildFileList
  90. */
  91. public function copyFiles($fileList)
  92. {
  93. $overwriteAll=false;
  94. foreach($fileList as $name=>$file)
  95. {
  96. $source=strtr($file['source'],'/\\',DIRECTORY_SEPARATOR);
  97. $target=strtr($file['target'],'/\\',DIRECTORY_SEPARATOR);
  98. $callback=isset($file['callback']) ? $file['callback'] : null;
  99. $params=isset($file['params']) ? $file['params'] : null;
  100. if(is_dir($source))
  101. {
  102. $this->ensureDirectory($target);
  103. continue;
  104. }
  105. if($callback!==null)
  106. $content=call_user_func($callback,$source,$params);
  107. else
  108. $content=file_get_contents($source);
  109. if(is_file($target))
  110. {
  111. if($content===file_get_contents($target))
  112. {
  113. echo " unchanged $name\n";
  114. continue;
  115. }
  116. if($overwriteAll)
  117. echo " overwrite $name\n";
  118. else
  119. {
  120. echo " exist $name\n";
  121. echo " ...overwrite? [Yes|No|All|Quit] ";
  122. $answer=trim(fgets(STDIN));
  123. if(!strncasecmp($answer,'q',1))
  124. return;
  125. else if(!strncasecmp($answer,'y',1))
  126. echo " overwrite $name\n";
  127. else if(!strncasecmp($answer,'a',1))
  128. {
  129. echo " overwrite $name\n";
  130. $overwriteAll=true;
  131. }
  132. else
  133. {
  134. echo " skip $name\n";
  135. continue;
  136. }
  137. }
  138. }
  139. else
  140. {
  141. $this->ensureDirectory(dirname($target));
  142. echo " generate $name\n";
  143. }
  144. file_put_contents($target,$content);
  145. }
  146. }
  147. /**
  148. * Builds the file list of a directory.
  149. * This method traverses through the specified directory and builds
  150. * a list of files and subdirectories that the directory contains.
  151. * The result of this function can be passed to {@link copyFiles}.
  152. * @param string the source directory
  153. * @param string the target directory
  154. * @param string base directory
  155. * @return array the file list (see {@link copyFiles})
  156. */
  157. public function buildFileList($sourceDir, $targetDir, $baseDir='')
  158. {
  159. $list=array();
  160. $handle=opendir($sourceDir);
  161. while(($file=readdir($handle))!==false)
  162. {
  163. if($file==='.' || $file==='..' || $file==='.svn' ||$file==='.yii')
  164. continue;
  165. $sourcePath=$sourceDir.DIRECTORY_SEPARATOR.$file;
  166. $targetPath=$targetDir.DIRECTORY_SEPARATOR.$file;
  167. $name=$baseDir===''?$file : $baseDir.'/'.$file;
  168. $list[$name]=array('source'=>$sourcePath, 'target'=>$targetPath);
  169. if(is_dir($sourcePath))
  170. $list=array_merge($list,$this->buildFileList($sourcePath,$targetPath,$name));
  171. }
  172. closedir($handle);
  173. return $list;
  174. }
  175. /**
  176. * Creates all parent directories if they do not exist.
  177. * @param string the directory to be checked
  178. */
  179. public function ensureDirectory($directory)
  180. {
  181. if(!is_dir($directory))
  182. {
  183. $this->ensureDirectory(dirname($directory));
  184. echo " mkdir ".strtr($directory,'\\','/')."\n";
  185. mkdir($directory);
  186. }
  187. }
  188. /**
  189. * Renders a view file.
  190. * @param string view file path
  191. * @param array optional data to be extracted as local view variables
  192. * @param boolean whether to return the rendering result instead of displaying it
  193. * @return mixed the rendering result if required. Null otherwise.
  194. */
  195. public function renderFile($_viewFile_,$_data_=null,$_return_=false)
  196. {
  197. if(is_array($_data_))
  198. extract($_data_,EXTR_PREFIX_SAME,'data');
  199. else
  200. $data=$_data_;
  201. if($_return_)
  202. {
  203. ob_start();
  204. ob_implicit_flush(false);
  205. require($_viewFile_);
  206. return ob_get_clean();
  207. }
  208. else
  209. require($_viewFile_);
  210. }
  211. /**
  212. * Converts a word to its plural form.
  213. * @param string the word to be pluralized
  214. * @return string the pluralized word
  215. */
  216. public function pluralize($name)
  217. {
  218. $rules=array(
  219. '/(x|ch|ss|sh|us|as|is|os)$/i' => '\1es',
  220. '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
  221. '/(m)an$/i' => '\1en',
  222. '/(child)$/i' => '\1ren',
  223. '/(r)y$/i' => '\1ies',
  224. '/s$/' => 's',
  225. );
  226. foreach($rules as $rule=>$replacement)
  227. {
  228. if(preg_match($rule,$name))
  229. return preg_replace($rule,$replacement,$name);
  230. }
  231. return $name.'s';
  232. }
  233. }