Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

GeneratorBundle/Generator/DefaultSiteGenerator.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\GeneratorBundle\Generator;
4
5
use Kunstmaan\GeneratorBundle\Helper\GeneratorUtils;
6
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
7
use Symfony\Component\Yaml\Yaml;
8
9
/**
10
 * Generates a default website using several Kunstmaan bundles using default templates and assets
11
 */
12
class DefaultSiteGenerator extends KunstmaanGenerator
13
{
14
    /**
15
     * @var BundleInterface
16
     */
17
    private $bundle;
18
19
    /**
20
     * @var string
21
     */
22
    private $prefix;
23
24
    /**
25
     * @var string
26
     */
27
    private $rootDir;
28
29
    /**
30
     * @var bool
31
     */
32
    private $demosite;
33
34
    /**
35
     * Generate the website.
36
     *
37
     * @param BundleInterface $bundle
38
     * @param string $prefix
39
     * @param string $rootDir
40
     * @param bool $demosite
41
     */
42
    public function generate(BundleInterface $bundle, $prefix, $rootDir, $demosite = false)
43
    {
44
        $this->bundle = $bundle;
45
        $this->prefix = GeneratorUtils::cleanPrefix($prefix);
46
        $this->rootDir = $rootDir;
47
        $this->demosite = $demosite;
48
49
        $parameters = array(
50
            'namespace'     => $this->bundle->getNamespace(),
51
            'bundle'        => $this->bundle,
52
            'bundle_name'   => $this->bundle->getName(),
53
            'prefix'        => $this->prefix,
54
            'demosite'      => $this->demosite,
55
            'multilanguage' => $this->isMultiLangEnvironment(),
56
        );
57
58
        $this->generateControllers($parameters);
59
        $this->generateAdminLists($parameters);
60
        $this->generateEntities($parameters);
61
        $this->generateFormTypes($parameters);
62
        $this->generateTwigExtensions($parameters);
63
        $this->generateMenuAdaptors($parameters);
64
        $this->generateFixtures($parameters);
65
        $this->generatePagepartConfigs($parameters);
66
        $this->generatePagetemplateConfigs($parameters);
67
        $this->generateConfig();
68
        $this->generateRouting($parameters);
69
        $this->generateTemplates($parameters);
70
    }
71
72
    /**
73
     * Generate controller classes.
74
     *
75
     * @param array $parameters
76
     */
77 View Code Duplication
    private function generateControllers(array $parameters)
78
    {
79
        $relPath = '/Controller/';
80
        $sourceDir = $this->skeletonDir.$relPath;
81
        $targetDir = $this->bundle->getPath().$relPath;
82
83
        $this->renderSingleFile($sourceDir, $targetDir, 'DefaultController.php', $parameters, true);
84
85
        if ($this->demosite) {
86
	    $this->renderSingleFile($sourceDir, $targetDir, 'BikeAdminListController.php', $parameters, true);
87
        }
88
89
        $this->assistant->writeLine('Generating controllers : <info>OK</info>');
90
    }
91
92
    /**
93
     * Generate admin list classes.
94
     *
95
     * @param array $parameters
96
     */
97
    private function generateAdminLists(array $parameters)
98
    {
99
        if ($this->demosite) {
100
            $relPath = '/AdminList/';
101
            $this->renderFiles($this->skeletonDir.$relPath, $this->bundle->getPath().$relPath, $parameters, true);
102
103
            $this->assistant->writeLine('Generating admin lists : <info>OK</info>');
104
        }
105
    }
106
107
    /**
108
     * Generate the entity classes.
109
     *
110
     * @param array $parameters The template parameters
111
     */
112
    public function generateEntities(array $parameters)
113
    {
114
        $relPath = '/Entity/Pages/';
115
        $sourceDir = $this->skeletonDir.$relPath;
116
        $targetDir = $this->bundle->getPath().$relPath;
117
118
        $this->renderSingleFile($sourceDir, $targetDir, 'HomePage.php', $parameters);
119
        $this->renderSingleFile($sourceDir, $targetDir, 'ContentPage.php', $parameters);
120
        $this->renderSingleFile($sourceDir, $targetDir, 'BehatTestPage.php', $parameters);
121
122
        if ($this->demosite) {
123
            $this->renderSingleFile($sourceDir, $targetDir, 'FormPage.php', $parameters);
124
	    $this->renderSingleFile($sourceDir, $targetDir, 'SearchPage.php', $parameters);
125
        }
126
127 View Code Duplication
        if ($this->demosite) {
128
            $relPath = '/Entity/PageParts/';
129
            $sourceDir = $this->skeletonDir.$relPath;
130
            $targetDir = $this->bundle->getPath().$relPath;
131
132
	    $this->renderSingleFile($sourceDir, $targetDir, 'PageBannerPagePart.php', $parameters);
133
	    $this->renderSingleFile($sourceDir, $targetDir, 'ServicePagePart.php', $parameters);
134
	    $this->renderSingleFile($sourceDir, $targetDir, 'UspPagePart.php', $parameters);
135
	    $this->renderSingleFile($sourceDir, $targetDir, 'BikesListPagePart.php', $parameters);
136
        }
137
138 View Code Duplication
        if ($this->demosite) {
139
            $relPath = '/Entity/';
140
            $sourceDir = $this->skeletonDir.$relPath;
141
            $targetDir = $this->bundle->getPath().$relPath;
142
143
	    $this->renderSingleFile($sourceDir, $targetDir, 'Bike.php', $parameters);
144
	    $this->renderSingleFile($sourceDir, $targetDir, 'UspItem.php', $parameters);
145
        }
146
147
        $this->assistant->writeLine('Generating entities : <info>OK</info>');
148
    }
149
150
    /**
151
     * Generate the form type classes.
152
     *
153
     * @param array $parameters The template parameters
154
     */
155
    public function generateFormTypes(array $parameters)
156
    {
157
        $relPath = '/Form/Pages/';
158
        $sourceDir = $this->skeletonDir.$relPath;
159
        $targetDir = $this->bundle->getPath().$relPath;
160
161
        $this->renderSingleFile($sourceDir, $targetDir, 'HomePageAdminType.php', $parameters);
162
        $this->renderSingleFile($sourceDir, $targetDir, 'ContentPageAdminType.php', $parameters);
163
        $this->renderSingleFile($sourceDir, $targetDir, 'BehatTestPageAdminType.php', $parameters);
164
165
        if ($this->demosite) {
166
            $this->renderSingleFile($sourceDir, $targetDir, 'FormPageAdminType.php', $parameters);
167
        }
168
169 View Code Duplication
        if ($this->demosite) {
170
	    $relPath = '/Form/PageParts/';
171
	    $sourceDir = $this->skeletonDir.$relPath;
172
	    $targetDir = $this->bundle->getPath().$relPath;
173
174
	    $this->renderSingleFile($sourceDir, $targetDir, 'PageBannerPagePartAdminType.php', $parameters);
175
	    $this->renderSingleFile($sourceDir, $targetDir, 'ServicePagePartAdminType.php', $parameters);
176
	    $this->renderSingleFile($sourceDir, $targetDir, 'UspPagePartAdminType.php', $parameters);
177
	    $this->renderSingleFile($sourceDir, $targetDir, 'BikesListPagePartAdminType.php', $parameters);
178
	}
179
180 View Code Duplication
        if ($this->demosite) {
181
	    $relPath = '/Form/';
182
	    $sourceDir = $this->skeletonDir.$relPath;
183
	    $targetDir = $this->bundle->getPath().$relPath;
184
185
	    $this->renderSingleFile($sourceDir, $targetDir, 'BikeAdminType.php', $parameters);
186
	    $this->renderSingleFile($sourceDir, $targetDir, 'UspItemAdminType.php', $parameters);
187
        }
188
189
        $this->assistant->writeLine('Generating form types : <info>OK</info>');
190
    }
191
192
    /**
193
     * Generate the menu adaptors classes.
194
     *
195
     * @param array $parameters The template parameters
196
     */
197
    public function generateMenuAdaptors(array $parameters)
198
    {
199
        if ($this->demosite) {
200
            $relPath = '/Helper/Menu/';
201
            $sourceDir = $this->skeletonDir.$relPath;
202
            $targetDir = $this->bundle->getPath().$relPath;
203
204
            $this->renderSingleFile($sourceDir, $targetDir, 'AdminMenuAdaptor.php', $parameters);
205
206
            $file = $this->bundle->getPath().'/Resources/config/services.yml';
207
            if (!is_file($file)) {
208
                $ymlData = "services:";
209
            } else {
210
                $ymlData = "";
211
            }
212
            $ymlData .= "\n\n    ".strtolower($this->bundle->getName()).".admin_menu_adaptor:";
213
            $ymlData .= "\n        class: ".$this->bundle->getNamespace()."\Helper\Menu\AdminMenuAdaptor";
214
            $ymlData .= "\n        tags:";
215
            $ymlData .= "\n            -  { name: 'kunstmaan_admin.menu.adaptor' }\n";
216
            file_put_contents($file, $ymlData, FILE_APPEND);
217
218
            $this->assistant->writeLine('Generating menu adaptors : <info>OK</info>');
219
        }
220
    }
221
222
    /**
223
     * Generate the data fixtures classes.
224
     *
225
     * @param array $parameters The template parameters
226
     */
227
    public function generateFixtures(array $parameters)
228
    {
229
        $relPath = '/DataFixtures/ORM/DefaultSiteGenerator/';
230
        $sourceDir = $this->skeletonDir.$relPath;
231
        $targetDir = $this->bundle->getPath().$relPath;
232
233
        $this->renderSingleFile($sourceDir, $targetDir, 'DefaultSiteFixtures.php', $parameters);
234
235
        if ($this->demosite) {
236
            $this->renderSingleFile($sourceDir, $targetDir, 'SliderFixtures.php', $parameters);
237
            $this->renderSingleFile($sourceDir, $targetDir, 'SitemapFixtures.php', $parameters);
238
        }
239
240
        $this->assistant->writeLine('Generating fixtures : <info>OK</info>');
241
    }
242
243
    /**
244
     * Generate the pagepart section configuration.
245
     *
246
     * @param array $parameters The template parameters
247
     */
248
    public function generatePagepartConfigs(array $parameters)
249
    {
250
        $relPath = '/Resources/config/pageparts/';
251
        $sourceDir = $this->skeletonDir.$relPath;
252
        $targetDir = $this->bundle->getPath().$relPath;
253
254
        $this->renderSingleFile($sourceDir, $targetDir, 'main.yml', $parameters);
255
256
        if ($this->demosite) {
257
	    $this->renderSingleFile($sourceDir, $targetDir, 'header.yml', $parameters);
258
	    $this->renderSingleFile($sourceDir, $targetDir, 'section1.yml', $parameters);
259
	    $this->renderSingleFile($sourceDir, $targetDir, 'section2.yml', $parameters);
260
	    $this->renderSingleFile($sourceDir, $targetDir, 'section3.yml', $parameters);
261
	    $this->renderSingleFile($sourceDir, $targetDir, 'section4.yml', $parameters);
262
	    $this->renderSingleFile($sourceDir, $targetDir, 'section5.yml', $parameters);
263
            $this->renderSingleFile($sourceDir, $targetDir, 'form.yml', $parameters);
264
        }
265
266
        $this->assistant->writeLine('Generating pagepart configuration : <info>OK</info>');
267
    }
268
269
    /**
270
     * Generate the page template configuration.
271
     *
272
     * @param array $parameters The template parameters
273
     */
274
    public function generatePagetemplateConfigs(array $parameters)
275
    {
276
        $relPath = '/Resources/config/pagetemplates/';
277
        $sourceDir = $this->skeletonDir . $relPath;
278
        $targetDir = $this->bundle->getPath() . $relPath;
279
280
        $this->renderSingleFile($sourceDir, $targetDir, 'homepage.yml', $parameters);
281
        $this->renderSingleFile($sourceDir, $targetDir, 'contentpage.yml', $parameters);
282
        $this->renderSingleFile($sourceDir, $targetDir, 'behat-test-page.yml', $parameters);
283
284
        if ($this->demosite) {
285
	    $this->renderSingleFile($sourceDir, $targetDir, 'contentpage-with-submenu.yml', $parameters);
286
            $this->renderSingleFile($sourceDir, $targetDir, 'formpage.yml', $parameters);
287
	    $this->renderSingleFile($sourceDir, $targetDir, 'searchpage.yml', $parameters);
288
        }
289
290
        $this->assistant->writeLine('Generating pagetemplate configuration : <info>OK</info>');
291
    }
292
293
    /**
294
     * Append to the application config file.
295
     */
296
    public function generateConfig()
297
    {
298
        $configFile = $this->rootDir.'/app/config/config.yml';
299
        $config = file_get_contents($configFile);
300
301
        $data = Yaml::parse($config);
302
        if (!array_key_exists('white_october_pagerfanta', $data)) {
303
            $ymlData = "\n\nwhite_october_pagerfanta:";
304
            $ymlData .= "\n    default_view: twitter_bootstrap\n";
305
            file_put_contents($configFile, $ymlData, FILE_APPEND);
306
        }
307
    }
308
309
    /**
310
     * Generate bundle routing configuration.
311
     * @param array $parameters The template parameters
312
     */
313
    public function generateRouting(array $parameters)
314
    {
315
        $relPath = '/Resources/config/';
316
        $sourceDir = $this->skeletonDir.$relPath;
317
        $targetDir = $this->bundle->getPath().$relPath;
318
319
        $this->renderSingleFile($sourceDir, $targetDir, 'routing.yml', $parameters, true);
320
321
        $this->assistant->writeLine('Generating routing : <info>OK</info>');
322
    }
323
324
    /**
325
     * Generate the twig templates.
326
     *
327
     * @param array $parameters The template parameters
328
     */
329
    public function generateTemplates(array $parameters)
330
    {
331
        $relPath = '/Resources/views/Layout/';
332
        $sourceDir = $this->skeletonDir.$relPath;
333
        $targetDir = $this->bundle->getPath().$relPath;
334
335
	if ($this->demosite) {
336
	    $this->renderSingleFile($sourceDir, $targetDir, 'submenu.html.twig', $parameters);
337
	}
338
339
        // Pages
340
341
        $relPath = '/Resources/views/Pages/HomePage/';
342
        $sourceDir = $this->skeletonDir.$relPath;
343
        $targetDir = $this->bundle->getPath().$relPath;
344
345
        $this->renderSingleFile($sourceDir, $targetDir, 'pagetemplate.html.twig', $parameters);
346
        $this->renderSingleFile($sourceDir, $targetDir, 'view.html.twig', $parameters);
347
348
        $relPath = '/Resources/views/Pages/ContentPage/';
349
        $this->renderFiles($this->skeletonDir.$relPath, $this->bundle->getPath().$relPath, $parameters, true);
350
351
        if ($this->demosite) {
352
            $relPath = '/Resources/views/Pages/FormPage/';
353
            $this->renderFiles($this->skeletonDir.$relPath, $this->bundle->getPath().$relPath, $parameters, true);
354
355
	    $relPath = '/Resources/views/Pages/SearchPage/';
356
            $this->renderFiles($this->skeletonDir.$relPath, $this->bundle->getPath().$relPath, $parameters, true);
357
        }
358
359
        // Pageparts
360
361
        if ($this->demosite) {
362
	    $relPath = '/Resources/views/PageParts/PageBannerPagePart/';
363
	    $this->renderFiles($this->skeletonDir.$relPath, $this->bundle->getPath().$relPath, $parameters, true);
364
365
	    $relPath = '/Resources/views/PageParts/ServicePagePart/';
366
	    $this->renderFiles($this->skeletonDir.$relPath, $this->bundle->getPath().$relPath, $parameters, true);
367
368
	    $relPath = '/Resources/views/PageParts/UspPagePart/';
369
	    $this->renderFiles($this->skeletonDir.$relPath, $this->bundle->getPath().$relPath, $parameters, true);
370
371
	    $relPath = '/Resources/views/PageParts/BikesListPagePart/';
372
            $this->renderFiles($this->skeletonDir.$relPath, $this->bundle->getPath().$relPath, $parameters, true);
373
        }
374
375
        // Error templates
376
        $relPath = '/Resources/views/Error/';
377
        $this->renderFiles($this->skeletonDir.$relPath, $this->bundle->getPath().$relPath, $parameters, true);
378
379
        $sourcePath = '/app/TwigBundle/';
380
        $targetPath = $this->rootDir.'/app/Resources/TwigBundle/';
381
        $this->renderFiles($this->skeletonDir.$sourcePath, $targetPath, $parameters, true);
382
383
        // Bundle overwrites
384
385
        if ($this->demosite) {
386
            $sourcePath = '/app/KunstmaanSitemapBundle/';
387
            $targetPath = $this->rootDir.'/app/Resources/KunstmaanSitemapBundle/';
388
389
            $this->renderFiles($this->skeletonDir.$sourcePath, $targetPath, $parameters, true);
390
391
            $sourcePath = '/app/KunstmaanFormBundle/';
392
            $targetPath = $this->rootDir.'/app/Resources/KunstmaanFormBundle/';
393
            $this->renderFiles($this->skeletonDir.$sourcePath, $targetPath, $parameters, true);
394
        }
395
396
        $this->assistant->writeLine('Generating template files : <info>OK</info>');
397
    }
398
399
    /**
400
     * Generate the twig extensions.
401
     *
402
     * @param array $parameters The template parameters
403
     */
404 View Code Duplication
    public function generateTwigExtensions($parameters)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
405
    {
406
        $relPath = '/Twig/';
407
        if ($this->demosite) {
408
            $this->renderSingleFile($this->skeletonDir.$relPath, $this->bundle->getPath().$relPath, 'BikesTwigExtension.php', $parameters, true);
409
        }
410
411
        $relPath = '/Resources/config/';
412
        $sourceDir = $this->skeletonDir.$relPath;
413
        $targetDir = $this->bundle->getPath().$relPath;
414
        $this->renderSingleFile($sourceDir, $targetDir, 'services.yml', $parameters, true);
415
    }
416
417
    /**
418
     * Returns true if we detect the site uses the locale.
419
     *
420
     * @return bool
421
     */
422
    private function isMultiLangEnvironment() {
423
        // use the multilanguage parameter, if it exists
424
        if ($this->container->hasParameter('multilanguage')) {
425
            return $this->container->getParameter('multilanguage');
426
        }
427
428
        // This is a pretty silly implementation.
429
        // It just checks if it can find _locale in the routing.yml
430
        $routingFile = file_get_contents($this->rootDir.'/app/config/routing.yml');
431
        return preg_match('/_locale:/i', $routingFile);
432
    }
433
}
434