planetconquest is an open source project powered by Assembla

Assembla offers free public and private SVN/Git repositories and project hosting with bug/issue tracking and collaboration tools.

planetconquest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
 <?php


/**
 * Cette classe permet la génération des instanciators
 * - parcours récursif
 * - évite les abstract (TODO)
 * - génčre les instanciators pour chaque DTO (TODO)
 * - gčre un fichier de metadata (cache ? TODO)
 * - suppression des instanciators inutiles (TODO)
 * - option pour tout regénérer (TODO)
 *
 * RESERVE LE REPERTOIRE MODELS/INSTANCIATORS
 * NECESSITE LE ZF (ou un autoload similaire)
 *
 * @author Fab
 *
 */
class CodeGenerator_InstanciatorGenerator extends CodeGenerator_Abstract {
    const INSTANCIATOR_BASE_PREFIX = "Instanciator";
    const PREFIX_SEPARATOR = "_";

    protected $modelsDirectory;


    /**
     * Constructeur du generateur d'instanciator
     *
     * @param String $modelsDirectory Fichier de cache comportant les metadonnées de la génération
     */
    public function CodeGenerator_InstanciatorGenerator($modelsDirectory){
        $this->modelsDirectory = $modelsDirectory;

        // Chargement des metadonnées déjŕ existantes (TODO)

        // OU creation d'un nouvel objet
    }

    /**
     * Génération des instanciateurs de toutes les DTO
     *  (création des instanciators dans modelsDirectory/Instanciator
     *  DTO sousrep
     *
     * @param String $modelsDirectory Repertoire racine des classes metiers
     * @param String $dtoPrefix Emplacement dans le modele des DTO
     * @param String $instanciatorPrefix Prefixe dans le nom des instanciator
     *
     */
    public function generateAll($dtoPrefix, $instanciatorPrefix){
        //  TODO ERREURS syntaxe ?

        // Lancement de la récupération des beans
        $dtoDirectory = $this->modelsDirectory . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR,explode("_",$dtoPrefix)) . DIRECTORY_SEPARATOR;
        $this->processDirectory($dtoDirectory,$dtoPrefix,$instanciatorPrefix);

        // Génération des instanciators a partir de l'objet de metadata ou genération ŕ la volée ? TODO

