Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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