cake_script.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * Short description for file.
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
  11. * Copyright 2005-2007, Cake Software Foundation, Inc.
  12. * 1785 E. Sahara Avenue, Suite 490-204
  13. * Las Vegas, Nevada 89104
  14. *
  15. * Licensed under The MIT License
  16. * Redistributions of files must retain the above copyright notice.
  17. *
  18. * @filesource
  19. * @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
  20. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  21. * @package cake
  22. * @subpackage cake.cake.scripts
  23. * @since CakePHP(tm) v 1.2.0.4604
  24. * @version $Revision$
  25. * @modifiedby $LastChangedBy$
  26. * @lastmodified $Date$
  27. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  28. */
  29. /**
  30. * Base class for command-line utilities for automating programmer chores.
  31. *
  32. * @package cake
  33. * @subpackage cake.cake.scripts
  34. */
  35. class CakeScript extends Object {
  36. /**
  37. * ConsoleDispatcher object
  38. *
  39. * @var object An instance of the ConsoleDispatcher object that loaded this script
  40. */
  41. var $Dispatch = null;
  42. /**
  43. * If true, the script will ask for permission to perform actions.
  44. *
  45. * @var boolean
  46. */
  47. var $interactive = true;
  48. /**
  49. * Holds the DATABASE_CONFIG object for the app. Null if database.php could not be found,
  50. * or the app does not exist.
  51. *
  52. * @var object
  53. */
  54. var $dbConfig = null;
  55. /**
  56. * Contains command switches parsed from the command line.
  57. *
  58. * @var array
  59. */
  60. var $params = array();
  61. /**
  62. * Contains arguments parsed from the command line.
  63. *
  64. * @var array
  65. */
  66. var $args = array();
  67. /**
  68. * Constructs this CakeScript instance.
  69. *
  70. */
  71. function __construct(&$dispatch) {
  72. $this->Dispatch = & $dispatch;
  73. $this->params = & $this->Dispatch->params;
  74. $this->args = & $this->Dispatch->args;
  75. $this->name = & $this->Dispatch->scriptName;
  76. $this->command = & $this->Dispatch->scriptCommand;
  77. }
  78. /**
  79. * Initializes the CakeScript
  80. * can be overriden in subclasses
  81. *
  82. * @return null
  83. */
  84. function initialize() {
  85. if($this->_loadDbConfig()) {
  86. //$this->_loadModel();
  87. }
  88. $this->hr();
  89. $this->out('Name: '. APP_DIR);
  90. $this->out('Path: '. ROOT . DS . APP_DIR);
  91. $this->hr();
  92. }
  93. /**
  94. * Loads database file and constructs DATABASE_CONFIG class
  95. * makes $this->dbConfig available to subclasses
  96. *
  97. * @return bool
  98. */
  99. function _loadDbConfig() {
  100. if(config('database')) {
  101. if (class_exists('DATABASE_CONFIG')) {
  102. $this->dbConfig = new DATABASE_CONFIG();
  103. return true;
  104. }
  105. }
  106. $this->err('Database config could not be loaded');
  107. return false;
  108. }
  109. /**
  110. * Loads AppModel file and constructs AppModel class
  111. * makes $this->AppModel available to subclasses
  112. *
  113. * @return bool
  114. */
  115. function _loadModel() {
  116. uses ('model'.DS.'connection_manager',
  117. 'model'.DS.'datasources'.DS.'dbo_source', 'model'.DS.'model'
  118. );
  119. if(loadModel()) {
  120. $this->AppModel = & new AppModel();
  121. return true;
  122. }
  123. $this->err('AppModel could not be loaded');
  124. return false;
  125. }
  126. /**
  127. * Prompts the user for input, and returns it.
  128. *
  129. * @param string $prompt Prompt text.
  130. * @param mixed $options Array or string of options.
  131. * @param string $default Default input value.
  132. * @return Either the default value, or the user-provided input.
  133. */
  134. function in($prompt, $options = null, $default = null) {
  135. return $this->Dispatch->getInput($prompt, $options, $default);
  136. }
  137. /**
  138. * Outputs to the stdout filehandle.
  139. *
  140. * @param string $string String to output.
  141. * @param boolean $newline If true, the outputs gets an added newline.
  142. */
  143. function out($string, $newline = true) {
  144. return $this->Dispatch->stdout($string, $newline);
  145. }
  146. /**
  147. * Outputs to the stderr filehandle.
  148. *
  149. * @param string $string Error text to output.
  150. */
  151. function err($string) {
  152. return $this->Dispatch->stderr($string."\n");
  153. }
  154. /**
  155. * Outputs a series of minus characters to the standard output, acts as a visual separator.
  156. *
  157. */
  158. function hr($newline = false) {
  159. if ($newline) {
  160. $this->out("\n");
  161. }
  162. $this->out('---------------------------------------------------------------');
  163. if ($newline) {
  164. $this->out("\n");
  165. }
  166. }
  167. /**
  168. * Displays a formatted error message
  169. *
  170. * @param unknown_type $title
  171. * @param unknown_type $msg
  172. */
  173. function displayError($title, $msg) {
  174. $out = "\n";
  175. $out .= "Error: $title\n";
  176. $out .= "$msg\n";
  177. $out .= "\n";
  178. $this->out($out);
  179. exit();
  180. }
  181. /**
  182. * Will check the number args matches otherwise throw an error
  183. *
  184. * @param unknown_type $expectedNum
  185. * @param unknown_type $command
  186. */
  187. function _checkArgs($expectedNum, $command = null) {
  188. if(!$command) {
  189. $command = $this->command;
  190. }
  191. if (count($this->args) < $expectedNum) {
  192. $this->displayError('Wrong number of parameters: '.count($this->args), 'Please type \'cake '.$this->Dispatch->script.' help\' for help on usage of the '.$command.' command.');
  193. }
  194. }
  195. /**
  196. * Creates a file at given path.
  197. *
  198. * @param string $path Where to put the file.
  199. * @param string $contents Content to put in the file.
  200. * @return Success
  201. */
  202. function createFile ($path, $contents) {
  203. $path = str_replace(DS . DS, DS, $path);
  204. echo "\nCreating file $path\n";
  205. if (is_file($path) && $this->interactive === true) {
  206. $this->out(__("File exists, overwrite?", true). " {$path} (y/n/q):");
  207. $key = trim(fgets($this->stdin));
  208. if ($key == 'q') {
  209. $this->out(__("Quitting.", true) ."\n");
  210. exit;
  211. } elseif ($key == 'a') {
  212. $this->dont_ask = true;
  213. } elseif ($key == 'y') {
  214. } else {
  215. $this->out(__("Skip", true) ." {$path}\n");
  216. return false;
  217. }
  218. }
  219. uses('file');
  220. if ($File = new File($path, true)) {
  221. $File->write($contents);
  222. $this->out(__("Wrote", true) ."{$path}\n");
  223. return true;
  224. } else {
  225. $this->err(__("Error! Could not write to", true)." {$path}.\n");
  226. return false;
  227. }
  228. }
  229. /**
  230. * Outputs usage text on the standard output. Implement it in subclasses.
  231. *
  232. */
  233. function help() {
  234. // empty
  235. }
  236. /**
  237. * Returns true if given path is a directory.
  238. *
  239. * @param string $path
  240. * @return True if given path is a directory.
  241. */
  242. function isDir($path) {
  243. if(is_dir($path)) {
  244. return true;
  245. } else {
  246. return false;
  247. }
  248. }
  249. /**
  250. * Recursive directory copy.
  251. *
  252. * @param string $fromDir
  253. * @param string $toDir
  254. * @param octal $chmod
  255. * @param boolean $verbose
  256. * @return Success.
  257. */
  258. function copyDir($fromDir, $toDir, $chmod = 0755, $verbose = false) {
  259. $errors = array();
  260. $messages = array();
  261. if (!is_dir($toDir)) {
  262. uses('folder');
  263. $folder = new Folder();
  264. $folder->mkdirr($toDir, 0755);
  265. }
  266. if (!is_writable($toDir)) {
  267. $errors[] = 'target '.$toDir.' is not writable';
  268. }
  269. if (!is_dir($fromDir)) {
  270. $errors[] = 'source '.$fromDir.' is not a directory';
  271. }
  272. if (!empty($errors)) {
  273. if ($verbose) {
  274. foreach($errors as $err) {
  275. $this->stdout('Error: '.$err);
  276. }
  277. }
  278. return false;
  279. }
  280. $exceptions = array('.','..','.svn');
  281. $handle = opendir($fromDir);
  282. while (false !== ($item = readdir($handle))) {
  283. if (!in_array($item,$exceptions)) {
  284. $from = str_replace('//','/',$fromDir.'/'.$item);
  285. $to = str_replace('//','/',$toDir.'/'.$item);
  286. if (is_file($from)) {
  287. if (@copy($from, $to)) {
  288. chmod($to, $chmod);
  289. touch($to, filemtime($from));
  290. $messages[] = 'File copied from '.$from.' to '.$to;
  291. } else {
  292. $errors[] = 'cannot copy file from '.$from.' to '.$to;
  293. }
  294. }
  295. if (is_dir($from)) {
  296. if (@mkdir($to)) {
  297. chmod($to, $chmod);
  298. $messages[] = 'Directory created: '.$to;
  299. } else {
  300. $errors[] = 'cannot create directory '.$to;
  301. }
  302. $this->copyDir($from,$to,$chmod,$verbose);
  303. }
  304. }
  305. }
  306. closedir($handle);
  307. if ($verbose) {
  308. foreach($errors as $err) {
  309. $this->stdout('Error: '.$err);
  310. }
  311. foreach($messages as $msg) {
  312. $this->stdout($msg);
  313. }
  314. }
  315. return true;
  316. }
  317. }
  318. ?>