Completed
Push — 5.6 ( 679697...f4d50c )
by Jeroen
16:35 queued 10:49
created

DefaultSiteGenerator::generateControllers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 2.0054

Importance

Changes 0
Metric Value
dl 14
loc 14
ccs 8
cts 9
cp 0.8889
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0054
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 string $prefix
38
     * @param string $rootDir
39
     * @param bool   $demosite
40
     */
41 1
    public function generate(BundleInterface $bundle, $prefix, $rootDir, $demosite = false)
42
    {
43 1
        $this->bundle = $bundle;
44 1
        $this->prefix = GeneratorUtils::cleanPrefix($prefix);
45 1
        $this->rootDir = $rootDir;
46 1
        $this->demosite = $demosite;
47
48
        $parameters = [
49 1
            'namespace' => $this->bundle->getNamespace(),
50 1
            'bundle' => $this->bundle,
51 1
            'bundle_name' => $this->bundle->getName(),
52 1
            'prefix' => $this->prefix,
53 1
            'demosite' => $this->demosite,
54 1
            'multilanguage' => $this->isMultiLangEnvironment(),
55 1
            'isV4' => $this->isSymfony4(),
56
        ];
57
58 1
        $this->generateControllers($parameters);
59 1
        $this->generateAdminLists($parameters);
60 1
        $this->generateEntities($parameters);
61 1
        $this->generateFormTypes($parameters);
62 1
        $this->generateTwigExtensions($parameters);
63 1
        $this->generateMenuAdaptors($parameters);
64 1
        $this->generateFixtures($parameters);
65 1
        $this->generatePagepartConfigs($parameters);
66 1
        $this->generatePagetemplateConfigs($parameters);
67 1
        $this->generateConfig();
68 1
        $this->generateRouting($parameters);
69 1
        $this->generateTemplates($parameters);
70 1
    }
71
72
    /**
73
     * Generate controller classes.
74
     */
75 1 View Code Duplication
    private function generateControllers(array $parameters)
0 ignored issues
show
Duplication introduced by
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...
76
    {
77 1
        $relPath = '/Controller/';
78 1
        $sourceDir = $this->skeletonDir . $relPath;
79 1
        $targetDir = $this->bundle->getPath() . $relPath;
80
81 1
        $this->renderSingleFile($sourceDir, $targetDir, 'DefaultController.php', $parameters, true);
82
83 1
        if ($this->demosite) {
84
            $this->renderSingleFile($sourceDir, $targetDir, 'BikeAdminListController.php', $parameters, true);
85
        }
86
87 1
        $this->assistant->writeLine('Generating controllers : <info>OK</info>');
88 1
    }
89
90
    /**
91
     * Generate admin list classes.
92
     */
93 1
    private function generateAdminLists(array $parameters)
94
    {
95 1
        if ($this->demosite) {
96
            $relPath = '/AdminList/';
97
            $this->renderFiles($this->skeletonDir . $relPath, $this->bundle->getPath() . $relPath, $parameters, true);
98
99
            $this->assistant->writeLine('Generating admin lists : <info>OK</info>');
100
        }
101 1
    }
102
103
    /**
104
     * Generate the entity classes.
105
     *
106
     * @param array $parameters The template parameters
107
     */
108 1
    public function generateEntities(array $parameters)
