processSassConfiguration()   A
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.3888
c 0
b 0
f 0
cc 5
nc 9
nop 1
1
<?php
2
/**
3
 * This file is part of BraincraftedBootstrapBundle.
4
 *
5
 * (c) 2012-2013 by Florian Eckerstorfer
6
 */
7
8
namespace Braincrafted\Bundle\BootstrapBundle\DependencyInjection;
9
10
use Symfony\Component\Config\FileLocator;
11
use Symfony\Component\DependencyInjection\ContainerBuilder;
12
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
13
use Symfony\Component\DependencyInjection\Loader;
14
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
15
16
use Braincrafted\Bundle\BootstrapBundle\DependencyInjection\AsseticConfiguration;
17
18
/**
19
 * BraincraftedBootstrapExtension
20
 *
21
 * @package    BraincraftedBootstrapBundle
22
 * @subpackage DependencyInjection
23
 * @author     Florian Eckerstorfer <[email protected]>
24
 * @copyright  2012-2013 Florian Eckerstorfer
25
 * @license    http://opensource.org/licenses/MIT The MIT License
26
 * @link       http://bootstrap.braincrafted.com Bootstrap for Symfony2
27
 */
28
class BraincraftedBootstrapExtension extends Extension implements PrependExtensionInterface
29
{
30
    /** @var string */
31
    protected $formTemplate = 'BraincraftedBootstrapBundle:Form:bootstrap.html.twig';
32
33
    /** @var string */
34
    protected $menuTemplate = 'BraincraftedBootstrapBundle:Menu:bootstrap.html.twig';
35
36
    /** @var string */
37
    protected $paginationTemplate = 'BraincraftedBootstrapBundle:Pagination:bootstrap.html.twig';
38
39
    /**
40
     * {@inheritDoc}
41
     */
42
    public function load(array $configs, ContainerBuilder $container)
43
    {
44
        $configuration = new Configuration();
45
        $config = $this->processSassConfiguration(
46
            $this->processConfiguration($configuration, $configs)
47
        );
48
49
        $loader = new Loader\XmlFileLoader(
50
            $container,
51
            new FileLocator(__DIR__.'/../Resources/config')
52
        );
53
        $loader->load('services/form.xml');
54
        $loader->load('services/twig.xml');
55
        $loader->load('services/session.xml');
56
57
        if (true === isset($config['customize'])) {
58
            $container->setParameter('braincrafted_bootstrap.customize', $config['customize']);
59
        }
60
        $container->setParameter('braincrafted_bootstrap.assets_dir', $config['assets_dir']);
61
        $container->setParameter('braincrafted_bootstrap.fontawesome_dir', $config['fontawesome_dir']);
62
        $container->setParameter('braincrafted_bootstrap.fonts_dir', $config['fonts_dir']);
63
        $container->setParameter('braincrafted_bootstrap.output_dir', $config['output_dir']);
64
65
        // changed from css_preprocessor to css_preprocessor for 3.0
66
        $container->setParameter('braincrafted_bootstrap.css_preprocessor', $config['css_preprocessor']);
67
        $container->setParameter('braincrafted_bootstrap.icon_prefix', $config['icon_prefix']);
68
        $container->setParameter('braincrafted_bootstrap.icon_tag', $config['icon_tag']);
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74
    public function prepend(ContainerBuilder $container)
75
    {
76
        $bundles = $container->getParameter('kernel.bundles');
77
78
        $configs = $container->getExtensionConfig($this->getAlias());
79
        $config = $this->processSassConfiguration(
80
            $this->processConfiguration(new Configuration(), $configs)
81
        );
82
83
        // Configure Assetic if AsseticBundle is activated and the option
84
        // "braincrafted_bootstrap.auto_configure.assetic" is set to TRUE (default value).
85
        if (true === isset($bundles['AsseticBundle']) && true === $config['auto_configure']['assetic']) {
86
            $this->configureAsseticBundle($container, $config);
87
        }
88
89
        // Configure Twig if TwigBundle is activated and the option
90
        // "braincrafted_bootstrap.auto_configure.twig" is set to TRUE (default value).
91
        if (true === isset($bundles['TwigBundle']) && true === $config['auto_configure']['twig']) {
92
            $this->configureTwigBundle($container);
93
        }
94
95
        // Configure KnpMenu if KnpMenuBundle and TwigBundle are activated and the option
96
        // "braincrafted_bootstrap.auto_configure.knp_menu" is set to TRUE (default value).
97
        if (true === isset($bundles['TwigBundle']) &&
98
            true === isset($bundles['KnpMenuBundle']) &&
99
            true === $config['auto_configure']['knp_menu']) {
100
            $this->configureKnpMenuBundle($container);
101
        }
102
103
        // Configure KnpPaginiator if KnpPaginatorBundle and TwigBundle are activated and the option
104
        // "braincrafted_bootstrap.auto_configure.knp_paginator" is set to TRUE (default value).
105
        if (true === isset($bundles['TwigBundle']) &&
106
            true === isset($bundles['KnpPaginatorBundle']) &&
107
            true === $config['auto_configure']['knp_paginator']) {
108
            $this->configureKnpPaginatorBundle($container);
109
        }
110
    }
111
112
    /**
113
     * @param array $config
114
     *
115
     * @return array
116
     */
117
    protected function processSassConfiguration(array $config)
118
    {
119
        // changed from css_preprocessor to css_preprocessor for 3.0
120
        if (in_array($config['css_preprocessor'], array('sass', 'scssphp'))) {
121
            if ($config['assets_dir'] === Configuration::DEFAULT_ASSETS_DIR) {
122
                $config['assets_dir'] = Configuration::DEFAULT_ASSETS_DIR_SASS;
123
            }
124
            if ($config['customize']['bootstrap_output'] === Configuration::DEFAULT_BOOTSTRAP_OUTPUT) {
125
                $config['customize']['bootstrap_output'] = Configuration::DEFAULT_BOOTSTRAP_OUTPUT_SASS;
126
            }
127
            if ($config['customize']['bootstrap_template'] === Configuration::DEFAULT_BOOTSTRAP_TEMPLATE) {
128
                $config['customize']['bootstrap_template'] = Configuration::DEFAULT_BOOTSTRAP_TEMPLATE_SASS;
129
            }
130
        }
131
132
        return $config;
133
    }
134
135
    /**
136
     * @param ContainerBuilder $container The service container
137
     * @param array            $config    The bundle configuration
138
     *
139
     * @return void
140
     */
141
    protected function configureAsseticBundle(ContainerBuilder $container, array $config)
142
    {
143
        foreach (array_keys($container->getExtensions()) as $name) {
144
            switch ($name) {
145
                case 'assetic':
146
                    $asseticConfig = new AsseticConfiguration;
147
                    $container->prependExtensionConfig(
148
                        $name,
149
                        array('assets' => $asseticConfig->build($config))
150
                    );
151
                    break;
152
            }
153
        }
154
    }
155
156
    /**
157
     * @param ContainerBuilder $container The service container
158
     *
159
     * @return void
160
     */
161
    protected function configureTwigBundle(ContainerBuilder $container)
162
    {
163
        foreach (array_keys($container->getExtensions()) as $name) {
164
            switch ($name) {
165
                case 'twig':
166
                    $container->prependExtensionConfig(
167
                        $name,
168
                        array('form_themes'  => array($this->formTemplate))
169
                    );
170
                    break;
171
            }
172
        }
173
    }
174
175
    /**
176
     * @param ContainerBuilder $container The service container
177
     *
178
     * @return void
179
     */
180
    protected function configureKnpMenuBundle(ContainerBuilder $container)
181
    {
182
        foreach (array_keys($container->getExtensions()) as $name) {
183
            switch ($name) {
184
                case 'knp_menu':
185
                    $container->prependExtensionConfig(
186
                        $name,
187
                        array('twig' => array('template'  => $this->menuTemplate))
188
                    );
189
                    break;
190
            }
191
        }
192
    }
193
194
    /**
195
     * @param ContainerBuilder $container The service container
196
     *
197
     * @return void
198
     */
199
    protected function configureKnpPaginatorBundle(ContainerBuilder $container)
200
    {
201
        foreach (array_keys($container->getExtensions()) as $name) {
202
            switch ($name) {
203
                case 'knp_paginator':
204
                    $container->prependExtensionConfig(
205
                        $name,
206
                        array('template' => array('pagination' => $this->paginationTemplate))
207
                    );
208
                    break;
209
            }
210
        }
211
    }
212
}
213