Completed
Push — master ( 51c294...2b970c )
by Beñat
04:36
created

Lin3sCmsKernelBundle::twigBasePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the CMS Kernel package.
5
 *
6
 * Copyright (c) 2016-present LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LIN3S\CMSKernel\Infrastructure\Symfony\Bundle;
13
14
use LIN3S\CMSKernel\Infrastructure\Symfony\Bundle\DependencyInjection\Compiler\ClassMapTemplateFactoryPass;
15
use LIN3S\CMSKernel\Infrastructure\Symfony\Bundle\DependencyInjection\Compiler\DoctrineORMCustomTypesPass;
16
use LIN3S\CMSKernel\Infrastructure\Symfony\Bundle\DependencyInjection\Compiler\RegisterBusesPass;
17
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
20
use Symfony\Component\HttpKernel\Bundle\Bundle;
21
22
/**
23
 * @author Beñat Espiña <[email protected]>
24
 */
25
class Lin3sCmsKernelBundle extends Bundle
26
{
27
    const DEPENDENT_BUNDLES = [
28
        'SimpleBusCommandBusBundle',
29
    ];
30
31
    public function build(ContainerBuilder $container)
32
    {
33
        $this->checkDependentBundlesAreEnable($container);
34
35
        $container->addCompilerPass(new ClassMapTemplateFactoryPass(), PassConfig::TYPE_OPTIMIZE);
36
        $container->addCompilerPass(new DoctrineORMCustomTypesPass(), PassConfig::TYPE_OPTIMIZE);
37
        $container->addCompilerPass(new RegisterBusesPass(), PassConfig::TYPE_OPTIMIZE);
38
39
        $container->loadFromExtension('doctrine', [
40
            'orm' => [
41
                'mappings' => [
42
                    'Lin3sCmsKernelMenu'        => [
43
                        'type'      => 'xml',
44
                        'is_bundle' => false,
45
                        'dir'       => $this->doctrineORMBasePath() . '/Menu/Mapping/',
46
                        'prefix'    => 'LIN3S\CMSKernel\Domain\Model\Menu',
47
                    ],
48
                    'Lin3sCmsKernelPage'        => [
49
                        'type'      => 'xml',
50
                        'is_bundle' => false,
51
                        'dir'       => $this->doctrineORMBasePath() . '/Page/Mapping/',
52
                        'prefix'    => 'LIN3S\CMSKernel\Domain\Model\Page',
53
                    ],
54
                    'Lin3sCmsKernelSeo'         => [
55
                        'type'      => 'xml',
56
                        'is_bundle' => false,
57
                        'dir'       => $this->doctrineORMBasePath() . '/Seo/Mapping/',
58
                        'prefix'    => 'LIN3S\CMSKernel\Domain\Model\Seo',
59
                    ],
60
                    'Lin3sCmsKernelTranslation' => [
61
                        'type'      => 'xml',
62
                        'is_bundle' => false,
63
                        'dir'       => $this->doctrineORMBasePath() . '/Translation/Mapping/',
64
                        'prefix'    => 'LIN3S\CMSKernel\Domain\Model\Translation',
65
                    ],
66
                ],
67
            ],
68
        ]);
69
70
        $container->loadFromExtension('twig', [
71
            'paths'       => [
72
                $this->twigBasePath(),
73
            ],
74
            'form_themes' => [
75
                'Form/file.html.twig',
76
                'Form/template_selector.html.twig',
77
                'Form/wysiwyg.html.twig',
78
                'Form/menu_tree.html.twig',
79
            ],
80
        ]);
81
    }
82
83
    private function checkDependentBundlesAreEnable(ContainerBuilder $container)
84
    {
85
        $enabledBundles = $container->getParameter('kernel.bundles');
86
        foreach (self::DEPENDENT_BUNDLES as $requiredBundle) {
87
            if (!isset($enabledBundles[$requiredBundle])) {
88
                throw new RuntimeException(
89
                    sprintf(
90
                        'In order to use "%s" you also need to enable and configure the "%s"',
91
                        $this->getName(),
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
92
                        $requiredBundle
93
                    )
94
                );
95
            }
96
        }
97
    }
98
99
    private function doctrineORMBasePath()
100
    {
101
        return $this->basePath() . '/../../Persistence/Doctrine/ORM';
102
    }
103
104
    private function twigBasePath()
105
    {
106
        return $this->basePath() . '/../../Ui/Templates/Twig';
107
    }
108
109
    private function basePath()
110
    {
111
        return dirname((new \ReflectionClass(self::class))->getFileName());
112
    }
113
}
114