Passed
Push — master ( d5adc1...45b033 )
by Fabio
06:12
created

TShellInterpreter   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 82
rs 10
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 17 5
A addActionClass() 0 3 1
A getInstance() 0 7 2
A printGreeting() 0 5 2
A clearActionClass() 0 3 1
A printHelp() 0 11 2
1
<?php
2
/**
3
 * TApplication class file
4
 *
5
 * @author Brad Anderson <[email protected]>
6
 * @link https://github.com/pradosoft/prado
7
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
8
 * @package Prado\Shell
9
 */
10
11
namespace Prado\Shell;
12
13
use Prado\Prado;
14
15
/**
16
 * TShellInterpreter Class
17
 *
18
 * Command line interface, configures the action classes and dispatches the command actions.
19
 *
20
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
21
 * @package Prado\Shell
22
 * @since 3.0.5
23
 */
24
class TShellInterpreter
25
{
26
	/**
27
	 * @var array command action classes
28
	 */
29
	protected $_actions = [];
30
	
31
	protected $_helpPrinted = false;
32
33
	/**
34
	 * @param string $class action class name
35
	 */
36
	public function addActionClass($class)
37
	{
38
		$this->_actions[$class] = new $class;
39
	}
40
41
	/**
42
	 */
43
	public function clearActionClass()
44
	{
45
		$this->_actions = [];
46
	}
47
48
	/**
49
	 * @return TShellInterpreter static instance
50
	 */
51
	public static function getInstance()
52
	{
53
		static $instance;
54
		if ($instance === null) {
55
			$instance = new self;
56
		}
57
		return $instance;
58
	}
59
60
	public function printGreeting()
61
	{
62
		if (!$this->_helpPrinted) {
63
			echo "Command line tools for Prado " . Prado::getVersion() . ".\n";
64
			$this->_helpPrinted = true;
65
		}
66
	}
67
68
	/**
69
	 * Dispatch the command line actions.
70
	 * @param array $args command line arguments
71
	 */
72
	public function run($args)
73
	{
74
		if (count($args) > 1) {
75
			array_shift($args);
76
		}
77
		$valid = false;
78
		$_actions = $this->_actions;
79
		foreach ($_actions as $class => $action) {
80
			if ($action->isValidAction($args)) {
81
				$valid |= $action->performAction($args);
82
				break;
83
			} else {
84
				$valid = false;
85
			}
86
		}
87
		if (!$valid) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $valid of type false|integer is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
88
			$this->printHelp();
89
		}
90
	}
91
92
	/**
93
	 * Print command line help, default action.
94
	 */
95
	public function printHelp()
96
	{
97
		TShellInterpreter::getInstance()->printGreeting();
98
99
		echo "usage: php prado-cli.php action <parameter> [optional]\n";
100
		echo "example: php prado-cli.php flushcaches /prado_app_directory\n\n";
101
		echo "example: php prado-cli.php app /prado_app_directory help\n\n";
102
		echo "example: php prado-cli.php app /prado_app_directory cron tasks\n\n";
103
		echo "actions:\n";
104
		foreach ($this->_actions as $action) {
105
			echo $action->renderHelp();
106
		}
107
	}
108
}
109