109
    {
110 1
        $relPath = '/Entity/Pages/';
111 1
        $sourceDir = $this->skeletonDir . $relPath;
112 1
        $targetDir = $this->bundle->getPath() . $relPath;
113
114 1
        $this->renderSingleFile($sourceDir, $targetDir, 'HomePage.php', $parameters);
115 1
        $this->renderSingleFile($sourceDir, $targetDir, 'ContentPage.php', $parameters);
116 1
        $this->renderSingleFile($sourceDir, $targetDir, 'BehatTestPage.php', $parameters);
117
118 1
        if ($this->demosite) {
119
            $this->renderSingleFile($sourceDir, $targetDir, 'FormPage.php', $parameters);
120
            $this->renderSingleFile($sourceDir, $targetDir, 'SearchPage.php', $parameters);
121
        }
122
123 1 View Code Duplication
        if ($this->demosite) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
124
            $relPath = '/Entity/PageParts/';
125
            $sourceDir = $this->skeletonDir . $relPath;
126
            $targetDir = $this->bundle->getPath() . $relPath;
127
128
            $this->renderSingleFile($sourceDir, $targetDir, 'PageBannerPagePart.php', $parameters);
129
            $this->renderSingleFile($sourceDir, $targetDir, 'ServicePagePart.php', $parameters);
130
            $this->renderSingleFile($sourceDir, $targetDir, 'UspPagePart.php', $parameters);
131
            $this->renderSingleFile($sourceDir, $targetDir, 'BikesListPagePart.php', $parameters);
132
        }
133
134 1 View Code Duplication
        if ($this->demosite) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
135
            $relPath = '/Entity/';
136
            $sourceDir = $this->skeletonDir . $relPath;
137
            $targetDir = $this->bundle->getPath() . $relPath;
138
139
            $this->renderSingleFile($sourceDir, $targetDir, 'Bike.php', $parameters);
140
            $this->renderSingleFile($sourceDir, $targetDir, 'UspItem.php', $parameters);
141
        }
142
143 1
        $this->assistant->writeLine('Generating entities : <info>OK</info>');
144 1
    }
145
146
    /**
147
     * Generate the form type classes.
148
     *
149
     * @param array $parameters The template parameters
150
     */
151 1
    public function generateFormTypes(array $parameters)
152
    {
153 1
        $relPath = '/Form/Pages/';
154 1
        $sourceDir = $this->skeletonDir . $relPath;
155 1
        $targetDir = $this->bundle->getPath() . $relPath;
156
157 1
        $this->renderSingleFile($sourceDir, $targetDir, 'HomePageAdminType.php', $parameters);
158 1
        $this->renderSingleFile($sourceDir, $targetDir, 'ContentPageAdminType.php', $parameters);
159 1
        $this->renderSingleFile($sourceDir, $targetDir, 'BehatTestPageAdminType.php', $parameters);
160
161 1
        if ($this->demosite) {
162
            $this->renderSingleFile($sourceDir, $targetDir, 'FormPageAdminType.php', $parameters);
163
        }
164
165 1 View Code Duplication
        if ($this->demosite) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
166
            $relPath = '/Form/PageParts/';
167
            $sourceDir = $this->skeletonDir . $relPath;
168
            $targetDir = $this->bundle->getPath() . $relPath;
169
170
            $this->renderSingleFile($sourceDir, $targetDir, 'PageBannerPagePartAdminType.php', $parameters);
171
            $this->renderSingleFile($sourceDir, $targetDir, 'ServicePagePartAdminType.php', $parameters);
172
            $this->renderSingleFile($sourceDir, $targetDir, 'UspPagePartAdminType.php', $parameters);
173
            $this->renderSingleFile($sourceDir, $targetDir, 'BikesListPagePartAdminType.php', $parameters);
174
        }
175
176 1 View Code Duplication
        if ($this->demosite) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
177
            $relPath = '/Form/';
178
            $sourceDir = $this->skeletonDir . $relPath;
179
            $targetDir = $this->bundle->getPath() . $relPath;
180
181
            $this->renderSingleFile($sourceDir, $targetDir, 'BikeAdminType.php', $parameters);
182
            $this->renderSingleFile($sourceDir, $targetDir, 'UspItemAdminType.php', $parameters);
183
        }
184
185 1
        $this->assistant->writeLine('Generating form types : <info>OK</info>');
186 1
    }
187
188
    /**
189
     * Generate the menu adaptors classes.
190
     *
191
     * @param array $parameters The template parameters
192
     */
193 1
    public function generateMenuAdaptors(array $parameters)
