Completed
Push — master ( 60399b...890f86 )
by Craig
06:15
created

AbstractBundle::addStylesheet()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 6
nop 1
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
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 Zikula\Core;
13
14
use Symfony\Component\Console\Application;
15
use Symfony\Component\DependencyInjection\Container;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
use Symfony\Component\HttpKernel\Bundle\Bundle;
18
use Zikula\Bundle\CoreBundle\Bundle\Scanner;
19
use Zikula\Bundle\CoreBundle\Bundle\MetaData;
20
use Zikula\ThemeModule\AbstractTheme;
21
use Zikula\ThemeModule\Engine\AssetBag;
22
23
abstract class AbstractBundle extends Bundle
24
{
25
    const STATE_DISABLED = 2;
26
    const STATE_ACTIVE = 3;
27
    const STATE_MISSING = 6;
28
29
    protected $state;
30
31
    protected $booted = false;
32
33
    private $basePath;
34
35
    public function isBooted()
36
    {
37
        return $this->booted;
38
    }
39
40
    public function setState($state)
41
    {
42
        if (!in_array($state, [self::STATE_ACTIVE, self::STATE_DISABLED, self::STATE_MISSING])) {
43
            throw new \InvalidArgumentException(sprintf('Invalid state %s', $state));
44
        }
45
46
        $this->state = $state;
47
48
        return $this;
49
    }
50
51
    public function getState()
52
    {
53
        return $this->state;
54
    }
55
56
    public function getInstallerClass()
57
    {
58
        $ns = $this->getNamespace();
59
        $class = $ns.'\\'.substr($ns, strrpos($ns, '\\') + 1, strlen($ns)).'Installer';
60
61
        return $class;
62
    }
63
64
    public function getRoutingConfig()
65
    {
66
        return "@{$this->name}/Resources/config/routing.yml";
67
    }
68
69
    public function getTranslationDomain()
70
    {
71
        return strtolower($this->getName());
72
    }
73
74
    /**
75
     * Gets the translation domain path
76
     *
77
     * @return string
78
     */
79
    public function getLocalePath()
80
    {
81
        return $this->getPath().'/Resources/locale';
82
    }
83
84
    /**
85
     * Gets the views path
86
     *
87
     * @return string
88
     */
89
    public function getViewsPath()
90
    {
91
        return $this->getPath().'/Resources/views';
92
    }
93
94
    /**
95
     * Gets the config path.
96
     *
97
     * @return string
98
     */
99
    public function getConfigPath()
100
    {
101
        return $this->getPath().'/Resources/config';
102
    }
103
104
    /**
105
     * Get the assetpath relative to /web e.g. /modules/acmefoo
106
     * @return string
107
     */
108
    public function getRelativeAssetPath()
109
    {
110
        return strtolower($this->getNameType() . 's/' . substr($this->getName(), 0, -strlen($this->getNameType())));
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    public function getBasePath()
117
    {
118
        if (null === $this->basePath) {
119
            $ns = str_replace('\\', '/', $this->getNamespace());
120
            $path = str_replace('\\', '/', $this->getPath());
121
            $this->basePath = substr($path, 0, strrpos($path, $ns) - 1);
122
        }
123
124
        return $this->basePath;
125
    }
126
127
    public function getNameType()
128
    {
129
        return 'Bundle';
130
    }
131
132
    protected function hasCommands()
133
    {
134
        return false;
135
    }
136
137
    public function getContainerExtension()
138
    {
139
        $type = $this->getNameType();
140
        $typeLower = strtolower($type);
141
        if (null === $this->extension) {
142
            $basename = preg_replace('/'.$type.'/', '', $this->getName());
143
144
            $class = $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
145
            if (class_exists($class)) {
146
                $extension = new $class();
147
148
                // check naming convention
149
                $expectedAlias = Container::underscore($basename);
150
                if ($expectedAlias != $extension->getAlias()) {
151
                    throw new \LogicException(sprintf(
152
                        'The extension alias for the default extension of a %s must be the underscored version of the %s name ("%s" instead of "%s")',
153
                        $typeLower, $typeLower, $expectedAlias, $extension->getAlias()
154
                    ));
155
                }
156
157
                $this->extension = $extension;
158
            } else {
159
                $this->extension = false;
160
            }
161
        }
162
163
        if ($this->extension) {
164
            return $this->extension;
165
        }
166
    }
167
168
    public function registerCommands(Application $application)
169
    {
170
        if ($this->hasCommands()) {
171
            parent::registerCommands($application);
172
        }
173
    }
174
175
    /**
176
     * Get container.
177
     *
178
     * @return ContainerInterface
179
     */
180
    public function getContainer()
181
    {
182
        return $this->container;
183
    }
184
185
    /**
186
     * Add the bundle's stylesheet to the page assets
187
     * @param string $name
188
     */
189
    public function addStylesheet($name = 'style.css')
190
    {
191
        try {
192
            $styleSheet = $this->getContainer()->get('zikula_core.common.theme.asset_helper')->resolve('@' . $this->getName() . ":css/$name");
193
        } catch (\InvalidArgumentException $e) {
194
            $styleSheet = '';
195
        }
196
        if (!empty($styleSheet)) {
197
            $weight = $this instanceof AbstractTheme ? AssetBag::WEIGHT_THEME_STYLESHEET : AssetBag::WEIGHT_DEFAULT;
198
            $this->container->get('zikula_core.common.theme.assets_css')->add([$styleSheet => $weight]);
199
        }
200
    }
201
202
    /**
203
     * @return MetaData
204
     */
205
    public function getMetaData()
206
    {
207
        $scanner = new Scanner();
208
        $jsonPath = $this->getPath() . '/composer.json';
209
        $jsonContent = $scanner->decode($jsonPath);
210
        $metaData = new MetaData($jsonContent);
211
        if (!empty($this->container)) {
212
            $metaData->setTranslator($this->container->get('translator'));
213
        }
214
        if (!empty($this->container) && $this->container->getParameter('installed')) {
215
            // overwrite composer.json settings with dynamic values from extension repository
216
            $extensionEntity = $this->container->get('zikula_extensions_module.extension_repository')->get($this->getName());
217
            if (!is_null($extensionEntity)) {
218
                $metaData->setUrl($extensionEntity->getUrl());
219
                $metaData->setDisplayName($extensionEntity->getDisplayname());
220
                $metaData->setDescription($extensionEntity->getDescription());
221
            }
222
        }
223
224
        return $metaData;
225
    }
226
}
227