Shell.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. <?php
  2. /**
  3. * Base class for Shells
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @since CakePHP(tm) v 1.2.0.5012
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. App::uses('TaskCollection', 'Console');
  19. App::uses('ConsoleOutput', 'Console');
  20. App::uses('ConsoleInput', 'Console');
  21. App::uses('ConsoleInputSubcommand', 'Console');
  22. App::uses('ConsoleOptionParser', 'Console');
  23. App::uses('File', 'Utility');
  24. /**
  25. * Base class for command-line utilities for automating programmer chores.
  26. *
  27. * @package Cake.Console
  28. */
  29. class Shell extends Object {
  30. /**
  31. * Output constant making verbose shells.
  32. */
  33. const VERBOSE = 2;
  34. /**
  35. * Output constant for making normal shells.
  36. */
  37. const NORMAL = 1;
  38. /**
  39. * Output constants for making quiet shells.
  40. */
  41. const QUIET = 0;
  42. /**
  43. * An instance of ConsoleOptionParser that has been configured for this class.
  44. *
  45. * @var ConsoleOptionParser
  46. */
  47. public $OptionParser;
  48. /**
  49. * If true, the script will ask for permission to perform actions.
  50. *
  51. * @var boolean
  52. */
  53. public $interactive = true;
  54. /**
  55. * Contains command switches parsed from the command line.
  56. *
  57. * @var array
  58. */
  59. public $params = array();
  60. /**
  61. * The command (method/task) that is being run.
  62. *
  63. * @var string
  64. */
  65. public $command;
  66. /**
  67. * Contains arguments parsed from the command line.
  68. *
  69. * @var array
  70. */
  71. public $args = array();
  72. /**
  73. * The name of the shell in camelized.
  74. *
  75. * @var string
  76. */
  77. public $name = null;
  78. /**
  79. * The name of the plugin the shell belongs to.
  80. * Is automatically set by ShellDispatcher when a shell is constructed.
  81. *
  82. * @var string
  83. */
  84. public $plugin = null;
  85. /**
  86. * Contains tasks to load and instantiate
  87. *
  88. * @var array
  89. * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::$tasks
  90. */
  91. public $tasks = array();
  92. /**
  93. * Contains the loaded tasks
  94. *
  95. * @var array
  96. */
  97. public $taskNames = array();
  98. /**
  99. * Contains models to load and instantiate
  100. *
  101. * @var array
  102. * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::$uses
  103. */
  104. public $uses = array();
  105. /**
  106. * Task Collection for the command, used to create Tasks.
  107. *
  108. * @var TaskCollection
  109. */
  110. public $Tasks;
  111. /**
  112. * Normalized map of tasks.
  113. *
  114. * @var string
  115. */
  116. protected $_taskMap = array();
  117. /**
  118. * stdout object.
  119. *
  120. * @var ConsoleOutput
  121. */
  122. public $stdout;
  123. /**
  124. * stderr object.
  125. *
  126. * @var ConsoleOutput
  127. */
  128. public $stderr;
  129. /**
  130. * stdin object
  131. *
  132. * @var ConsoleInput
  133. */
  134. public $stdin;
  135. /**
  136. * Constructs this Shell instance.
  137. *
  138. * @param ConsoleOutput $stdout A ConsoleOutput object for stdout.
  139. * @param ConsoleOutput $stderr A ConsoleOutput object for stderr.
  140. * @param ConsoleInput $stdin A ConsoleInput object for stdin.
  141. * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell
  142. */
  143. public function __construct($stdout = null, $stderr = null, $stdin = null) {
  144. if (!$this->name) {
  145. $this->name = Inflector::camelize(str_replace(array('Shell', 'Task'), '', get_class($this)));
  146. }
  147. $this->Tasks = new TaskCollection($this);
  148. $this->stdout = $stdout ? $stdout : new ConsoleOutput('php://stdout');
  149. $this->stderr = $stderr ? $stderr : new ConsoleOutput('php://stderr');
  150. $this->stdin = $stdin ? $stdin : new ConsoleInput('php://stdin');
  151. $this->_useLogger();
  152. $parent = get_parent_class($this);
  153. if ($this->tasks !== null && $this->tasks !== false) {
  154. $this->_mergeVars(array('tasks'), $parent, true);
  155. }
  156. if ($this->uses !== null && $this->uses !== false) {
  157. $this->_mergeVars(array('uses'), $parent, false);
  158. }
  159. }
  160. /**
  161. * Initializes the Shell
  162. * acts as constructor for subclasses
  163. * allows configuration of tasks prior to shell execution
  164. *
  165. * @return void
  166. * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::initialize
  167. */
  168. public function initialize() {
  169. $this->_loadModels();
  170. }
  171. /**
  172. * Starts up the Shell and displays the welcome message.
  173. * Allows for checking and configuring prior to command or main execution
  174. *
  175. * Override this method if you want to remove the welcome information,
  176. * or otherwise modify the pre-command flow.
  177. *
  178. * @return void
  179. * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::startup
  180. */
  181. public function startup() {
  182. $this->_welcome();
  183. }
  184. /**
  185. * Displays a header for the shell
  186. *
  187. * @return void
  188. */
  189. protected function _welcome() {
  190. $this->out();
  191. $this->out(__d('cake_console', '<info>Welcome to CakePHP %s Console</info>', 'v' . Configure::version()));
  192. $this->hr();
  193. $this->out(__d('cake_console', 'App : %s', APP_DIR));
  194. $this->out(__d('cake_console', 'Path: %s', APP));
  195. $this->hr();
  196. }
  197. /**
  198. * If $uses = true
  199. * Loads AppModel file and constructs AppModel class
  200. * makes $this->AppModel available to subclasses
  201. * If public $uses is an array of models will load those models
  202. *
  203. * @return boolean
  204. */
  205. protected function _loadModels() {
  206. if (empty($this->uses)) {
  207. return false;
  208. }
  209. App::uses('ClassRegistry', 'Utility');
  210. $uses = is_array($this->uses) ? $this->uses : array($this->uses);
  211. $modelClassName = $uses[0];
  212. if (strpos($uses[0], '.') !== false) {
  213. list($plugin, $modelClassName) = explode('.', $uses[0]);
  214. }
  215. $this->modelClass = $modelClassName;
  216. foreach ($uses as $modelClass) {
  217. list($plugin, $modelClass) = pluginSplit($modelClass, true);
  218. $this->{$modelClass} = ClassRegistry::init($plugin . $modelClass);
  219. }
  220. return true;
  221. }
  222. /**
  223. * Loads tasks defined in public $tasks
  224. *
  225. * @return boolean
  226. */
  227. public function loadTasks() {
  228. if ($this->tasks === true || empty($this->tasks) || empty($this->Tasks)) {
  229. return true;
  230. }
  231. $this->_taskMap = TaskCollection::normalizeObjectArray((array)$this->tasks);
  232. $this->taskNames = array_merge($this->taskNames, array_keys($this->_taskMap));
  233. return true;
  234. }
  235. /**
  236. * Check to see if this shell has a task with the provided name.
  237. *
  238. * @param string $task The task name to check.
  239. * @return boolean Success
  240. * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::hasTask
  241. */
  242. public function hasTask($task) {
  243. return isset($this->_taskMap[Inflector::camelize($task)]);
  244. }
  245. /**
  246. * Check to see if this shell has a callable method by the given name.
  247. *
  248. * @param string $name The method name to check.
  249. * @return boolean
  250. * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::hasMethod
  251. */
  252. public function hasMethod($name) {
  253. try {
  254. $method = new ReflectionMethod($this, $name);
  255. if (!$method->isPublic() || substr($name, 0, 1) === '_') {
  256. return false;
  257. }
  258. if ($method->getDeclaringClass()->name == 'Shell') {
  259. return false;
  260. }
  261. return true;
  262. } catch (ReflectionException $e) {
  263. return false;
  264. }
  265. }
  266. /**
  267. * Dispatch a command to another Shell. Similar to Object::requestAction()
  268. * but intended for running shells from other shells.
  269. *
  270. * ### Usage:
  271. *
  272. * With a string command:
  273. *
  274. * `return $this->dispatchShell('schema create DbAcl');`
  275. *
  276. * Avoid using this form if you have string arguments, with spaces in them.
  277. * The dispatched will be invoked incorrectly. Only use this form for simple
  278. * command dispatching.
  279. *
  280. * With an array command:
  281. *
  282. * `return $this->dispatchShell('schema', 'create', 'i18n', '--dry');`
  283. *
  284. * @return mixed The return of the other shell.
  285. * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::dispatchShell
  286. */
  287. public function dispatchShell() {
  288. $args = func_get_args();
  289. if (is_string($args[0]) && count($args) === 1) {
  290. $args = explode(' ', $args[0]);
  291. }
  292. $Dispatcher = new ShellDispatcher($args, false);
  293. return $Dispatcher->dispatch();
  294. }
  295. /**
  296. * Runs the Shell with the provided argv.
  297. *
  298. * Delegates calls to Tasks and resolves methods inside the class. Commands are looked
  299. * up with the following order:
  300. *
  301. * - Method on the shell.
  302. * - Matching task name.
  303. * - `main()` method.
  304. *
  305. * If a shell implements a `main()` method, all missing method calls will be sent to
  306. * `main()` with the original method name in the argv.
  307. *
  308. * @param string $command The command name to run on this shell. If this argument is empty,
  309. * and the shell has a `main()` method, that will be called instead.
  310. * @param array $argv Array of arguments to run the shell with. This array should be missing the shell name.
  311. * @return void
  312. * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::runCommand
  313. */
  314. public function runCommand($command, $argv) {
  315. $isTask = $this->hasTask($command);
  316. $isMethod = $this->hasMethod($command);
  317. $isMain = $this->hasMethod('main');
  318. if ($isTask || $isMethod && $command !== 'execute') {
  319. array_shift($argv);
  320. }
  321. $this->OptionParser = $this->getOptionParser();
  322. try {
  323. list($this->params, $this->args) = $this->OptionParser->parse($argv, $command);
  324. } catch (ConsoleException $e) {
  325. $this->out($this->OptionParser->help($command));
  326. return false;
  327. }
  328. if (!empty($this->params['quiet'])) {
  329. $this->_useLogger(false);
  330. }
  331. if (!empty($this->params['plugin'])) {
  332. CakePlugin::load($this->params['plugin']);
  333. }
  334. $this->command = $command;
  335. if (!empty($this->params['help'])) {
  336. return $this->_displayHelp($command);
  337. }
  338. if (($isTask || $isMethod || $isMain) && $command !== 'execute') {
  339. $this->startup();
  340. }
  341. if ($isTask) {
  342. $command = Inflector::camelize($command);
  343. return $this->{$command}->runCommand('execute', $argv);
  344. }
  345. if ($isMethod) {
  346. return $this->{$command}();
  347. }
  348. if ($isMain) {
  349. return $this->main();
  350. }
  351. $this->out($this->OptionParser->help($command));
  352. return false;
  353. }
  354. /**
  355. * Display the help in the correct format
  356. *
  357. * @param string $command
  358. * @return void
  359. */
  360. protected function _displayHelp($command) {
  361. $format = 'text';
  362. if (!empty($this->args[0]) && $this->args[0] == 'xml') {
  363. $format = 'xml';
  364. $this->stdout->outputAs(ConsoleOutput::RAW);
  365. } else {
  366. $this->_welcome();
  367. }
  368. return $this->out($this->OptionParser->help($command, $format));
  369. }
  370. /**
  371. * Gets the option parser instance and configures it.
  372. * By overriding this method you can configure the ConsoleOptionParser before returning it.
  373. *
  374. * @return ConsoleOptionParser
  375. * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::getOptionParser
  376. */
  377. public function getOptionParser() {
  378. $name = ($this->plugin ? $this->plugin . '.' : '') . $this->name;
  379. $parser = new ConsoleOptionParser($name);
  380. return $parser;
  381. }
  382. /**
  383. * Overload get for lazy building of tasks
  384. *
  385. * @param string $name
  386. * @return Shell Object of Task
  387. */
  388. public function __get($name) {
  389. if (empty($this->{$name}) && in_array($name, $this->taskNames)) {
  390. $properties = $this->_taskMap[$name];
  391. $this->{$name} = $this->Tasks->load($properties['class'], $properties['settings']);
  392. $this->{$name}->args =& $this->args;
  393. $this->{$name}->params =& $this->params;
  394. $this->{$name}->initialize();
  395. $this->{$name}->loadTasks();
  396. }
  397. return $this->{$name};
  398. }
  399. /**
  400. * Prompts the user for input, and returns it.
  401. *
  402. * @param string $prompt Prompt text.
  403. * @param string|array $options Array or string of options.
  404. * @param string $default Default input value.
  405. * @return mixed Either the default value, or the user-provided input.
  406. * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::in
  407. */
  408. public function in($prompt, $options = null, $default = null) {
  409. if (!$this->interactive) {
  410. return $default;
  411. }
  412. $originalOptions = $options;
  413. $in = $this->_getInput($prompt, $originalOptions, $default);
  414. if ($options && is_string($options)) {
  415. if (strpos($options, ',')) {
  416. $options = explode(',', $options);
  417. } elseif (strpos($options, '/')) {
  418. $options = explode('/', $options);
  419. } else {
  420. $options = array($options);
  421. }
  422. }
  423. if (is_array($options)) {
  424. $options = array_merge(
  425. array_map('strtolower', $options),
  426. array_map('strtoupper', $options),
  427. $options
  428. );
  429. while ($in === '' || !in_array($in, $options)) {
  430. $in = $this->_getInput($prompt, $originalOptions, $default);
  431. }
  432. }
  433. return $in;
  434. }
  435. /**
  436. * Prompts the user for input, and returns it.
  437. *
  438. * @param string $prompt Prompt text.
  439. * @param string|array $options Array or string of options.
  440. * @param string $default Default input value.
  441. * @return Either the default value, or the user-provided input.
  442. */
  443. protected function _getInput($prompt, $options, $default) {
  444. if (!is_array($options)) {
  445. $printOptions = '';
  446. } else {
  447. $printOptions = '(' . implode('/', $options) . ')';
  448. }
  449. if ($default === null) {
  450. $this->stdout->write('<question>' . $prompt . '</question>' . " $printOptions \n" . '> ', 0);
  451. } else {
  452. $this->stdout->write('<question>' . $prompt . '</question>' . " $printOptions \n" . "[$default] > ", 0);
  453. }
  454. $result = $this->stdin->read();
  455. if ($result === false) {
  456. $this->_stop(1);
  457. }
  458. $result = trim($result);
  459. if ($default !== null && ($result === '' || $result === null)) {
  460. return $default;
  461. }
  462. return $result;
  463. }
  464. /**
  465. * Wrap a block of text.
  466. * Allows you to set the width, and indenting on a block of text.
  467. *
  468. * ### Options
  469. *
  470. * - `width` The width to wrap to. Defaults to 72
  471. * - `wordWrap` Only wrap on words breaks (spaces) Defaults to true.
  472. * - `indent` Indent the text with the string provided. Defaults to null.
  473. *
  474. * @param string $text Text the text to format.
  475. * @param string|integer|array $options Array of options to use, or an integer to wrap the text to.
  476. * @return string Wrapped / indented text
  477. * @see String::wrap()
  478. * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::wrapText
  479. */
  480. public function wrapText($text, $options = array()) {
  481. return String::wrap($text, $options);
  482. }
  483. /**
  484. * Outputs a single or multiple messages to stdout. If no parameters
  485. * are passed outputs just a newline.
  486. *
  487. * ### Output levels
  488. *
  489. * There are 3 built-in output level. Shell::QUIET, Shell::NORMAL, Shell::VERBOSE.
  490. * The verbose and quiet output levels, map to the `verbose` and `quiet` output switches
  491. * present in most shells. Using Shell::QUIET for a message means it will always display.
  492. * While using Shell::VERBOSE means it will only display when verbose output is toggled.
  493. *
  494. * @param string|array $message A string or a an array of strings to output
  495. * @param integer $newlines Number of newlines to append
  496. * @param integer $level The message's output level, see above.
  497. * @return integer|boolean Returns the number of bytes returned from writing to stdout.
  498. * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::out
  499. */
  500. public function out($message = null, $newlines = 1, $level = Shell::NORMAL) {
  501. $currentLevel = Shell::NORMAL;
  502. if (!empty($this->params['verbose'])) {
  503. $currentLevel = Shell::VERBOSE;
  504. }
  505. if (!empty($this->params['quiet'])) {
  506. $currentLevel = Shell::QUIET;
  507. }
  508. if ($level <= $currentLevel) {
  509. return $this->stdout->write($message, $newlines);
  510. }
  511. return true;
  512. }
  513. /**
  514. * Outputs a single or multiple error messages to stderr. If no parameters
  515. * are passed outputs just a newline.
  516. *
  517. * @param string|array $message A string or a an array of strings to output
  518. * @param integer $newlines Number of newlines to append
  519. * @return void
  520. * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::err
  521. */
  522. public function err($message = null, $newlines = 1) {
  523. $this->stderr->write($message, $newlines);
  524. }
  525. /**
  526. * Returns a single or multiple linefeeds sequences.
  527. *
  528. * @param integer $multiplier Number of times the linefeed sequence should be repeated
  529. * @return string
  530. * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::nl
  531. */
  532. public function nl($multiplier = 1) {
  533. return str_repeat(ConsoleOutput::LF, $multiplier);
  534. }
  535. /**
  536. * Outputs a series of minus characters to the standard output, acts as a visual separator.
  537. *
  538. * @param integer $newlines Number of newlines to pre- and append
  539. * @param integer $width Width of the line, defaults to 63
  540. * @return void
  541. * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::hr
  542. */
  543. public function hr($newlines = 0, $width = 63) {
  544. $this->out(null, $newlines);
  545. $this->out(str_repeat('-', $width));
  546. $this->out(null, $newlines);
  547. }
  548. /**
  549. * Displays a formatted error message
  550. * and exits the application with status code 1
  551. *
  552. * @param string $title Title of the error
  553. * @param string $message An optional error message
  554. * @return void
  555. * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::error
  556. */
  557. public function error($title, $message = null) {
  558. $this->err(__d('cake_console', '<error>Error:</error> %s', $title));
  559. if (!empty($message)) {
  560. $this->err($message);
  561. }
  562. $this->_stop(1);
  563. }
  564. /**
  565. * Clear the console
  566. *
  567. * @return void
  568. * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::clear
  569. */
  570. public function clear() {
  571. if (empty($this->params['noclear'])) {
  572. if (DS === '/') {
  573. passthru('clear');
  574. } else {
  575. passthru('cls');
  576. }
  577. }
  578. }
  579. /**
  580. * Creates a file at given path
  581. *
  582. * @param string $path Where to put the file.
  583. * @param string $contents Content to put in the file.
  584. * @return boolean Success
  585. * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::createFile
  586. */
  587. public function createFile($path, $contents) {
  588. $path = str_replace(DS . DS, DS, $path);
  589. $this->out();
  590. if (is_file($path) && $this->interactive === true) {
  591. $this->out(__d('cake_console', '<warning>File `%s` exists</warning>', $path));
  592. $key = $this->in(__d('cake_console', 'Do you want to overwrite?'), array('y', 'n', 'q'), 'n');
  593. if (strtolower($key) == 'q') {
  594. $this->out(__d('cake_console', '<error>Quitting</error>.'), 2);
  595. $this->_stop();
  596. } elseif (strtolower($key) != 'y') {
  597. $this->out(__d('cake_console', 'Skip `%s`', $path), 2);
  598. return false;
  599. }
  600. } else {
  601. $this->out(__d('cake_console', 'Creating file %s', $path));
  602. }
  603. $File = new File($path, true);
  604. if ($File->exists() && $File->writable()) {
  605. $data = $File->prepare($contents);
  606. $File->write($data);
  607. $this->out(__d('cake_console', '<success>Wrote</success> `%s`', $path));
  608. return true;
  609. }
  610. $this->err(__d('cake_console', '<error>Could not write to `%s`</error>.', $path), 2);
  611. return false;
  612. }
  613. /**
  614. * Action to create a Unit Test
  615. *
  616. * @return boolean Success
  617. */
  618. protected function _checkUnitTest() {
  619. if (class_exists('PHPUnit_Framework_TestCase')) {
  620. return true;
  621. //@codingStandardsIgnoreStart
  622. } elseif (@include 'PHPUnit' . DS . 'Autoload.php') {
  623. //@codingStandardsIgnoreEnd
  624. return true;
  625. } elseif (App::import('Vendor', 'phpunit', array('file' => 'PHPUnit' . DS . 'Autoload.php'))) {
  626. return true;
  627. }
  628. $prompt = __d('cake_console', 'PHPUnit is not installed. Do you want to bake unit test files anyway?');
  629. $unitTest = $this->in($prompt, array('y', 'n'), 'y');
  630. $result = strtolower($unitTest) == 'y' || strtolower($unitTest) == 'yes';
  631. if ($result) {
  632. $this->out();
  633. $this->out(__d('cake_console', 'You can download PHPUnit from %s', 'http://phpunit.de'));
  634. }
  635. return $result;
  636. }
  637. /**
  638. * Makes absolute file path easier to read
  639. *
  640. * @param string $file Absolute file path
  641. * @return string short path
  642. * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::shortPath
  643. */
  644. public function shortPath($file) {
  645. $shortPath = str_replace(ROOT, null, $file);
  646. $shortPath = str_replace('..' . DS, '', $shortPath);
  647. return str_replace(DS . DS, DS, $shortPath);
  648. }
  649. /**
  650. * Creates the proper controller path for the specified controller class name
  651. *
  652. * @param string $name Controller class name
  653. * @return string Path to controller
  654. */
  655. protected function _controllerPath($name) {
  656. return Inflector::underscore($name);
  657. }
  658. /**
  659. * Creates the proper controller plural name for the specified controller class name
  660. *
  661. * @param string $name Controller class name
  662. * @return string Controller plural name
  663. */
  664. protected function _controllerName($name) {
  665. return Inflector::pluralize(Inflector::camelize($name));
  666. }
  667. /**
  668. * Creates the proper model camelized name (singularized) for the specified name
  669. *
  670. * @param string $name Name
  671. * @return string Camelized and singularized model name
  672. */
  673. protected function _modelName($name) {
  674. return Inflector::camelize(Inflector::singularize($name));
  675. }
  676. /**
  677. * Creates the proper underscored model key for associations
  678. *
  679. * @param string $name Model class name
  680. * @return string Singular model key
  681. */
  682. protected function _modelKey($name) {
  683. return Inflector::underscore($name) . '_id';
  684. }
  685. /**
  686. * Creates the proper model name from a foreign key
  687. *
  688. * @param string $key Foreign key
  689. * @return string Model name
  690. */
  691. protected function _modelNameFromKey($key) {
  692. return Inflector::camelize(str_replace('_id', '', $key));
  693. }
  694. /**
  695. * creates the singular name for use in views.
  696. *
  697. * @param string $name
  698. * @return string $name
  699. */
  700. protected function _singularName($name) {
  701. return Inflector::variable(Inflector::singularize($name));
  702. }
  703. /**
  704. * Creates the plural name for views
  705. *
  706. * @param string $name Name to use
  707. * @return string Plural name for views
  708. */
  709. protected function _pluralName($name) {
  710. return Inflector::variable(Inflector::pluralize($name));
  711. }
  712. /**
  713. * Creates the singular human name used in views
  714. *
  715. * @param string $name Controller name
  716. * @return string Singular human name
  717. */
  718. protected function _singularHumanName($name) {
  719. return Inflector::humanize(Inflector::underscore(Inflector::singularize($name)));
  720. }
  721. /**
  722. * Creates the plural human name used in views
  723. *
  724. * @param string $name Controller name
  725. * @return string Plural human name
  726. */
  727. protected function _pluralHumanName($name) {
  728. return Inflector::humanize(Inflector::underscore($name));
  729. }
  730. /**
  731. * Find the correct path for a plugin. Scans $pluginPaths for the plugin you want.
  732. *
  733. * @param string $pluginName Name of the plugin you want ie. DebugKit
  734. * @return string $path path to the correct plugin.
  735. */
  736. protected function _pluginPath($pluginName) {
  737. if (CakePlugin::loaded($pluginName)) {
  738. return CakePlugin::path($pluginName);
  739. }
  740. return current(App::path('plugins')) . $pluginName . DS;
  741. }
  742. /**
  743. * Used to enable or disable logging stream output to stdout and stderr
  744. * If you don't wish to see in your stdout or stderr everything that is logged
  745. * through CakeLog, call this function with first param as false
  746. *
  747. * @param boolean $enable wheter to enable CakeLog output or not
  748. * @return void
  749. */
  750. protected function _useLogger($enable = true) {
  751. if (!$enable) {
  752. CakeLog::drop('stdout');
  753. CakeLog::drop('stderr');
  754. return;
  755. }
  756. CakeLog::config('stdout', array(
  757. 'engine' => 'ConsoleLog',
  758. 'types' => array('notice', 'info'),
  759. 'stream' => $this->stdout,
  760. ));
  761. CakeLog::config('stderr', array(
  762. 'engine' => 'ConsoleLog',
  763. 'types' => array('emergency', 'alert', 'critical', 'error', 'warning', 'debug'),
  764. 'stream' => $this->stderr,
  765. ));
  766. }
  767. }