|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of Pomm's ModelManager package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) 2014 - 2015 Grégoire HUBERT <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace PommProject\ModelManager\Generator; |
|
11
|
|
|
|
|
12
|
|
|
use PommProject\Foundation\Inspector\Inspector; |
|
13
|
|
|
use PommProject\Foundation\ParameterHolder; |
|
14
|
|
|
use PommProject\ModelManager\Exception\GeneratorException; |
|
15
|
|
|
use PommProject\ModelManager\Session; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* BaseGenerator |
|
19
|
|
|
* |
|
20
|
|
|
* Base class for Generator |
|
21
|
|
|
* |
|
22
|
|
|
* @package ModelManager |
|
23
|
|
|
* @copyright 2014 - 2015 Grégoire HUBERT |
|
24
|
|
|
* @author Grégoire HUBERT |
|
25
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
|
26
|
|
|
* @abstract |
|
27
|
|
|
*/ |
|
28
|
|
|
abstract class BaseGenerator |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var Session |
|
32
|
|
|
*/ |
|
33
|
|
|
private $session; |
|
34
|
|
|
|
|
35
|
|
|
protected $schema; |
|
36
|
|
|
protected $relation; |
|
37
|
|
|
protected $filename; |
|
38
|
|
|
protected $namespace; |
|
39
|
|
|
protected $flexible_container; |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Constructor |
|
44
|
|
|
* |
|
45
|
|
|
* @access public |
|
46
|
|
|
* @param Session $session |
|
47
|
|
|
* @param string $schema |
|
48
|
|
|
* @param string $relation |
|
49
|
|
|
* @param string $filename |
|
50
|
|
|
* @param string $namespace |
|
51
|
|
|
* @param $flexible_container |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct(Session $session, $schema, $relation, $filename, $namespace, $flexible_container = null) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->session = $session; |
|
56
|
|
|
$this->schema = $schema; |
|
57
|
|
|
$this->relation = $relation; |
|
58
|
|
|
$this->filename = $filename; |
|
59
|
|
|
$this->namespace = $namespace; |
|
60
|
|
|
$this->flexible_container = $flexible_container; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* outputFileCreation |
|
65
|
|
|
* |
|
66
|
|
|
* Output what the generator will do. |
|
67
|
|
|
* |
|
68
|
|
|
* @access protected |
|
69
|
|
|
* @param array $output |
|
70
|
|
|
* @return BaseGenerator $this |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function outputFileCreation(array &$output) |
|
73
|
|
|
{ |
|
74
|
|
|
if (file_exists($this->filename)) { |
|
75
|
|
|
$output[] = ['status' => 'ok', 'operation' => 'overwriting', 'file' => $this->filename]; |
|
76
|
|
|
} else { |
|
77
|
|
|
$output[] = ['status' => 'ok', 'operation' => 'creating', 'file' => $this->filename]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* setSession |
|
85
|
|
|
* |
|
86
|
|
|
* Set the session. |
|
87
|
|
|
* |
|
88
|
|
|
* @access protected |
|
89
|
|
|
* @param Session $session |
|
90
|
|
|
* @return BaseGenerator $this |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function setSession(Session $session) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->session = $session; |
|
95
|
|
|
|
|
96
|
|
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* getSession |
|
101
|
|
|
* |
|
102
|
|
|
* Return the session is set. Throw an exception otherwise. |
|
103
|
|
|
* |
|
104
|
|
|
* @access protected |
|
105
|
|
|
* @throws GeneratorException |
|
106
|
|
|
* @return Session |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function getSession() |
|
109
|
|
|
{ |
|
110
|
|
|
if ($this->session === null) { |
|
111
|
|
|
throw new GeneratorException(sprintf("Session is not set.")); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $this->session; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* getInspector |
|
119
|
|
|
* |
|
120
|
|
|
* Shortcut to session's inspector client. |
|
121
|
|
|
* |
|
122
|
|
|
* @access protected |
|
123
|
|
|
* @return Inspector |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function getInspector() |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->getSession()->getClientUsingPooler('inspector', null); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* generate |
|
132
|
|
|
* |
|
133
|
|
|
* Called to generate the file. |
|
134
|
|
|
* Possible options are: |
|
135
|
|
|
* * force: true if files can be overwritten, false otherwise |
|
136
|
|
|
* |
|
137
|
|
|
* @access public |
|
138
|
|
|
* @param ParameterHolder $input |
|
139
|
|
|
* @param array $output |
|
140
|
|
|
* @throws GeneratorException |
|
141
|
|
|
* @return array $output |
|
142
|
|
|
*/ |
|
143
|
|
|
abstract public function generate(ParameterHolder $input, array $output = []); |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* getCodeTemplate |
|
147
|
|
|
* |
|
148
|
|
|
* Return the code template for files to be generated. |
|
149
|
|
|
* |
|
150
|
|
|
* @access protected |
|
151
|
|
|
* @return string |
|
152
|
|
|
*/ |
|
153
|
|
|
abstract protected function getCodeTemplate(); |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* mergeTemplate |
|
157
|
|
|
* |
|
158
|
|
|
* Merge templates with given values. |
|
159
|
|
|
* |
|
160
|
|
|
* @access protected |
|
161
|
|
|
* @param array $variables |
|
162
|
|
|
* @return string |
|
163
|
|
|
*/ |
|
164
|
|
|
protected function mergeTemplate(array $variables) |
|
165
|
|
|
{ |
|
166
|
|
|
$prepared_variables = []; |
|
167
|
|
|
foreach ($variables as $name => $value) { |
|
168
|
|
|
$prepared_variables[sprintf("{:%s:}", $name)] = $value; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
return strtr( |
|
172
|
|
|
$this->getCodeTemplate(), |
|
173
|
|
|
$prepared_variables |
|
174
|
|
|
); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* saveFile |
|
179
|
|
|
* |
|
180
|
|
|
* Write the generated content to a file. |
|
181
|
|
|
* |
|
182
|
|
|
* @access protected |
|
183
|
|
|
* @param string $filename |
|
184
|
|
|
* @param string $content |
|
185
|
|
|
* @throws GeneratorException |
|
186
|
|
|
* @return BaseGenerator $this |
|
187
|
|
|
*/ |
|
188
|
|
|
protected function saveFile($filename, $content) |
|
189
|
|
|
{ |
|
190
|
|
|
if (!file_exists(dirname($filename)) |
|
191
|
|
|
&& mkdir(dirname($filename), 0777, true) === false |
|
192
|
|
|
) { |
|
193
|
|
|
throw new GeneratorException( |
|
194
|
|
|
sprintf( |
|
195
|
|
|
"Could not create directory '%s'.", |
|
196
|
|
|
dirname($filename) |
|
197
|
|
|
) |
|
198
|
|
|
); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
if (file_put_contents($filename, $content) === false) { |
|
202
|
|
|
throw new GeneratorException( |
|
203
|
|
|
sprintf( |
|
204
|
|
|
"Could not open '%s' for writing.", |
|
205
|
|
|
$filename |
|
206
|
|
|
) |
|
207
|
|
|
); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
return $this; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* checkOverwrite |
|
215
|
|
|
* |
|
216
|
|
|
* Check if the file exists and if it the write is forced. |
|
217
|
|
|
* |
|
218
|
|
|
* @access protected |
|
219
|
|
|
* @param ParameterHolder $input |
|
220
|
|
|
* @throws GeneratorException |
|
221
|
|
|
* @return BaseGenerator $this |
|
222
|
|
|
*/ |
|
223
|
|
|
protected function checkOverwrite(ParameterHolder $input) |
|
224
|
|
|
{ |
|
225
|
|
|
if (file_exists($this->filename) && $input->getParameter('force') !== true) { |
|
226
|
|
|
throw new GeneratorException(sprintf("Cannot overwrite file '%s' without --force option.", $this->filename)); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
return $this; |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|