PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Console/Shell.php

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