41% of 888OPs |
43% of 253Lines |
22% of 116Branches |
1% of 4118Paths |
# | |
---|---|
1 |
<?php |
2 |
|
3 |
namespace mageekguy\atoum\scripts; |
4 |
|
5 |
use |
6 |
mageekguy\atoum, |
7 |
mageekguy\atoum\exceptions, |
8 |
mageekguy\atoum\scripts\treemap\analyzers |
9 |
; |
10 |
|
11 |
class treemap extends atoum\script\configurable |
12 |
{ |
13 |
const defaultConfigFile = '.treemap.php'; |
14 |
const dataFile = 'data.json'; |
15 |
|
16 |
protected $projectName = null; |
17 |
protected $projectUrl = null; |
18 |
protected $codeUrl = null; |
19 |
protected $directories = array(); |
20 |
protected $htmlDirectory = null; |
21 |
protected $outputDirectory = null; |
22 |
protected $onlyJsonFile = false; |
23 |
protected $analyzers = array(); |
24 |
protected $categorizers = array(); |
25 |
|
26 |
public function __construct($name, atoum\adapter $adapter = null)100% |
27 |
{ |
28 |
parent::__construct($name, $adapter); |
29 |
|
30 |
$this->setIncluder(); |
31 |
} |
32 |
|
33 |
public function getProjectName()100% |
34 |
{ |
35 |
return $this->projectName; |
36 |
} |
37 |
|
38 |
public function setProjectName($projectName)100% |
39 |
{ |
40 |
$this->projectName = $projectName; |
41 |
|
42 |
return $this; |
43 |
} |
44 |
|
45 |
public function getProjectUrl()100% |
46 |
{ |
47 |
return $this->projectUrl; |
48 |
} |
49 |
|
50 |
public function setProjectUrl($projectUrl)100% |
51 |
{ |
52 |
$this->projectUrl = $projectUrl; |
53 |
|
54 |
return $this; |
55 |
} |
56 |
|
57 |
public function getCodeUrl()100% |
58 |
{ |
59 |
return $this->codeUrl; |
60 |
} |
61 |
|
62 |
public function setCodeUrl($codeUrl)100% |
63 |
{ |
64 |
$this->codeUrl = $codeUrl; |
65 |
|
66 |
return $this; |
67 |
} |
68 |
|
69 |
public function addDirectory($directory)100% |
70 |
{ |
71 |
if (in_array($directory, $this->directories) === false) |
72 |
{ |
73 |
$this->directories[] = $directory; |
74 |
} |
75 |
|
76 |
return $this; |
77 |
} |
78 |
|
79 |
public function getDirectories()100% |
80 |
{ |
81 |
return $this->directories; |
82 |
} |
83 |
|
84 |
public function setHtmlDirectory($path = null)100% |
85 |
{ |
86 |
$this->htmlDirectory = $path; |
87 |
|
88 |
return $this; |
89 |
} |
90 |
|
91 |
public function getHtmlDirectory()100% |
92 |
{ |
93 |
return $this->htmlDirectory; |
94 |
} |
95 |
|
96 |
public function setOutputDirectory($directory)100% |
97 |
{ |
98 |
$this->outputDirectory = $directory; |
99 |
|
100 |
return $this; |
101 |
} |
102 |
|
103 |
public function getOutputDirectory()100% |
104 |
{ |
105 |
return $this->outputDirectory; |
106 |
} |
107 |
|
108 |
public function getOnlyJsonFile($boolean = null)100% |
109 |
{ |
110 |
if ($boolean !== null) |
111 |
{ |
112 |
$this->onlyJsonFile = ($boolean == true); |
113 |
} |
114 |
|
115 |
return $this->onlyJsonFile; |
116 |
} |
117 |
|
118 |
public function getAnalyzers()100% |
119 |
{ |
120 |
return $this->analyzers; |
121 |
} |
122 |
|
123 |
public function addAnalyzer(treemap\analyzer $analyzer)100% |
124 |
{ |
125 |
$this->analyzers[] = $analyzer; |
126 |
|
127 |
return $this; |
128 |
} |
129 |
|
130 |
public function getCategorizers()100% |
131 |
{ |
132 |
return $this->categorizers; |
133 |
} |
134 |
|
135 |
public function addCategorizer(treemap\categorizer $categorizer)100% |
136 |
{ |
137 |
$this->categorizers[] = $categorizer; |
138 |
|
139 |
return $this; |
140 |
} |
141 |
|
142 |
public function useConfigFile($path)100% |
143 |
{ |
144 |
$script = $this; |
145 |
|
146 |
return $this->includeConfigFile($path, function($path) use ($script) { include_once($path); }); |
147 |
} |
148 |
|
149 |
protected function setArgumentHandlers()63% |
150 |
{ |
151 |
return parent::setArgumentHandlers() |
152 |
->addArgumentHandler( |
153 |
function($script, $argument, $projectName) { |
154 |
if (sizeof($projectName) != 1) |
155 |
{ |
156 |
throw new exceptions\logic\invalidArgument(sprintf($script->getLocale()->_('Bad usage of %s, do php %s --help for more informations'), $argument, $script->getName())); |
157 |
} |
158 |
|
159 |
$script->setProjectName(current($projectName)); |
160 |
}, |
161 |
array('-pn', '--project-name'), |
162 |
'<string>', |
163 |
$this->locale->_('Set project name <string>') |
164 |
) |
165 |
->addArgumentHandler( |
166 |
function($script, $argument, $projectUrl) { |
167 |
if (sizeof($projectUrl) != 1) |
168 |
{ |
169 |
throw new exceptions\logic\invalidArgument(sprintf($script->getLocale()->_('Bad usage of %s, do php %s --help for more informations'), $argument, $script->getUrl())); |
170 |
} |
171 |
|
172 |
$script->setProjectUrl(current($projectUrl)); |
173 |
}, |
174 |
array('-pu', '--project-url'), |
175 |
'<string>', |
176 |
$this->locale->_('Set project url <string>') |
177 |
) |
178 |
->addArgumentHandler( |
179 |
function($script, $argument, $codeUrl) { |
180 |
if (sizeof($codeUrl) != 1) |
181 |
{ |
182 |
throw new exceptions\logic\invalidArgument(sprintf($script->getLocale()->_('Bad usage of %s, do php %s --help for more informations'), $argument, $script->getUrl())); |
183 |
} |
184 |
|
185 |
$script->setCodeUrl(current($codeUrl)); |
186 |
}, |
187 |
array('-cu', '--code-url'), |
188 |
'<string>', |
189 |
$this->locale->_('Set code url <string>') |
190 |
) |
191 |
->addArgumentHandler( |
192 |
function($script, $argument, $directories) { |
193 |
if (sizeof($directories) <= 0) |
194 |
{ |
195 |
throw new exceptions\logic\invalidArgument(sprintf($script->getLocale()->_('Bad usage of %s, do php %s --help for more informations'), $argument, $script->getName())); |
196 |
} |
197 |
|
198 |
foreach ($directories as $directory) |
199 |
{ |
200 |
$script->addDirectory($directory); |
201 |
} |
202 |
}, |
203 |
array('-d', '--directories'), |
204 |
'<directory>...', |
205 |
$this->locale->_('Scan all directories <directory>') |
206 |
) |
207 |
->addArgumentHandler( |
208 |
function($script, $argument, $outputDirectory) { |
209 |
if (sizeof($outputDirectory) != 1) |
210 |
{ |
211 |
throw new exceptions\logic\invalidArgument(sprintf($script->getLocale()->_('Bad usage of %s, do php %s --help for more informations'), $argument, $script->getName())); |
212 |
} |
213 |
|
214 |
$script->setOutputDirectory(current($outputDirectory)); |
215 |
}, |
216 |
array('-od', '--output-directory'), |
217 |
'<directory>', |
218 |
$this->locale->_('Generate treemap in directory <directory>') |
219 |
) |
220 |
->addArgumentHandler( |
221 |
function($script, $argument, $value) { |
222 |
if (sizeof($value) != 0) |
223 |
{ |
224 |
throw new exceptions\logic\invalidArgument(sprintf($script->getLocale()->_('Bad usage of %s, do php %s --help for more informations'), $argument, $script->getName())); |
225 |
} |
226 |
|
227 |
$script->getOnlyJsonFile(true); |
228 |
}, |
229 |
array('-ojf', '--only-json-file'), |
230 |
null, |
231 |
$this->locale->_('Generate only JSON file') |
232 |
) |
233 |
->addArgumentHandler( |
234 |
function($script, $argument, $value) { |
235 |
if (sizeof($value) != 0) |
236 |
{ |
237 |
throw new exceptions\logic\invalidArgument(sprintf($script->getLocale()->_('Bad usage of %s, do php %s --help for more informations'), $argument, $script->getName())); |
238 |
} |
239 |
|
240 |
$script->addAnalyzer(new analyzers\sloc()); |
241 |
}, |
242 |
array('--sloc'), |
243 |
null, |
244 |
$this->locale->_('Count source line of code (SLOC)') |
245 |
) |
246 |
->addArgumentHandler( |
247 |
function($script, $argument, $value) { |
248 |
if (sizeof($value) != 0) |
249 |
{ |
250 |
throw new exceptions\logic\invalidArgument(sprintf($script->getLocale()->_('Bad usage of %s, do php %s --help for more informations'), $argument, $script->getName())); |
251 |
} |
252 |
|
253 |
$script->addAnalyzer(new analyzers\token()); |
254 |
}, |
255 |
array('--php-token'), |
256 |
null, |
257 |
$this->locale->_('Count PHP tokens') |
258 |
) |
259 |
->addArgumentHandler( |
260 |
function($script, $argument, $value) { |
261 |
if (sizeof($value) != 0) |
262 |
{ |
263 |
throw new exceptions\logic\invalidArgument(sprintf($script->getLocale()->_('Bad usage of %s, do php %s --help for more informations'), $argument, $script->getName())); |
264 |
} |
265 |
|
266 |
$script->addAnalyzer(new analyzers\size()); |
267 |
}, |
268 |
array('--file-size'), |
269 |
null, |
270 |
$this->locale->_('Get file size') |
271 |
) |
272 |
->addArgumentHandler( |
273 |
function($script, $argument, $htmlDirectory) { |
274 |
if (sizeof($htmlDirectory) != 1) |
275 |
{ |
276 |
throw new exceptions\logic\invalidArgument(sprintf($script->getLocale()->_('Bad usage of %s, do php %s --help for more informations'), $argument, $script->getName())); |
277 |
} |
278 |
|
279 |
$script->setHtmlDirectory(current($htmlDirectory)); |
280 |
}, |
281 |
array('-hd', '--html-directory'), |
282 |
'<directory>', |
283 |
$this->locale->_('Use html files in <directory> to generate treemap') |
284 |
) |
285 |
; |
286 |
} |
287 |
|
288 |
protected function doRun()0% |
289 |
{ |
290 |
if ($this->projectName === null) |
291 |
{ |
292 |
throw new exceptions\runtime($this->locale->_('Project name is undefined')); |
293 |
} |
294 |
|
295 |
if (sizeof($this->directories) <= 0) |
296 |
{ |
297 |
throw new exceptions\runtime($this->locale->_('Directories are undefined')); |
298 |
} |
299 |
|
300 |
if ($this->outputDirectory === null) |
301 |
{ |
302 |
throw new exceptions\runtime($this->locale->_('Output directory is undefined')); |
303 |
} |
304 |
|
305 |
if ($this->htmlDirectory === null) |
306 |
{ |
307 |
throw new exceptions\runtime($this->locale->_('Html directory is undefined')); |
308 |
} |
309 |
|
310 |
$maxDepth = 1; |
311 |
|
312 |
$nodes = array( |
313 |
'name' => $this->projectName, |
314 |
'url' => $this->projectUrl, |
315 |
'path' => '', |
316 |
'children' => array() |
317 |
); |
318 |
|
319 |
foreach ($this->directories as $rootDirectory) |
320 |
{ |
321 |
try |
322 |
{ |
323 |
$directoryIterator = new \recursiveIteratorIterator(new atoum\iterators\filters\recursives\dot($rootDirectory)); |
324 |
} |
325 |
catch (\exception $exception) |
326 |
{ |
327 |
throw new exceptions\runtime($this->locale->_('Directory \'' . $rootDirectory . '\' does not exist')); |
328 |
} |
329 |
|
330 |
foreach ($directoryIterator as $file) |
331 |
{ |
332 |
$node = & $nodes; |
333 |
|
334 |
$directories = ltrim(substr(dirname($file->getPathname()), strlen($rootDirectory)), DIRECTORY_SEPARATOR); |
335 |
|
336 |
if ($directories !== '') |
337 |
{ |
338 |
$directories = explode(DIRECTORY_SEPARATOR, $directories); |
339 |
|
340 |
$depth = sizeof($directories); |
341 |
|
342 |
if ($depth > $maxDepth) |
343 |
{ |
344 |
$maxDepth = $depth; |
345 |
} |
346 |
|
347 |
foreach ($directories as $directory) |
348 |
{ |
349 |
$childFound = false; |
350 |
|
351 |
foreach ($node['children'] as $key => $child) |
352 |
{ |
353 |
$childFound = ($child['name'] === $directory); |
354 |
|
355 |
if ($childFound === true) |
356 |
{ |
357 |
break; |
358 |
} |
359 |
} |
360 |
|
361 |
if ($childFound === false) |
362 |
{ |
363 |
$key = sizeof($node['children']); |
364 |
$node['children'][] = array( |
365 |
'name' => $directory, |
366 |
'path' => $node['path'] . DIRECTORY_SEPARATOR . $directory, |
367 |
'children' => array() |
368 |
); |
369 |
} |
370 |
|
371 |
$node = & $node['children'][$key]; |
372 |
} |
373 |
} |
374 |
|
375 |
$child = array( |
376 |
'name' => $file->getFilename(), |
377 |
'path' => $node['path'] . DIRECTORY_SEPARATOR . $file->getFilename(), |
378 |
'metrics' => array(), |
379 |
'type' => '' |
380 |
); |
381 |
|
382 |
foreach ($this->analyzers as $analyzer) |
383 |
{ |
384 |
$child['metrics'][$analyzer->getMetricName()] = $analyzer->getMetricFromFile($file); |
385 |
} |
386 |
|
387 |
foreach ($this->categorizers as $categorizer) |
388 |
{ |
389 |
if ($categorizer->categorize($file) === true) |
390 |
{ |
391 |
$child['type'] = $categorizer->getName(); |
392 |
|
393 |
break; |
394 |
} |
395 |
} |
396 |
|
397 |
$node['children'][] = $child; |
398 |
} |
399 |
} |
400 |
|
401 |
$data = array( |
402 |
'codeUrl' => $this->codeUrl, |
403 |
'metrics' => array(), |
404 |
'categorizers' => array(), |
405 |
'maxDepth' => $maxDepth, |
406 |
'nodes' => $nodes |
407 |
); |
408 |
|
409 |
foreach ($this->analyzers as $analyzer) |
410 |
{ |
411 |
$data['metrics'][] = array( |
412 |
'name' => $analyzer->getMetricName(), |
413 |
'label' => $analyzer->getMetricLabel() |
414 |
); |
415 |
} |
416 |
|
417 |
foreach ($this->categorizers as $categorizer) |
418 |
{ |
419 |
$data['categorizers'][] = array( |
420 |
'name' => $categorizer->getName(), |
421 |
'minDepthColor' => $categorizer->getMinDepthColor(), |
422 |
'maxDepthColor' => $categorizer->getMaxDepthColor() |
423 |
); |
424 |
} |
425 |
|
426 |
if (@file_put_contents($this->outputDirectory . DIRECTORY_SEPARATOR . self::dataFile, json_encode($data)) === false) |
427 |
{ |
428 |
throw new exceptions\runtime($this->locale->_('Unable to write in \'' . $this->outputDirectory . '\'')); |
429 |
} |
430 |
|
431 |
if ($this->onlyJsonFile === false) |
432 |
{ |
433 |
try |
434 |
{ |
435 |
$htmlDirectoryIterator = new \recursiveIteratorIterator(new atoum\iterators\filters\recursives\dot($this->htmlDirectory)); |
436 |
} |
437 |
catch (\exception $exception) |
438 |
{ |
439 |
throw new exceptions\runtime($this->locale->_('Directory \'' . $this->htmlDirectory . '\' does not exist')); |
440 |
} |
441 |
|
442 |
foreach ($htmlDirectoryIterator as $file) |
443 |
{ |
444 |
if (@copy($file, $this->outputDirectory . DIRECTORY_SEPARATOR . basename($file)) === false) |
445 |
{ |
446 |
throw new exceptions\runtime($this->locale->_('Unable to write in \'' . $this->outputDirectory . '\'')); |
447 |
} |
448 |
} |
449 |
} |
450 |
|
451 |
return $this; |
452 |
} |
453 |
} |