PageRenderTime 113ms CodeModel.GetById 42ms RepoModel.GetById 0ms app.codeStats 0ms

/registration/cake/console/cake.php

https://github.com/adivik2000/nigraha
PHP | 511 lines | 307 code | 34 blank | 170 comment | 73 complexity | 6ef735881223f225e7b076e722ff037e MD5 | raw file
  1. #!/usr/bin/php -q
  2. <?php
  3. /* SVN FILE: $Id: cake.php 5318 2007-06-20 09:01:21Z phpnut $ */
  4. /**
  5. * Command-line code generation utility to automate programmer chores.
  6. *
  7. * Shell dispatcher class
  8. *
  9. * PHP versions 4 and 5
  10. *
  11. * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
  12. * Copyright 2005-2007, Cake Software Foundation, Inc.
  13. * 1785 E. Sahara Avenue, Suite 490-204
  14. * Las Vegas, Nevada 89104
  15. *
  16. * Licensed under The MIT License
  17. * Redistributions of files must retain the above copyright notice.
  18. *
  19. * @filesource
  20. * @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
  21. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  22. * @package cake
  23. * @subpackage cake.cake.console
  24. * @since CakePHP(tm) v 1.2.0.5012
  25. * @version $Revision: 5318 $
  26. * @modifiedby $LastChangedBy: phpnut $
  27. * @lastmodified $Date: 2007-06-20 04:01:21 -0500 (Wed, 20 Jun 2007) $
  28. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  29. */
  30. /**
  31. * Shell dispatcher
  32. *
  33. * @package cake
  34. * @subpackage cake.cake.console
  35. */
  36. class ShellDispatcher {
  37. /**
  38. * Standard input stream.
  39. *
  40. * @var filehandle
  41. * @access public
  42. */
  43. var $stdin;
  44. /**
  45. * Standard output stream.
  46. *
  47. * @var filehandle
  48. * @access public
  49. */
  50. var $stdout;
  51. /**
  52. * Standard error stream.
  53. *
  54. * @var filehandle
  55. * @access public
  56. */
  57. var $stderr;
  58. /**
  59. * Contains command switches parsed from the command line.
  60. *
  61. * @var array
  62. * @access public
  63. */
  64. var $params = array();
  65. /**
  66. * Contains arguments parsed from the command line.
  67. *
  68. * @var array
  69. * @access public
  70. */
  71. var $args = array();
  72. /**
  73. * The file name of the shell that was invoked.
  74. *
  75. * @var string
  76. * @access public
  77. */
  78. var $shell = null;
  79. /**
  80. * The class name of the shell that was invoked.
  81. *
  82. * @var string
  83. * @access public
  84. */
  85. var $shellClass = null;
  86. /**
  87. * The command called if public methods are available.
  88. *
  89. * @var string
  90. * @access public
  91. */
  92. var $shellCommand = null;
  93. /**
  94. * The path locations of shells.
  95. *
  96. * @var array
  97. * @access public
  98. */
  99. var $shellPaths = array();
  100. /**
  101. * The path to the current shell location.
  102. *
  103. * @var string
  104. * @access public
  105. */
  106. var $shellPath = null;
  107. /**
  108. * The name of the shell in camelized.
  109. *
  110. * @var string
  111. * @access public
  112. */
  113. var $shellName = null;
  114. /**
  115. * Constructs this ShellDispatcher instance.
  116. *
  117. * @param array $args the argv.
  118. */
  119. function ShellDispatcher($args = array()) {
  120. $this->__construct($args);
  121. }
  122. /**
  123. * Constructor
  124. *
  125. * @param array $args the argv.
  126. */
  127. function __construct($args = array()) {
  128. $this->__initConstants();
  129. $this->parseParams($args);
  130. $this->__initEnvironment();
  131. $this->dispatch();
  132. die("\n");
  133. }
  134. /**
  135. * Defines core configuration.
  136. *
  137. * @access private
  138. */
  139. function __initConstants() {
  140. if (function_exists('ini_set')) {
  141. ini_set('display_errors', '1');
  142. ini_set('error_reporting', E_ALL);
  143. ini_set('html_errors', false);
  144. ini_set('implicit_flush', true);
  145. ini_set('max_execution_time', 60 * 5);
  146. }
  147. define('PHP5', (phpversion() >= 5));
  148. define('DS', DIRECTORY_SEPARATOR);
  149. define('CAKE_CORE_INCLUDE_PATH', dirname(dirname(dirname(__FILE__))));
  150. define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
  151. define('DISABLE_DEFAULT_ERROR_HANDLING', false);
  152. }
  153. /**
  154. * Defines current working environment.
  155. *
  156. * @access private
  157. */
  158. function __initEnvironment() {
  159. $this->stdin = fopen('php://stdin', 'r');
  160. $this->stdout = fopen('php://stdout', 'w');
  161. $this->stderr = fopen('php://stderr', 'w');
  162. if (!isset($this->args[0]) || !isset($this->params['working'])) {
  163. $this->stdout("\nCakePHP Console: ");
  164. $this->stdout('This file has been loaded incorrectly and cannot continue.');
  165. $this->stdout('Please make sure that ' . DIRECTORY_SEPARATOR . 'cake' . DIRECTORY_SEPARATOR . 'console is in your system path,');
  166. $this->stdout('and check the manual for the correct usage of this command.');
  167. $this->stdout('(http://manual.cakephp.org/)');
  168. exit();
  169. }
  170. if (basename(__FILE__) != basename($this->args[0])) {
  171. $this->stdout("\nCakePHP Console: ");
  172. $this->stdout('Warning: the dispatcher may have been loaded incorrectly, which could lead to unexpected results...');
  173. if ($this->getInput('Continue anyway?', array('y', 'n'), 'y') == 'n') {
  174. exit();
  175. }
  176. }
  177. if (!$this->__bootstrap()) {
  178. $this->stdout("\nCakePHP Console: ");
  179. $this->stdout("\nUnable to load Cake core:");
  180. $this->stdout("\tMake sure " . DS . 'cake' . DS . 'libs exists in ' . CAKE_CORE_INCLUDE_PATH);
  181. exit();
  182. }
  183. $this->shiftArgs();
  184. $this->shellPaths = array(
  185. APP . 'vendors' . DS . 'shells' . DS,
  186. VENDORS . 'shells' . DS,
  187. CONSOLE_LIBS
  188. );
  189. }
  190. /**
  191. * Initializes the environment and loads the Cake core.
  192. *
  193. * @return boolean Success.
  194. * @access private
  195. */
  196. function __bootstrap() {
  197. define('ROOT', $this->params['root']);
  198. define('APP_DIR', $this->params['app']);
  199. define('APP_PATH', ROOT . DS . APP_DIR . DS);
  200. $includes = array(
  201. CORE_PATH . 'cake' . DS . 'basics.php',
  202. CORE_PATH . 'cake' . DS . 'config' . DS . 'paths.php',
  203. );
  204. if (!file_exists(APP_PATH . 'config' . DS . 'core.php')) {
  205. $includes[] = CORE_PATH . 'cake' . DS . 'console' . DS . 'libs' . DS . 'templates' . DS . 'skel' . DS . 'config' . DS . 'core.php';
  206. } else {
  207. $includes[] = APP_PATH . 'config' . DS . 'core.php';
  208. }
  209. foreach ($includes as $inc) {
  210. if (!@include_once($inc)) {
  211. $this->stderr("Failed to load Cake core file {$inc}");
  212. return false;
  213. }
  214. }
  215. $libraries = array('object', 'session', 'configure', 'inflector', 'model'.DS.'connection_manager',
  216. 'debugger', 'security', 'controller' . DS . 'controller');
  217. foreach ($libraries as $inc) {
  218. if (!file_exists(LIBS . $inc . '.php')) {
  219. $this->stderr("Failed to load Cake core class " . ucwords($inc));
  220. $this->stderr("(" . LIBS.$inc.".php)");
  221. return false;
  222. }
  223. uses($inc);
  224. }
  225. Configure::getInstance(file_exists(CONFIGS . 'bootstrap.php'));
  226. Configure::write('debug', 1);
  227. return true;
  228. }
  229. /**
  230. * Dispatches a CLI request
  231. *
  232. * @access public
  233. */
  234. function dispatch() {
  235. $this->stdout("\nWelcome to CakePHP v" . Configure::version() . " Console");
  236. $this->stdout("---------------------------------------------------------------");
  237. if (isset($this->args[0])) {
  238. $this->shell = $this->args[0];
  239. $this->shiftArgs();
  240. $this->shellName = Inflector::camelize($this->shell);
  241. $this->shellClass = $this->shellName . 'Shell';
  242. if ($this->shell == 'help') {
  243. $this->help();
  244. } else {
  245. $loaded = false;
  246. foreach ($this->shellPaths as $path) {
  247. $this->shellPath = $path . $this->shell . ".php";
  248. if (file_exists($this->shellPath)) {
  249. $loaded = true;
  250. break;
  251. }
  252. }
  253. if ($loaded) {
  254. require CONSOLE_LIBS . 'shell.php';
  255. require $this->shellPath;
  256. if (class_exists($this->shellClass)) {
  257. $command = null;
  258. if (isset($this->args[0])) {
  259. $command = $this->args[0];
  260. }
  261. $this->shellCommand = Inflector::variable($command);
  262. $shell = new $this->shellClass($this);
  263. $this->shiftArgs();
  264. if ($command == 'help') {
  265. if (method_exists($shell, 'help')) {
  266. $shell->help();
  267. exit();
  268. } else {
  269. $this->help();
  270. }
  271. }
  272. $shell->initialize();
  273. $shell->loadTasks();
  274. foreach ($shell->taskNames as $task) {
  275. $shell->{$task}->initialize();
  276. $shell->{$task}->loadTasks();
  277. }
  278. $task = Inflector::camelize($command);
  279. if (in_array($task, $shell->taskNames)) {
  280. $shell->{$task}->startup();
  281. if (isset($this->args[0]) && $this->args[0] == 'help') {
  282. if (method_exists($shell->{$task}, 'help')) {
  283. $shell->{$task}->help();
  284. exit();
  285. } else {
  286. $this->help();
  287. }
  288. }
  289. $shell->{$task}->execute();
  290. return;
  291. }
  292. $classMethods = get_class_methods($shell);
  293. $privateMethod = $missingCommand = false;
  294. if ((in_array($command, $classMethods) || in_array(strtolower($command), $classMethods)) && strpos($command, '_', 0) === 0) {
  295. $privateMethod = true;
  296. }
  297. if (!in_array($command, $classMethods) && !in_array(strtolower($command), $classMethods)) {
  298. $missingCommand = true;
  299. }
  300. $protectedCommands = array('initialize', 'main','in','out','err','hr',
  301. 'createfile', 'isdir','copydir','object','tostring',
  302. 'requestaction','log','cakeerror', 'shelldispatcher',
  303. '__initconstants','__initenvironment','__construct',
  304. 'dispatch','__bootstrap','getinput','stdout','stderr','parseparams','shiftargs'
  305. );
  306. if (in_array(strtolower($command), $protectedCommands)) {
  307. $missingCommand = true;
  308. }
  309. if ($missingCommand && method_exists($shell, 'main')) {
  310. $shell->startup();
  311. $shell->main();
  312. } elseif ($missingCommand && method_exists($shell, 'help')) {
  313. $shell->help();
  314. } elseif (!$privateMethod && method_exists($shell, $command)) {
  315. $shell->startup();
  316. $shell->{$command}();
  317. } else {
  318. $this->stderr("Unknown {$this->shellName} command '$command'.\nFor usage, try 'cake {$this->shell} help'.\n\n");
  319. }
  320. } else {
  321. $this->stderr('Class '.$this->shellClass.' could not be loaded');
  322. }
  323. } else {
  324. $this->help();
  325. }
  326. }
  327. } else {
  328. $this->help();
  329. }
  330. }
  331. /**
  332. * Prompts the user for input, and returns it.
  333. *
  334. * @param string $prompt Prompt text.
  335. * @param mixed $options Array or string of options.
  336. * @param string $default Default input value.
  337. * @return Either the default value, or the user-provided input.
  338. * @access public
  339. */
  340. function getInput($prompt, $options = null, $default = null) {
  341. if (!is_array($options)) {
  342. $print_options = '';
  343. } else {
  344. $print_options = '(' . implode('/', $options) . ')';
  345. }
  346. if ($default == null) {
  347. $this->stdout($prompt . " $print_options \n" . '> ', false);
  348. } else {
  349. $this->stdout($prompt . " $print_options \n" . "[$default] > ", false);
  350. }
  351. $result = trim(fgets($this->stdin));
  352. if ($default != null && empty($result)) {
  353. return $default;
  354. } else {
  355. return $result;
  356. }
  357. }
  358. /**
  359. * Outputs to the stdout filehandle.
  360. *
  361. * @param string $string String to output.
  362. * @param boolean $newline If true, the outputs gets an added newline.
  363. * @access public
  364. */
  365. function stdout($string, $newline = true) {
  366. if ($newline) {
  367. fwrite($this->stdout, $string . "\n");
  368. } else {
  369. fwrite($this->stdout, $string);
  370. }
  371. }
  372. /**
  373. * Outputs to the stderr filehandle.
  374. *
  375. * @param string $string Error text to output.
  376. * @access public
  377. */
  378. function stderr($string) {
  379. fwrite($this->stderr, 'Error: '. $string);
  380. }
  381. /**
  382. * Parses command line options
  383. *
  384. * @param array $params Parameters to parse
  385. * @access public
  386. */
  387. function parseParams($params) {
  388. $out = array();
  389. for ($i = 0; $i < count($params); $i++) {
  390. if (strpos($params[$i], '-') === 0) {
  391. $this->params[substr($params[$i], 1)] = str_replace('"', '', $params[++$i]);
  392. } else {
  393. $this->args[] = $params[$i];
  394. }
  395. }
  396. $app = 'app';
  397. $root = dirname(dirname(dirname(__FILE__)));
  398. $working = $root;
  399. if (!empty($this->params['working'])) {
  400. $root = dirname($this->params['working']);
  401. $app = basename($this->params['working']);
  402. } else {
  403. $this->params['working'] = $root;
  404. }
  405. if (!empty($this->params['app'])) {
  406. if ($this->params['app']{0} == '/') {
  407. $root = dirname($this->params['app']);
  408. $app = basename($this->params['app']);
  409. } else {
  410. $root = realpath($this->params['working']);
  411. $app = $this->params['app'];
  412. }
  413. unset($this->params['app']);
  414. }
  415. if (in_array($app, array('cake', 'console')) || realpath($root.DS.$app) === dirname(dirname(dirname(__FILE__)))) {
  416. $root = dirname(dirname(dirname(__FILE__)));
  417. $app = 'app';
  418. }
  419. $working = $root . DS . $app;
  420. $this->params = array_merge(array('app'=> $app, 'root'=> $root, 'working'=> $working), $this->params);
  421. }
  422. /**
  423. * Removes first argument and shifts other arguments up
  424. *
  425. * @return boolean False if there are no arguments
  426. * @access public
  427. */
  428. function shiftArgs() {
  429. if (empty($this->args)) {
  430. return false;
  431. }
  432. unset($this->args[0]);
  433. $this->args = array_values($this->args);
  434. return true;
  435. }
  436. /**
  437. * Shows console help
  438. *
  439. * @access public
  440. */
  441. function help() {
  442. $this->stdout("Current Paths:");
  443. $this->stdout(" -working: " . $this->params['working']);
  444. $this->stdout(" -root: " . ROOT);
  445. $this->stdout(" -app: ". APP);
  446. $this->stdout(" -core: " . CORE_PATH);
  447. $this->stdout("");
  448. $this->stdout("Changing Paths:");
  449. $this->stdout("your working path should be the same as your application path");
  450. $this->stdout("to change your path use the '-app' param.");
  451. $this->stdout("Example: -app relative/path/to/myapp or -app /absolute/path/to/myapp");
  452. $this->stdout("\nAvailable Shells:");
  453. foreach ($this->shellPaths as $path) {
  454. if (is_dir($path)) {
  455. $shells = listClasses($path);
  456. $path = r(CORE_PATH, '', $path);
  457. $this->stdout("\n " . $path . ":");
  458. if (empty($shells)) {
  459. $this->stdout("\t - none");
  460. } else {
  461. foreach ($shells as $shell) {
  462. if ($shell != 'shell.php') {
  463. $this->stdout("\t " . r('.php', '', $shell));
  464. }
  465. }
  466. }
  467. }
  468. }
  469. $this->stdout("\nTo run a command, type 'cake shell_name [args]'");
  470. $this->stdout("To get help on a specific command, type 'cake shell_name help'");
  471. exit();
  472. }
  473. }
  474. if (!defined('DISABLE_AUTO_DISPATCH')) {
  475. $dispatcher = new ShellDispatcher($argv);
  476. }
  477. ?>