84% of 359OPs |
85% of 91Lines |
72% of 57Branches |
24% of 55Paths |
Method | OPs | OPs % | Lines | Line % | Branches | Branches % | Paths | Path % |
---|---|---|---|---|---|---|---|---|
mageekguy\atoum\script\configurable::__construct() | 18 | 100% | 3 | 100% | 1 | 0% | 1 | 100% |
mageekguy\atoum\script\configurable::setIncluder() | 16 | 100% | 2 | 100% | 1 | 100% | 1 | 100% |
mageekguy\atoum\script\configurable::getIncluder() | 6 | 100% | 1 | 100% | 1 | 100% | 1 | 100% |
mageekguy\atoum\script\configurable::getConfigFiles() | 6 | 100% | 1 | 100% | 1 | 100% | 1 | 100% |
mageekguy\atoum\script\configurable::useConfigFile() | 11 | 100% | 1 | 100% | 1 | 100% | 1 | 100% |
mageekguy\atoum\script\configurable::useDefaultConfigFiles() | 88 | 70% | 21 | 100% | 26 | 69% | 38 | 3% |
mageekguy\atoum\script\configurable::run() | 17 | 100% | 2 | 100% | 1 | 100% | 1 | 100% |
mageekguy\atoum\script\configurable::getSubDirectoryPath() | 56 | 84% | 16 | 100% | 13 | 77% | 5 | 40% |
mageekguy\atoum\script\configurable::setArgumentHandlers() | 48 | 100% | 28 | 57% | 1 | 100% | 1 | 100% |
mageekguy\atoum\script\configurable::includeConfigFile() | 93 | 75% | 16 | 88% | 11 | 64% | 5 | 60% |
# | |
---|---|
1 |
<?php |
2 |
|
3 |
namespace mageekguy\atoum\script; |
4 |
|
5 |
use |
6 |
mageekguy\atoum, |
7 |
mageekguy\atoum\includer, |
8 |
mageekguy\atoum\exceptions |
9 |
; |
10 |
|
11 |
abstract class configurable extends atoum\script |
12 |
{ |
13 |
const defaultConfigFile = '.config.php'; |
14 |
|
15 |
protected $includer = null; |
16 |
protected $configFiles = array(); |
17 |
|
18 |
public function __construct($name, atoum\adapter $adapter = null)100% |
19 |
{ |
20 |
parent::__construct($name, $adapter); |
21 |
|
22 |
$this->setIncluder(); |
23 |
} |
24 |
|
25 |
public function setIncluder(atoum\includer $includer = null)100% |
26 |
{ |
27 |
$this->includer = $includer ?: new atoum\includer(); |
28 |
|
29 |
return $this; |
30 |
} |
31 |
|
32 |
public function getIncluder()100% |
33 |
{ |
34 |
return $this->includer; |
35 |
} |
36 |
|
37 |
public function getConfigFiles()100% |
38 |
{ |
39 |
return $this->configFiles; |
40 |
} |
41 |
|
42 |
public function useConfigFile($path)100% |
43 |
{ |
44 |
return $this->includeConfigFile($path); |
45 |
} |
46 |
|
47 |
public function useDefaultConfigFiles($startDirectory = null)100% |
48 |
{ |
49 |
if ($startDirectory === null) |
50 |
{ |
51 |
$startDirectory = $this->getDirectory(); |
52 |
} |
53 |
|
54 |
$configFilesFound = $this->configFiles; |
55 |
|
56 |
foreach (self::getSubDirectoryPath($startDirectory) as $directory) |
57 |
{ |
58 |
try |
59 |
{ |
60 |
$this->useConfigFile($directory . static::defaultConfigFile); |
61 |
} |
62 |
catch (atoum\includer\exception $exception) {} |
63 |
} |
64 |
|
65 |
if ($configFilesFound === $this->configFiles) |
66 |
{ |
67 |
$workingDirectory = $this->adapter->getcwd(); |
68 |
|
69 |
if ($workingDirectory !== $startDirectory) |
70 |
{ |
71 |
foreach (self::getSubDirectoryPath($workingDirectory) as $directory) |
72 |
{ |
73 |
try |
74 |
{ |
75 |
$this->useConfigFile($directory . static::defaultConfigFile); |
76 |
} |
77 |
catch (atoum\includer\exception $exception) {} |
78 |
} |
79 |
} |
80 |
} |
81 |
|
82 |
return $this; |
83 |
} |
84 |
|
85 |
public function run(array $arguments = array())100% |
86 |
{ |
87 |
$this->useDefaultConfigFiles(); |
88 |
|
89 |
return parent::run($arguments); |
90 |
} |
91 |
|
92 |
public static function getSubDirectoryPath($directory, $directorySeparator = null)100% |
93 |
{ |
94 |
$directorySeparator = $directorySeparator ?: DIRECTORY_SEPARATOR; |
95 |
|
96 |
$paths = array(); |
97 |
|
98 |
if ($directory != '') |
99 |
{ |
100 |
if ($directory == $directorySeparator) |
101 |
{ |
102 |
$paths[] = $directory; |
103 |
} |
104 |
else |
105 |
{ |
106 |
$directory = rtrim($directory, $directorySeparator); |
107 |
|
108 |
$path = ''; |
109 |
|
110 |
foreach (explode($directorySeparator, $directory) as $subDirectory) |
111 |
{ |
112 |
$path .= $subDirectory . $directorySeparator; |
113 |
|
114 |
$paths[] = $path; |
115 |
} |
116 |
} |
117 |
} |
118 |
|
119 |
return $paths; |
120 |
} |
121 |
|
122 |
protected function setArgumentHandlers()57% |
123 |
{ |
124 |
parent::setArgumentHandlers() |
125 |
->addArgumentHandler( |
126 |
function($script, $argument, $values) { |
127 |
if (sizeof($values) !== 0) |
128 |
{ |
129 |
throw new exceptions\logic\invalidArgument(sprintf($script->getLocale()->_('Bad usage of %s, do php %s --help for more informations'), $argument, $script->getName())); |
130 |
} |
131 |
|
132 |
$script->help(); |
133 |
}, |
134 |
array('-h', '--help'), |
135 |
null, |
136 |
$this->locale->_('Display this help') |
137 |
) |
138 |
->addArgumentHandler( |
139 |
function($script, $argument, $files) { |
140 |
if (sizeof($files) <= 0) |
141 |
{ |
142 |
throw new exceptions\logic\invalidArgument(sprintf($script->getLocale()->_('Bad usage of %s, do php %s --help for more informations'), $argument, $script->getName())); |
143 |
} |
144 |
|
145 |
foreach ($files as $path) |
146 |
{ |
147 |
try |
148 |
{ |
149 |
$script->useConfigFile($path); |
150 |
} |
151 |
catch (includer\exception $exception) |
152 |
{ |
153 |
throw new exceptions\logic\invalidArgument(sprintf($script->getLocale()->_('Configuration file \'%s\' does not exist'), $path)); |
154 |
} |
155 |
} |
156 |
}, |
157 |
array('-c', '--configurations'), |
158 |
'<file>...', |
159 |
$this->locale->_('Use all configuration files <file>'), |
160 |
PHP_INT_MAX |
161 |
) |
162 |
; |
163 |
|
164 |
return $this; |
165 |
} |
166 |
|
167 |
protected function includeConfigFile($path, \closure $callback = null)88% |
168 |
{ |
169 |
if ($callback === null) |
170 |
{ |
171 |
$script = $this; |
172 |
|
173 |
$callback = function($path) use ($script) { include_once($path); }; |
174 |
} |
175 |
|
176 |
try |
177 |
{ |
178 |
$this->includer->resetErrors()->includePath($path, $callback); |
179 |
|
180 |
$this->configFiles[] = $path; |
181 |
} |
182 |
catch (atoum\includer\exception $exception) |
183 |
{ |
184 |
throw new atoum\includer\exception(sprintf($this->getLocale()->_('Unable to find configuration file \'%s\''), $path)); |
185 |
} |
186 |
|
187 |
$firstError = $this->includer->getFirstError(); |
188 |
|
189 |
if ($firstError !== null) |
190 |
{ |
191 |
list($error, $message, $file, $line) = $firstError; |
192 |
|
193 |
throw new exceptions\runtime($message . ' in ' . $path . ' at line ' . $line, $error); |
194 |
} |
195 |
|
196 |
return $this; |
197 |
} |
198 |
} |