|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Webino (http://webino.sk/) |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/webino/WebinoDev/ for the canonical source repository |
|
6
|
|
|
* @copyright Copyright (c) 2014-2017 Webino, s. r. o. (http://webino.sk/) |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace WebinoDev\Di\Definition; |
|
11
|
|
|
|
|
12
|
|
|
use RecursiveDirectoryIterator; |
|
13
|
|
|
use RecursiveIteratorIterator; |
|
14
|
|
|
use RegexIterator; |
|
15
|
|
|
use Zend\Code\Scanner\FileScanner; |
|
16
|
|
|
use Zend\Di\Definition\CompilerDefinition; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Smart wrapper for Zend DI definition generator |
|
20
|
|
|
*/ |
|
21
|
|
|
class Generator |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $dir; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $ignore = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var CompilerDefinition |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $compiler; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $dir |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct($dir) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->dir = $dir; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return array |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function getIgnore() |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->ignore; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param array $ignore |
|
56
|
|
|
* @return self |
|
57
|
|
|
*/ |
|
58
|
|
|
public function setIgnore(array $ignore) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->ignore = $ignore; |
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return CompilerDefinition |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getCompiler() |
|
68
|
|
|
{ |
|
69
|
|
|
if (null === $this->compiler) { |
|
70
|
|
|
$this->setCompiler(new CompilerDefinition); |
|
71
|
|
|
} |
|
72
|
|
|
return $this->compiler; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param CompilerDefinition $compiler |
|
77
|
|
|
* @return self |
|
78
|
|
|
*/ |
|
79
|
|
|
public function setCompiler(CompilerDefinition $compiler) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->compiler = $compiler; |
|
82
|
|
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param array $files |
|
87
|
|
|
* @return self |
|
88
|
|
|
*/ |
|
89
|
|
|
public function compile(array $files = []) |
|
90
|
|
|
{ |
|
91
|
|
|
if (!is_dir($this->dir)) { |
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$compiler = $this->getCompiler(); |
|
96
|
|
|
$ignore = $this->getIgnore(); |
|
97
|
|
|
|
|
98
|
|
|
if (!empty($ignore)) { |
|
99
|
|
|
// add files, exclude ignored |
|
100
|
|
|
foreach ($this->createDirIterator($this->dir, $ignore) as $path) { |
|
101
|
|
|
$this->compiler->addCodeScannerFile(new FileScanner($path[0])); |
|
102
|
|
|
} |
|
103
|
|
|
} else { |
|
104
|
|
|
// add whole source dir |
|
105
|
|
|
$compiler->addDirectory($this->dir . '/../src'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
// add custom files |
|
109
|
|
|
foreach ($files as $file) { |
|
110
|
|
|
$compiler->addCodeScannerFile(new FileScanner($this->dir . '/../vendor/' . $file)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$compiler->compile(); |
|
114
|
|
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return self |
|
119
|
|
|
*/ |
|
120
|
|
|
public function dump() |
|
121
|
|
|
{ |
|
122
|
|
|
if (!is_dir($this->dir)) { |
|
123
|
|
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$target = $this->dir . '/../data/di/definition.php'; |
|
127
|
|
|
$dir = dirname($target); |
|
128
|
|
|
|
|
129
|
|
|
is_dir($dir) or mkdir($dir); |
|
130
|
|
|
|
|
131
|
|
|
$definition = $this->getCompiler()->toArrayDefinition()->toArray(); |
|
132
|
|
|
file_put_contents($target, '<?php ' . PHP_EOL . 'return ' . var_export($definition, true) . ';' . PHP_EOL); |
|
133
|
|
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param string $dir |
|
138
|
|
|
* @param array $ignore |
|
139
|
|
|
* @return RegexIterator |
|
140
|
|
|
*/ |
|
141
|
|
|
private function createDirIterator($dir, array $ignore = []) |
|
142
|
|
|
{ |
|
143
|
|
|
return new RegexIterator( |
|
144
|
|
|
new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir . '/../src')), |
|
145
|
|
|
'~^((?!' . join('|', $ignore) . ').)*' . preg_quote('.php') . '$~i', |
|
146
|
|
|
RegexIterator::GET_MATCH |
|
147
|
|
|
); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|