PageRenderTime 55ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Console/Shell.php

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