194
    {
195 1
        if ($this->demosite) {
196
            $relPath = '/Helper/Menu/';
197
            $sourceDir = $this->skeletonDir . $relPath;
198
            $targetDir = $this->bundle->getPath() . $relPath;
199
200
            $this->renderSingleFile($sourceDir, $targetDir, 'AdminMenuAdaptor.php', $parameters);
201
202
            if ($this->isSymfony4()) {
203
                return;
204
            }
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 1
    }
221
222
    /**
223
     * Generate the data fixtures classes.
224
     *
225
     * @param array $parameters The template parameters
226
     */
227 1
    public function generateFixtures(array $parameters)
228
    {
229 1
        $relPath = '/DataFixtures/ORM/DefaultSiteGenerator/';
230 1
        $sourceDir = $this->skeletonDir . $relPath;
231 1
        $targetDir = $this->bundle->getPath() . $relPath;
232
233 1
        $this->renderSingleFile($sourceDir, $targetDir, 'DefaultSiteFixtures.php', $parameters);
234
235 1
        if ($this->demosite) {
236
            $this->renderSingleFile($sourceDir, $targetDir, 'SliderFixtures.php', $parameters);
237
            $this->renderSingleFile($sourceDir, $targetDir, 'SitemapFixtures.php', $parameters);
238
        }
239
240 1
        $this->assistant->writeLine('Generating fixtures : <info>OK</info>');
241 1
    }
242
243
    /**
244
     * Generate the pagepart section configuration.
245
     *
246
     * @param array $parameters The template parameters
247
     */
248 1
    public function generatePagepartConfigs(array $parameters)
249
    {
250 1
        $basePath = $this->isSymfony4() ? $this->container->getParameter('kernel.project_dir') : $this->bundle->getPath();
251 1
        $relPath = $this->isSymfony4() ? '/config/kunstmaancms/pageparts' : '/Resources/config/pageparts/';
252 1
        $sourceDir = $this->skeletonDir . '/Resources/config/pageparts/';
253 1
        $targetDir = $basePath . $relPath;
254
255 1
        $this->renderSingleFile($sourceDir, $targetDir, 'main.yml', $parameters);
256
257 1
        if ($this->demosite) {
258
            $this->renderSingleFile($sourceDir, $targetDir, 'header.yml', $parameters);
259
            $this->renderSingleFile($sourceDir, $targetDir, 'section1.yml', $parameters);
260
            $this->renderSingleFile($sourceDir, $targetDir, 'section2.yml', $parameters);
261
            $this->renderSingleFile($sourceDir, $targetDir, 'section3.yml', $parameters);
262
            $this->renderSingleFile($sourceDir, $targetDir, 'section4.yml', $parameters);
263
            $this->renderSingleFile($sourceDir, $targetDir, 'section5.yml', $parameters);
264
            $this->renderSingleFile($sourceDir, $targetDir, 'form.yml', $parameters);
265
        }
266
267 1
        $this->assistant->writeLine('Generating pagepart configuration : <info>OK</info>');
268 1
    }
269
270
    /**
271
     * Generate the page template configuration.
272
     *
273
     * @param array $parameters The template parameters
274
     */
275 1
    public function generatePagetemplateConfigs(array $parameters)
276
    {
277 1
        $basePath = $this->isSymfony4() ? $this->container->getParameter('kernel.project_dir') : $this->bundle->getPath();
278 1
        $relPath = $this->isSymfony4() ? '/config/kunstmaancms/pagetemplates/' : '/Resources/config/pagetemplates/';
279 1
        $sourceDir = $this->skeletonDir . '/Resources/config/pagetemplates/';
280 1
        $targetDir = $basePath . $relPath;
281
282 1
        $this->renderSingleFile($sourceDir, $targetDir, 'homepage.yml', $parameters);
283 1
        $this->renderSingleFile($sourceDir, $targetDir, 'contentpage.yml', $parameters);
284 1
        $this->renderSingleFile($sourceDir, $targetDir, 'behat-test-page.yml', $parameters);
285
286 1
        if ($this->demosite) {
287
            $this->renderSingleFile($sourceDir, $targetDir, 'contentpage-with-submenu.yml', $parameters);
288
            $this->renderSingleFile($sourceDir, $targetDir, 'formpage.yml', $parameters);
289
            $this->renderSingleFile($sourceDir, $targetDir, 'searchpage.yml', $parameters);
290
        }
291
292 1
        $this->assistant->writeLine('Generating pagetemplate configuration : <info>OK</info>');
293 1
    }
294
295
    /**
296
     * Append to the application config file.
297
     */
298 1
    public function generateConfig()
299
    {
300 1
        if ($this->isSymfony4()) {
301 1
            return;
302
        }
303
304
        $configFile = $this->rootDir . '/app/config/config.yml';
305
        $config = file_get_contents($configFile);
306
307
        $data = Yaml::parse($config);
308
        if (!array_key_exists('white_october_pagerfanta', $data)) {
309
            $ymlData = "\n\nwhite_october_pagerfanta:";
310
            $ymlData .= "\n    default_view: twitter_bootstrap\n";
311
            file_put_contents($configFile, $ymlData, FILE_APPEND);
312
        }
313
    }
314
315
    /**
316
     * Generate bundle routing configuration.
317
     *
318
     * @param array $parameters The template parameters
319
     */
320 1
    public function generateRouting(array $parameters)
321
    {
322 1
        if ($this->isSymfony4()) {
323 1
            return;
324
        }
325
326
        $relPath = '/Resources/config/';
327
        $sourceDir = $this->skeletonDir . $relPath;
328
        $targetDir = $this->bundle->getPath() . $relPath;
329
330
        $this->renderSingleFile($sourceDir, $targetDir, 'routing.yml', $parameters, true);
331
332
        $this->assistant->writeLine('Generating routing : <info>OK</info>');
333
    }
334
335
    /**
336
     * Generate the twig templates.
337
     *
338
     * @param array $parameters The template parameters
339
     */
340 1
    public function generateTemplates(array $parameters)
341
    {
342 1
        $relPath = '/Resources/views/Layout/';
343 1
        $sourceDir = $this->skeletonDir . $relPath;
344 1
        $targetDir = $this->getTemplateDir($this->bundle) . '/Layout/';
345
346 1
        if ($this->demosite) {
347
            $this->renderSingleFile($sourceDir, $targetDir, 'submenu.html.twig', $parameters);
348
        }
349
350
        // Pages
351
352 1
        $relPath = '/Resources/views/Pages/HomePage/';
353 1
        $sourceDir = $this->skeletonDir . $relPath;
354 1
        $targetDir = $this->getTemplateDir($this->bundle) . '/Pages/HomePage/';
355
356 1
        $this->renderSingleFile($sourceDir, $targetDir, 'pagetemplate.html.twig', $parameters);
357 1
        $this->renderSingleFile($sourceDir, $targetDir, 'view.html.twig', $parameters);
358
359 1
        $relPath = '/Resources/views/Pages/ContentPage/';
360 1
        $this->renderFiles($this->skeletonDir . $relPath, $this->getTemplateDir($this->bundle) . '/Pages/ContentPage/', $parameters, true);
361
362 1
        if ($this->demosite) {
363
            $relPath = '/Resources/views/Pages/FormPage/';
364
            $this->renderFiles($this->skeletonDir . $relPath, $this->getTemplateDir($this->bundle) . '/Pages/FormPage/', $parameters, true);
365
366
            $relPath = '/Resources/views/Pages/SearchPage/';
367
            $this->renderFiles($this->skeletonDir . $relPath, $this->getTemplateDir($this->bundle) . '/Pages/SearchPage/', $parameters, true);
368
        }
369
370
        // Pageparts
371
372 1
        if ($this->demosite) {
373
            $relPath = '/Resources/views/PageParts/PageBannerPagePart/';
374
            $this->renderFiles($this->skeletonDir . $relPath, $this->getTemplateDir($this->bundle) . '/PageParts/PageBannerPagePart/', $parameters, true);
375
376
            $relPath = '/Resources/views/PageParts/ServicePagePart/';
377
            $this->renderFiles($this->skeletonDir . $relPath, $this->getTemplateDir($this->bundle) . '/PageParts/ServicePagePart/', $parameters, true);
378
379
            $relPath = '/Resources/views/PageParts/UspPagePart/';
380
            $this->renderFiles($this->skeletonDir . $relPath, $this->getTemplateDir($this->bundle) . '/PageParts/UspPagePart/', $parameters, true);
381
382
            $relPath = '/Resources/views/PageParts/BikesListPagePart/';
383
            $this->renderFiles($this->skeletonDir . $relPath, $this->getTemplateDir($this->bundle) . '/PageParts/BikesListPagePart/', $parameters, true);
384
        }
385
386
        // Error templates
387 1
        $relPath = '/Resources/views/Error/';
388 1
        $this->renderFiles($this->skeletonDir . $relPath, $this->getTemplateDir($this->bundle) . '/Error/', $parameters, true);
389
390 1
        $sourcePath = '/app/TwigBundle/views';
391 1
        $targetPath = $this->isSymfony4() ? $this->rootDir . '/templates/bundles/TwigBundle' : $this->rootDir . '/app/Resources/TwigBundle/views';
392 1
        $this->renderFiles($this->skeletonDir . $sourcePath, $targetPath, $parameters, true);
393
394
        // Bundle overwrites
395
396 1
        if ($this->demosite) {
397
            $sourcePath = '/app/KunstmaanSitemapBundle/views';
398
            $targetPath = $this->isSymfony4() ? $this->rootDir . '/templates/bundles/KunstmaanSitemapBundle' : $this->rootDir . '/app/Resources/KunstmaanSitemapBundle/views';
399
400
            $this->renderFiles($this->skeletonDir . $sourcePath, $targetPath, $parameters, true);
401
402
            $sourcePath = '/app/KunstmaanFormBundle/views';
403
            $targetPath = $this->isSymfony4() ? $this->rootDir . '/templates/bundles/KunstmaanFormBundle' : $this->rootDir . '/app/Resources/KunstmaanFormBundle/views';
404
            $this->renderFiles($this->skeletonDir . $sourcePath, $targetPath, $parameters, true);
405
        }
406
407 1
        $this->assistant->writeLine('Generating template files : <info>OK</info>');
408 1
    }
409
410
    /**
411
     * Generate the twig extensions.
412
     *
413
     * @param array $parameters The template parameters
414
     */
415 1
    public function generateTwigExtensions($parameters)
416
    {
417 1
        $relPath = '/Twig/';
418 1
        if ($this->demosite) {
419
            $this->renderSingleFile($this->skeletonDir . $relPath, $this->bundle->getPath() . $relPath, 'BikesTwigExtension.php', $parameters, true);
420
        }
421
422 1
        if ($this->isSymfony4()) {
423 1
            return;
424
        }
425
426
        $relPath = '/Resources/config/';
427
        $sourceDir = $this->skeletonDir . $relPath;
428
        $targetDir = $this->bundle->getPath() . $relPath;
429
        $this->renderSingleFile($sourceDir, $targetDir, 'services.yml', $parameters, true);
430
    }
431
432
    /**
433
     * Returns true if we detect the site uses the locale.
434
     *
435
     * @return bool
436
     */
437 1
    private function isMultiLangEnvironment()
438
    {
439
        // use the multilanguage parameter, if it exists
440 1
        if ($this->container->hasParameter('kunstmaan_admin.multi_language')) {
441 1
            return $this->container->getParameter('kunstmaan_admin.multi_language');
442
        }
443
444
        // This is a pretty silly implementation.
445
        // It just checks if it can find _locale in the routing.yml
446
        $routingFile = file_get_contents($this->rootDir . '/app/config/routing.yml');
447
448
        return preg_match('/_locale:/i', $routingFile);
449
    }
450
}
451