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

TShellAction::getAppConfigFile()   B

Complexity

Conditions 9
Paths 5

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 10
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 16
rs 8.0555
1
<?php
2
/**
3
 * TShellAction 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
 * Base class for command line actions.
17
 *
18
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
19
 * @package Prado\Shell
20
 * @since 3.0.5
21
 */
22
abstract class TShellAction extends \Prado\TComponent
23
{
24
	protected $action;
25
	protected $parameters;
26
	protected $optional;
27
	protected $description;
28
	/**
29
	 * Execute the action.
30
	 * @param array $args command line parameters
31
	 * @return bool true if action was handled
32
	 */
33
	abstract public function performAction($args);
34
35
	/**
36
	 * Creates a directory and sets its mode
37
	 * @param string $dir directory name
38
	 * @param int $mask directory mode mask suitable for chmod()
39
	 */
40
	protected function createDirectory($dir, $mask)
41
	{
42
		if (!is_dir($dir)) {
43
			mkdir($dir);
44
			echo "creating $dir\n";
45
		}
46
		if (is_dir($dir)) {
47
			chmod($dir, $mask);
48
		}
49
	}
50
51
	/**
52
	 * Creates a file and fills it with content
53
	 * @param string $filename file name
54
	 * @param int $content file contents
55
	 */
56
	protected function createFile($filename, $content)
57
	{
58
		if (!is_file($filename)) {
59
			file_put_contents($filename, $content);
60
			echo "creating $filename\n";
61
		}
62
	}
63
64
	/**
65
	 * Checks if specified parameters are suitable for the specified action
66
	 * @param array $args parameters
67
	 * @return bool
68
	 */
69
	public function isValidAction($args)
70
	{
71
		return 0 == strcasecmp($args[0], $this->action) &&
72
			count($args) - 1 >= count($this->parameters);
73
	}
74
75
	/**
76
	 * @return string
77
	 */
78
	public function renderHelp()
79
	{
80
		$params = [];
81
		foreach ($this->parameters as $v) {
82
			$params[] = '<' . $v . '>';
83
		}
84
		$parameters = implode(' ', $params);
85
		$options = [];
86
		foreach ($this->optional as $v) {
87
			$options[] = '[' . $v . ']';
88
		}
89
		$optional = (strlen($parameters) ? ' ' : '') . implode(' ', $options);
90
		$description = '';
91
		foreach (explode("\n", wordwrap($this->description, 65)) as $line) {
92
			$description .= '    ' . $line . "\n";
93
		}
94
		return <<<EOD
95
  {$this->action} {$parameters}{$optional}
96
{$description}
97
98
EOD;
99
	}
100
101
	/**
102
	 * Initalize a Prado application inside the specified directory
103
	 * @param string $directory directory name
104
	 * @return false|TApplication
0 ignored issues
show
Bug introduced by
The type Prado\Shell\TApplication was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
105
	 */
106
	protected function initializePradoApplication($directory)
107
	{
108
		$_SERVER['SCRIPT_FILENAME'] = $directory . '/index.php';
109
		$app_dir = realpath($directory . '/protected/');
110
		if ($app_dir !== false && is_dir($app_dir)) {
111
			if (Prado::getApplication() === null) {
112
				$app = new TShellApplication($app_dir);
113
				$app->run();
114
				$dir = substr(str_replace(realpath('./'), '', $app_dir), 1);
115
				TShellInterpreter::getInstance()->printGreeting();
116
				echo '** Loaded PRADO appplication in directory "' . $dir . "\".\n";
117
			}
118
119
			return Prado::getApplication();
0 ignored issues
show
Bug Best Practice introduced by
The expression return Prado\Prado::getApplication() also could return the type Prado\TApplication which is incompatible with the documented return type Prado\Shell\TApplication|false.
Loading history...
120
		} else {
121
			TShellInterpreter::getInstance()->printGreeting();
122
			echo '+' . str_repeat('-', 77) . "+\n";
123
			echo '** Unable to load PRADO application in directory "' . $directory . "\".\n";
124
			echo '+' . str_repeat('-', 77) . "+\n";
125
		}
126
		return false;
127
	}
128
129
	/**
130
	 * @param string $app_dir application directory
131
	 * @return false|string
132
	 */
133
	protected function getAppConfigFile($app_dir)
134
	{
135
		if (false !== ($xml = realpath($app_dir . '/application.xml')) && is_file($xml)) {
136
			return $xml;
137
		}
138
		if (false !== ($xml = realpath($app_dir . '/protected/application.xml')) && is_file($xml)) {
139
			return $xml;
140
		}
141
		if (false !== ($php = realpath($app_dir . '/application.php')) && is_file($php)) {
142
			return $php;
143
		}
144
		if (false !== ($php = realpath($app_dir . '/protected/application.php')) && is_file($php)) {
145
			return $php;
146
		}
147
		echo '** Unable to find application.xml or application.php in ' . $app_dir . "\n";
148
		return false;
149
	}
150
}
151