        // Génération fichier de metadata --> prévoir une const avec un repertoire "ressources" et une pour le fichier TODO
    }

    // public function generateOne() TODO utile ?

    /**
     * Traitement d'un répertoire de DTO et de ses sous répertoires, pour génération des instanciators
     *
     * @param unknown_type $dtoDirectory Repertoire de DTO a traiter
     * @param unknown_type $dtoPrefix Prefixe courant des DTO (prise en compte de l'arborescence)
     * @param unknown_type $instanciatorPrefix Prefixe courant de l'instanciator (prise en compte de l'arborescence)
     */
    protected function processDirectory($dtoDirectory,$dtoPrefix, $instanciatorPrefix){
        // Parcours du repertoire des DTO
        $dir = opendir($dtoDirectory);
        while($file =  readdir($dir)){
            if($file == '.' || $file == '..' || $file == '.svn') {
                continue;
            }

            // Si c'est un repertoire, on relance le traitement du sous repertoire
            if(is_dir($dtoDirectory . $file)) {
                $newDtoDirectory = $dtoDirectory . $file;
                $newDtoPrefix = $dtoPrefix  . self::PREFIX_SEPARATOR . $file;
                $newInstanciatorPrefix = $instanciatorPrefix . self::PREFIX_SEPARATOR . $file;

                $this->processDirectory($newDtoDirectory,$newDtoPrefix,$newInstanciatorPrefix);
            }
            // Sinon on traite la classe correspondante
            else {
                $tabFileName = (explode(".",$file));
                $className = $dtoPrefix .  self::PREFIX_SEPARATOR . $tabFileName[0];
                $instanciatorName = $instanciatorPrefix . self::PREFIX_SEPARATOR . $tabFileName[0];
                $this->processDTO($className, $instanciatorName);
            }
        }

    }

    /**
     * Génération de l'instanciator du bean correspondant
     *
     * @param unknown_type $sourceFile
     * @param unknown_type $instanciatorName
     */
    protected function processDTO($className, $instanciatorName){
    // Récupération du DTO a instancier
        $classDTO = new Zend_Reflection_Class($className);

        // Pas d'instances pour les abstracts
        if($classDTO->isAbstract()){
            return;
        }

        // Récupération de l'instanciator si il existe
        try{
            $classInstanciator = Zend_CodeGenerator_Php_Class::fromReflection(new Zend_Reflection_Class($instanciatorName));
        }
        catch(Exception $e) {
            $classInstanciator = new Zend_CodeGenerator_Php_Class();
            $classInstanciator->setName($instanciatorName);
            //$class->setExtendedClass("Service_Abstract"); //pour extends
        }



        // Création de la méthode d'instanciation
        $method = new Zend_CodeGenerator_Php_Method();
        $method->setName("setAllData");

        // Tableau de données en entrée
        $param = new Zend_CodeGenerator_Php_Parameter();
        $param->setName("data");
        $method->setParameter($param);

        //Génération du code d'instanciation
        $codeInstanciation = '$dto = new '.$className.'();'. PHP_EOL;

        $properties = $classDTO->getProperties();
        foreach($properties as $property){
            $codeInstanciation .= '$dto->set'.$property->getName().'($data[\''.$property->getName().'\']);'. PHP_EOL;
        }


        $codeInstanciation .= 'return $dto;';

        $method->setBody($codeInstanciation);
        $classInstanciator->setMethod($method);


        $this->displayGeneratedCode($classInstanciator);
        /*
        // DISPLAY CODE
        echo "CODE GENERE <br/>";
        echo "className = $className<br/>";


        echo $classInstanciator->generate();
        echo "<br/><br/><br/>";
        */



        /**

        if (!$class->hasMethod("setManager")) {
                $method = new Zend_CodeGenerator_Php_Method();
                $method->setName("setManager");
                $param = new Zend_CodeGenerator_Php_Parameter();
                $param->setName("manager");
                $method->setParameter($param);
                $method->setBody('$this->manager = $manager;' . PHP_EOL . 'return $this;');
                $docblock = new Zend_CodeGenerator_Php_Docblock();
                $method->setDocblock($docblock);
                $tag = new Zend_CodeGenerator_Php_Docblock_Tag_Param();
                $tag->setDatatype("Manager_" . $className);
                $tag->setParamName("manager");
                $docblock->setTag($tag);

                $tag = new Zend_CodeGenerator_Php_Docblock_Tag_Return();
                $tag->setDatatype("Service_" . $className);
                $docblock->setTag($tag);
                $class->setMethod($method);
        }

        if (!$class->hasMethod("getManager")) {
            $method = new Zend_CodeGenerator_Php_Method();
            $method->setName("getManager");
            $method->setBody('return $this->manager;');
            $docblock = new Zend_CodeGenerator_Php_Docblock();
            $method->setDocblock($docblock);

            $tag = new Zend_CodeGenerator_Php_Docblock_Tag_Return();
            $tag->setDatatype("Manager_" . $className);
            $docblock->setTag($tag);

            $class->setMethod($method);
        }

        if (!$class->hasMethod("getDTOForCreation")) {
            $method = new Zend_CodeGenerator_Php_Method();
            $method->setName("getDTOForCreation");
            $method->setBody('return $this->getManager()->getDTOForCreation();');
            $docblock = new Zend_CodeGenerator_Php_Docblock();
            $method->setDocblock($docblock);

            $tag = new Zend_CodeGenerator_Php_Docblock_Tag_Return();
            $tag->setDatatype("DTO_" . $className);
            $docblock->setTag($tag);

            $class->setMethod($method);
        }

        if (!$class->hasMethod("getAll")) {
            $method = new Zend_CodeGenerator_Php_Method();
            $method->setName("getAll");
            $method->setBody('return $this->getManager()->getAll();');
            $docblock = new Zend_CodeGenerator_Php_Docblock();
            $method->setDocblock($docblock);

            $tag = new Zend_CodeGenerator_Php_Docblock_Tag_Return();
            $tag->setDatatype("DTO_" . $className . '[]');
            $docblock->setTag($tag);

            $class->setMethod($method);
        }

        if (!$class->hasMethod("getById")) {
            $method = new Zend_CodeGenerator_Php_Method();
            $method->setName("getById");

            $param = new Zend_CodeGenerator_Php_Parameter();
            $param->setName("id");
            $method->setParameter($param);

            $method->setBody('return $this->getManager()->getById($id);');
            $docblock = new Zend_CodeGenerator_Php_Docblock();
            $method->setDocblock($docblock);

            $tag = new Zend_CodeGenerator_Php_Docblock_Tag_Param();
            $tag->setDatatype("int");
            $tag->setParamName("id");
            $docblock->setTag($tag);

            $tag = new Zend_CodeGenerator_Php_Docblock_Tag_Return();
            $tag->setDatatype("DTO_" . $className);
            $docblock->setTag($tag);

            $class->setMethod($method);
        }

        if (!$class->hasMethod("create")) {
            $method = new Zend_CodeGenerator_Php_Method();
            $method->setName("create");

            $param = new Zend_CodeGenerator_Php_Parameter();
            $param->setName("obj");
            $method->setParameter($param);

            $method->setBody('$this->getManager()->create($obj);');
            $docblock = new Zend_CodeGenerator_Php_Docblock();
            $method->setDocblock($docblock);

            $tag = new Zend_CodeGenerator_Php_Docblock_Tag_Param();
            $tag->setDatatype("DTO_" . $className);
            $tag->setParamName("obj");
            $docblock->setTag($tag);

            $class->setMethod($method);
        }

        if (!$class->hasMethod("update")) {
            $method = new Zend_CodeGenerator_Php_Method();
            $method->setName("update");

            $param = new Zend_CodeGenerator_Php_Parameter();
            $param->setName("obj");
            $method->setParameter($param);

            $method->setBody('$this->getManager()->update($obj);');
            $docblock = new Zend_CodeGenerator_Php_Docblock();
            $method->setDocblock($docblock);

            $tag = new Zend_CodeGenerator_Php_Docblock_Tag_Param();
            $tag->setDatatype("DTO_" . $className);
            $tag->setParamName("obj");
            $docblock->setTag($tag);

            $class->setMethod($method);
        }

        if (!$class->hasMethod("delete")) {
            $method = new Zend_CodeGenerator_Php_Method();
            $method->setName("delete");

            $param = new Zend_CodeGenerator_Php_Parameter();
            $param->setName("id");
            $method->setParameter($param);

            $method->setBody('$this->getManager()->delete($id);');
            $docblock = new Zend_CodeGenerator_Php_Docblock();
            $method->setDocblock($docblock);

            $tag = new Zend_CodeGenerator_Php_Docblock_Tag_Param();
            $tag->setDatatype("int");
            $tag->setParamName("id");
            $docblock->setTag($tag);

            $class->setMethod($method);
        }


        echo $class->generate();

        $file = new Zend_CodeGenerator_Php_File();
        $file->setClass($class);

        $sDir = "../application/models/Service/";

        file_put_contents($sDir . $className . ".php", $file->generate());
         */

    }

}
Ajax-loader Loading, please wait...