1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author stev leibelt <[email protected]> |
4
|
|
|
* @since 2015-07-11 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace NetBazzlineZfCliGenerator\Service\ProcessPipe\Transformer; |
8
|
|
|
|
9
|
|
|
use Net\Bazzline\Component\ProcessPipe\ExecutableException; |
10
|
|
|
use Net\Bazzline\Component\ProcessPipe\ExecutableInterface; |
11
|
|
|
|
12
|
|
|
class ParseToConfiguration implements ExecutableInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param mixed $input |
16
|
|
|
* @return mixed |
17
|
|
|
* @throws ExecutableException |
18
|
|
|
*/ |
19
|
|
|
public function execute($input = null) |
20
|
|
|
{ |
21
|
|
|
if (!is_array($input)) { |
22
|
|
|
throw new ExecutableException( |
23
|
|
|
'input must be an array' |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
if (empty($input)) { |
28
|
|
|
throw new ExecutableException( |
29
|
|
|
'empty input provided' |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$output = array(); |
34
|
|
|
|
35
|
|
|
foreach ($input as $line) { |
36
|
|
|
//remove description |
37
|
|
|
$breadcrumb = array(); |
38
|
|
|
$positionOfMultipleWhitespaces = strpos($line, ' '); |
39
|
|
|
|
40
|
|
|
if (is_numeric($positionOfMultipleWhitespaces)) { |
41
|
|
|
$line = substr($line, 0, $positionOfMultipleWhitespaces); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$tokens = explode(' ', $line); |
45
|
|
|
|
46
|
|
|
foreach ($tokens as $token) { |
47
|
|
|
$isValid = true; |
48
|
|
|
//we don't care of "--foo", "-f", <foo>, [<foo>] nor [foo] |
49
|
|
|
// @see: |
50
|
|
|
// http://framework.zend.com/manual/current/en/modules/zend.console.routes.html |
51
|
|
|
// http://framework.zend.com/manual/current/en/modules/zend.console.routes.html#console-routes-cheat-sheet |
52
|
|
|
foreach (array('-', '<', '[') as $needle) { |
53
|
|
|
if ($this->startsWith($token, $needle)) { |
54
|
|
|
$isValid = false; |
55
|
|
|
break; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($isValid) { |
60
|
|
|
$breadcrumb[] = $token; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
$output = $this->addToArray($output, $breadcrumb); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $output; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param array $array |
71
|
|
|
* @param array $path |
72
|
|
|
* @return array |
73
|
|
|
* @todo replace with external dependency |
74
|
|
|
*/ |
75
|
|
|
private function addToArray(array $array, array $path) |
76
|
|
|
{ |
77
|
|
|
$section = array_shift($path); |
78
|
|
|
|
79
|
|
|
if (!isset($array[$section])) { |
80
|
|
|
$array[$section]= array(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (!empty($path)) { |
84
|
|
|
$array[$section] = $this->addToArray($array[$section], $path); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $array; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $haystack |
92
|
|
|
* @param string $needle |
93
|
|
|
* @return bool |
94
|
|
|
* @todo replace with external dependency |
95
|
|
|
*/ |
96
|
|
|
private function startsWith($haystack, $needle) |
97
|
|
|
{ |
98
|
|
|
return (strncmp($haystack, $needle, strlen($needle)) === 0); |
99
|
|
|
} |
100
|
|
|
} |