|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Classes en rapport avec la génération automatique d'une autre classe |
|
4
|
|
|
* @author Vermeulen Maxime <[email protected]> |
|
5
|
|
|
* @version 1.0 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace BFW; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Classe générant une autre classe. |
|
12
|
|
|
* @package bfw |
|
13
|
|
|
*/ |
|
14
|
|
|
class CreateClasse implements \BFWInterface\ICreateClasse |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var $_kernel L'instance du Kernel |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $_kernel; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var $nom Le nom de la classe |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $nom = ''; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var $indente Le ou les caractère(s) mit pour indenté |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $indente = ' '; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var $extends Depuis quelle classe on hérite |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $extends = ''; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var $implements Liste les interfaces de la classe |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $implements = array(); |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var $ attributs Liste tous les attributs de la futur classe |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $attributs = array(); |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var $attributs_porter La portée de tous les attributs (public/private/protected) |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $attributs_porter = array(); |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var $attributs_option Les options passé à la méthode de création d'attribut pour chaque attribut |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $attributs_option = array(); |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var $methode Liste toutes les méthodes qui sont à créer |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $methode = array(); |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var $methode_porter La portée de toutes les méthodes (public/private/protected) |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $methode_porter = array(); |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var $get La liste de tous les accesseur get à faire |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $get = array(); |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @var $set La liste de tous les accesseur set à faire |
|
73
|
|
|
*/ |
|
74
|
|
|
protected $set = array(); |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @var $file Le contenu de la futur classe |
|
78
|
|
|
*/ |
|
79
|
|
|
protected $file = ''; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @var $methode_create La liste de toutes les méthodes créé (pour éviter d'en créer en double à cause des get et set) |
|
83
|
|
|
*/ |
|
84
|
|
|
protected $methode_create = array(); |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Constructeur |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $nom Le nom de la futur classe |
|
91
|
|
|
* @param array $options Les options de la classe |
|
92
|
|
|
*/ |
|
93
|
|
|
public function __construct($nom, $options=array()) |
|
94
|
|
|
{ |
|
95
|
1 |
|
$this->_kernel = getKernel(); |
|
96
|
|
|
|
|
97
|
1 |
|
$this->nom = $nom; |
|
98
|
|
|
|
|
99
|
1 |
|
if(isset($options['indente'])) |
|
100
|
1 |
|
{ |
|
101
|
1 |
|
$this->indente = $options['indente']; |
|
102
|
1 |
|
} |
|
103
|
|
|
|
|
104
|
1 |
|
if(isset($options['extends'])) |
|
105
|
1 |
|
{ |
|
106
|
1 |
|
$this->extends = $options['extends']; |
|
107
|
1 |
|
} |
|
108
|
|
|
|
|
109
|
1 |
|
if(isset($options['implements'])) |
|
110
|
1 |
|
{ |
|
111
|
1 |
|
$this->implements = $options['implements']; |
|
112
|
1 |
|
} |
|
113
|
1 |
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Retourne le contenu de la futur classe |
|
117
|
|
|
* |
|
118
|
|
|
* @return string La futur classe |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getFile() |
|
121
|
|
|
{ |
|
122
|
1 |
|
return $this->file; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Créer un attribut à la nouvelle classe |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $nom Le nom de l'attribut |
|
129
|
|
|
* @param array $opt (default: array()) Les options de l'attribut : |
|
130
|
|
|
* - string porter : La porté de l'attribut. Par défaut à "protected" |
|
131
|
|
|
* - bool get : Si un get doit être créé. Par défaut à true |
|
132
|
|
|
* - bool set : Si un set doit être créé. Par défaut à true |
|
133
|
|
|
* - string type : Le type de l'attribut. Par défaut aucun type prédéfini. |
|
134
|
|
|
* - mixed default : Valeur par défaut de l'attribut. |
|
135
|
|
|
* - bool default_string : Permet d'indiqué que la valeur par défaut est de type string (met des ' autour.) |
|
136
|
|
|
* |
|
137
|
|
|
* @TODO : Enlever default_string et repérer dynamiquement le type de la valeur. |
|
138
|
|
|
* |
|
139
|
|
|
* @return bool True si réussi, False si existe déjà. |
|
140
|
|
|
*/ |
|
141
|
|
|
public function createAttribut($nom, $opt=array()) |
|
142
|
|
|
{ |
|
143
|
1 |
|
if(in_array($nom, $this->attributs)) {return false;} |
|
144
|
|
|
|
|
145
|
1 |
|
if(!isset($opt['porter'])) |
|
146
|
1 |
|
{ |
|
147
|
1 |
|
$opt['porter'] = 'protected'; |
|
148
|
1 |
|
} |
|
149
|
|
|
|
|
150
|
1 |
|
if(!isset($opt['get'])) |
|
151
|
1 |
|
{ |
|
152
|
1 |
|
$opt['get'] = 1; |
|
153
|
1 |
|
} |
|
154
|
|
|
|
|
155
|
1 |
|
if(!isset($opt['set'])) |
|
156
|
1 |
|
{ |
|
157
|
1 |
|
$opt['set'] = 1; |
|
158
|
1 |
|
} |
|
159
|
|
|
|
|
160
|
1 |
|
$this->attributs[] = $nom; |
|
161
|
1 |
|
$this->attributs_porter[] = $opt['porter']; |
|
162
|
1 |
|
$this->attributs_option[] = $opt; |
|
163
|
|
|
|
|
164
|
1 |
|
if($opt['get'] == 1) |
|
165
|
1 |
|
{ |
|
166
|
1 |
|
$this->get[] = $nom; |
|
167
|
1 |
|
} |
|
168
|
|
|
|
|
169
|
1 |
|
if($opt['set'] == 1) |
|
170
|
1 |
|
{ |
|
171
|
1 |
|
$this->set[] = $nom; |
|
172
|
1 |
|
} |
|
173
|
|
|
|
|
174
|
1 |
|
return true; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Créer une nouvelle méthode pour la classe |
|
179
|
|
|
* |
|
180
|
|
|
* @todo Gestion des arguments pour la méthode |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $nom Le nom de la méthode |
|
183
|
|
|
* @param string $porter La porté de la méthode. Par défaut private. |
|
184
|
|
|
* |
|
185
|
|
|
* @return bool |
|
186
|
|
|
*/ |
|
187
|
|
|
public function createMethode($nom, $porter='protected') |
|
188
|
|
|
{ |
|
189
|
1 |
|
if(in_array($nom, $this->methode)) {return false;} |
|
190
|
|
|
|
|
191
|
1 |
|
$this->methode[] = $nom; |
|
192
|
1 |
|
$this->methode_porter[] = $porter; |
|
193
|
|
|
|
|
194
|
1 |
|
return true; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Créer un attribut dans la futur classe. |
|
199
|
|
|
* |
|
200
|
|
|
* @param int $key La clé de l'attribut à créer (tableau $this->attributs) |
|
201
|
|
|
*/ |
|
202
|
|
|
protected function genereAttribut($key) |
|
203
|
|
|
{ |
|
204
|
1 |
|
$javadoc = $code = ''; |
|
205
|
1 |
|
$default = isset($this->attributs_option[$key]['default']) ? true : false; |
|
206
|
|
|
|
|
207
|
1 |
|
$default_string = false; |
|
208
|
|
|
if( |
|
209
|
1 |
|
isset($this->attributs_option[$key]['default_string']) && |
|
210
|
1 |
|
$this->attributs_option[$key]['default_string'] == true |
|
211
|
1 |
|
) |
|
212
|
1 |
|
{ |
|
213
|
1 |
|
$default_string = true; |
|
214
|
1 |
|
} |
|
215
|
|
|
|
|
216
|
|
|
|
|
217
|
1 |
|
$javadoc .= $this->indente."/**\n"; |
|
218
|
1 |
|
$javadoc .= $this->indente.' * @var '; |
|
219
|
1 |
|
$javadoc .= '$'.$this->attributs[$key].' : Ma description.'; |
|
220
|
|
|
|
|
221
|
1 |
|
$code .= $this->indente; |
|
222
|
|
|
//Si un type à été déclaré |
|
223
|
1 |
|
$code .= (isset($this->attributs_option[$key]['type'])) ? '('.$this->attributs_option[$key]['type'].') ' : ''; |
|
224
|
1 |
|
$code .= $this->attributs_porter[$key].' $'.$this->attributs[$key]; |
|
225
|
|
|
|
|
226
|
|
|
//S'il y a une valeur par défaut |
|
227
|
1 |
|
if($default === true) |
|
228
|
1 |
|
{ |
|
229
|
1 |
|
$javadoc .= ' Par défaut à '; |
|
230
|
1 |
|
$code .= ' = '; |
|
231
|
|
|
|
|
232
|
|
|
//Si la valeur par défaut est dite un string. On ajoute des ' |
|
233
|
1 |
|
if($default_string === true) |
|
234
|
1 |
|
{ |
|
235
|
1 |
|
$javadoc .= '\''; |
|
236
|
1 |
|
$code .= '\''; |
|
237
|
1 |
|
} |
|
238
|
|
|
|
|
239
|
1 |
|
$javadoc .= $this->attributs_option[$key]['default']; |
|
240
|
1 |
|
$code .= $this->attributs_option[$key]['default']; |
|
241
|
|
|
|
|
242
|
|
|
//Si la valeur par défaut est dite un string. On ajoute des ' |
|
243
|
1 |
|
if($default_string === true) |
|
244
|
1 |
|
{ |
|
245
|
1 |
|
$javadoc .= '\''; |
|
246
|
1 |
|
$code .= '\''; |
|
247
|
1 |
|
} |
|
248
|
|
|
|
|
249
|
1 |
|
$javadoc .= '.'; |
|
250
|
1 |
|
} |
|
251
|
|
|
|
|
252
|
1 |
|
$javadoc .= "\n"; |
|
253
|
1 |
|
$javadoc .= $this->indente." */\n"; |
|
254
|
|
|
|
|
255
|
1 |
|
$this->file .= $javadoc.$code.";\n\n"; |
|
256
|
1 |
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Créer un accesseur get |
|
260
|
|
|
* |
|
261
|
|
|
* @param int $key La clé de la méthode à créer (tableau $this->get) |
|
262
|
|
|
*/ |
|
263
|
|
|
protected function genereGet($key) |
|
264
|
|
|
{ |
|
265
|
1 |
|
$nom = $this->get[$key]; |
|
266
|
|
|
|
|
267
|
1 |
|
$this->file .= $this->indente."/**\n"; |
|
268
|
1 |
|
$this->file .= $this->indente.' * Accesseur get vers '.$nom."\n".$this->indente." *\n"; |
|
269
|
1 |
|
$this->file .= $this->indente.' * @return mixed : La valeur de '.$nom."\n"; |
|
270
|
1 |
|
$this->file .= $this->indente." */\n"; |
|
271
|
|
|
|
|
272
|
1 |
|
$this->file .= $this->indente.'public function get_'.$nom.'() {return $this->'.$nom.';}'."\n\n"; |
|
273
|
1 |
|
$this->methode_create[] = 'get_'.$nom."\n"; |
|
274
|
1 |
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Créer un accesseur set |
|
278
|
|
|
* |
|
279
|
|
|
* @param int $key La clé de la méthode à créer (tableau $this->get) |
|
280
|
|
|
*/ |
|
281
|
|
|
protected function genereSet($key) |
|
282
|
|
|
{ |
|
283
|
1 |
|
$nom = $this->set[$key]; |
|
284
|
1 |
|
if(!in_array('set_'.$nom, $this->methode_create)) |
|
285
|
1 |
|
{ |
|
286
|
1 |
|
$this->file .= $this->indente."/**\n"; |
|
287
|
1 |
|
$this->file .= $this->indente.' * Accesseur set vers '.$nom."\n".$this->indente." *\n"; |
|
288
|
1 |
|
$this->file .= $this->indente.' * @param mixed : La nouvelle valeur de '.$nom."\n".$this->indente." *\n"; |
|
289
|
1 |
|
$this->file .= $this->indente." * @return bool : True si réussi, False sinon.\n"; |
|
290
|
1 |
|
$this->file .= $this->indente." */\n"; |
|
291
|
|
|
|
|
292
|
1 |
|
$this->file .= $this->indente.'public function set_'.$nom.'($data) '; |
|
293
|
1 |
|
$this->file .= '{return ($this->'.$nom.' = $data) ? true : false;}'."\n\n"; |
|
294
|
|
|
|
|
295
|
1 |
|
$this->methode_create[] = 'set_'.$nom; |
|
296
|
1 |
|
} |
|
297
|
1 |
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* Créer une méthode |
|
301
|
|
|
* |
|
302
|
|
|
* @param int $key La clé de la méthode à créer (tableau $this->méthode) |
|
303
|
|
|
* |
|
304
|
|
|
* @return void |
|
305
|
|
|
*/ |
|
306
|
|
|
protected function genereMethode($key) |
|
307
|
|
|
{ |
|
308
|
1 |
|
if(in_array($this->methode[$key], $this->methode_create)) {return;} |
|
309
|
|
|
|
|
310
|
1 |
|
$this->file .= $this->indente."/**\n"; |
|
311
|
1 |
|
$this->file .= $this->indente." * Description de ma méthode.\n"; |
|
312
|
1 |
|
$this->file .= $this->indente." */\n"; |
|
313
|
|
|
|
|
314
|
1 |
|
$this->file .= $this->indente.$this->methode_porter[$key].' function '.$this->methode[$key].'() {}'."\n"; |
|
315
|
1 |
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* Lance la génération de la classe. |
|
319
|
|
|
* |
|
320
|
|
|
* @return string La classe généré |
|
321
|
|
|
*/ |
|
322
|
|
|
public function genere() |
|
323
|
|
|
{ |
|
324
|
|
|
//Création de la classe |
|
325
|
1 |
|
$this->file = "<?php\n"; |
|
326
|
1 |
|
$this->file .= "/**\n * Ma description du fichier\n * @author me\n * @version 1.0\n */\n\n"; |
|
327
|
1 |
|
$this->file .= "/**\n * La description de ma classe\n * @package MonProjet\n */\n"; |
|
328
|
|
|
|
|
329
|
1 |
|
$this->file .= 'class '.$this->nom; |
|
330
|
1 |
|
if(!empty($this->extends)) |
|
331
|
1 |
|
{ |
|
332
|
1 |
|
$this->file .= ' extends '.$this->extends; |
|
333
|
1 |
|
} |
|
334
|
|
|
|
|
335
|
1 |
|
if(count($this->implements) > 0) |
|
336
|
1 |
|
{ |
|
337
|
1 |
|
$this->file .= ' implements '; |
|
338
|
1 |
|
foreach($this->implements as $key => $implement) |
|
339
|
|
|
{ |
|
340
|
1 |
|
$this->file .= ($key > 0) ? ', ' : ''; |
|
341
|
1 |
|
$this->file .= $implement; |
|
342
|
1 |
|
} |
|
343
|
1 |
|
} |
|
344
|
1 |
|
$this->file .= "\n{\n"; |
|
345
|
|
|
|
|
346
|
|
|
//Création des attributs |
|
347
|
1 |
|
foreach($this->attributs as $key => $attr) |
|
348
|
|
|
{ |
|
349
|
1 |
|
$this->genereAttribut($key); |
|
350
|
1 |
|
} |
|
351
|
|
|
|
|
352
|
|
|
//Le constructeur |
|
353
|
1 |
|
$this->file .= "\n"; |
|
354
|
1 |
|
$this->file .= $this->indente."/**\n"; |
|
355
|
1 |
|
$this->file .= $this->indente." * Constructeur de la classe\n"; |
|
356
|
1 |
|
$this->file .= $this->indente." */\n"; |
|
357
|
1 |
|
$this->file .= $this->indente."public function __construct() {}\n\n"; |
|
358
|
|
|
|
|
359
|
|
|
//Les gets |
|
360
|
1 |
|
foreach($this->get as $key => $nom) |
|
361
|
|
|
{ |
|
362
|
1 |
|
$this->genereGet($key); |
|
363
|
1 |
|
if(in_array($nom, $this->set)) |
|
364
|
1 |
|
{ |
|
365
|
1 |
|
$this->genereSet($key); |
|
366
|
1 |
|
} |
|
367
|
1 |
|
} |
|
368
|
|
|
|
|
369
|
1 |
|
foreach($this->set as $key => $nom) |
|
370
|
|
|
{ |
|
371
|
1 |
|
$this->genereSet($key); |
|
372
|
1 |
|
} |
|
373
|
|
|
|
|
374
|
1 |
|
foreach($this->methode as $key => $nom) |
|
375
|
|
|
{ |
|
376
|
1 |
|
$this->genereMethode($key); |
|
377
|
1 |
|
} |
|
378
|
|
|
|
|
379
|
1 |
|
$this->file .= "}\n?>"; |
|
380
|
1 |
|
return $this->file; |
|
381
|
|
|
} |
|
382
|
|
|
} |
|
383
|
|
|
|