Completed
Push — master ( 0df7dc...5bc06d )
by Craig
05:43
created

AbstractBundle   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 204
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 204
rs 10
c 0
b 0
f 0
wmc 29
lcom 2
cbo 2

18 Methods

Rating   Name   Duplication   Size   Complexity  
A isBooted() 0 4 1
A setState() 0 10 2
A getState() 0 4 1
A getInstallerClass() 0 7 1
A getRoutingConfig() 0 4 1
A getTranslationDomain() 0 4 1
A getLocalePath() 0 4 1
A getViewsPath() 0 4 1
A getConfigPath() 0 4 1
A getRelativeAssetPath() 0 4 1
A getRelativePath() 0 7 1
A getBasePath() 0 10 2
A getNameType() 0 4 1
A hasCommands() 0 4 1
B getContainerExtension() 0 30 5
A registerCommands() 0 6 2
A getContainer() 0 4 1
B getMetaData() 0 22 5
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
21
abstract class AbstractBundle extends Bundle
22
{
23
    const STATE_DISABLED = 2;
24
    const STATE_ACTIVE = 3;
25
    const STATE_MISSING = 6;
26
27
    protected $state;
28
29
    protected $booted = false;
30
31
    private $basePath;
32
33
    public function isBooted()
34
    {
35
        return $this->booted;
36
    }
37
38
    public function setState($state)
39
    {
40
        if (!in_array($state, [self::STATE_ACTIVE, self::STATE_DISABLED, self::STATE_MISSING])) {
41
            throw new \InvalidArgumentException(sprintf('Invalid state %s', $state));
42
        }
43
44
        $this->state = $state;
45
46
        return $this;
47
    }
48
49
    public function getState()
50
    {
51
        return $this->state;
52
    }
53
54
    public function getInstallerClass()
55
    {
56
        $ns = $this->getNamespace();
57
        $class = $ns.'\\'.substr($ns, strrpos($ns, '\\') + 1, strlen($ns)).'Installer';
58
59
        return $class;
60
    }
61
62
    public function getRoutingConfig()
63
    {
64
        return "@{$this->name}/Resources/config/routing.yml";
65
    }
66
67
    public function getTranslationDomain()
68
    {
69
        return strtolower($this->getName());
70
    }
71
72
    /**
73
     * Gets the translation domain path
74
     *
75
     * @return string
76
     */
77
    public function getLocalePath()
78
    {
79
        return $this->getPath().'/Resources/locale';
80
    }
81
82
    /**
83
     * Gets the views path
84
     *
85
     * @return string
86
     */
87
    public function getViewsPath()
88
    {
89
        return $this->getPath().'/Resources/views';
90
    }
91
92
    /**
93
     * Gets the config path.
94
     *
95
     * @return string
96
     */
97
    public function getConfigPath()
98
    {
99
        return $this->getPath().'/Resources/config';
100
    }
101
102
    /**
103
     * Get the assetpath relative to /web e.g. /modules/acmefoo
104
     * @return string
105
     */
106
    public function getRelativeAssetPath()
107
    {
108
        return strtolower($this->getNameType() . 's/' . substr($this->getName(), 0, -strlen($this->getNameType())));
109
    }
110
111
    /**
112
     * @return string
113
     *
114
     * @todo remove (drak)
115
     * @deprecated This is just a workaround
116
     *
117
     * @internal This is just required until the transition is over fully to Symfony
118
     */
119
    public function getRelativePath()
120
    {
121
        $path = str_replace('\\', '/', $this->getPath());
122
        preg_match('#/(modules|system|themes)/#', $path, $matches);
123
124
        return substr($path, strpos($path, $matches[1]), strlen($path));
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function getBasePath()
131
    {
132
        if (null === $this->basePath) {
133
            $ns = str_replace('\\', '/', $this->getNamespace());
134
            $path = str_replace('\\', '/', $this->getPath());
135
            $this->basePath = substr($path, 0, strrpos($path, $ns) - 1);
136
        }
137
138
        return $this->basePath;
139
    }
140
141
    public function getNameType()
142
    {
143
        return 'Bundle';
144
    }
145
146
    protected function hasCommands()
147
    {
148
        return false;
149
    }
150
151
    public function getContainerExtension()
152
    {
153
        $type = $this->getNameType();
154
        $typeLower = strtolower($type);
155
        if (null === $this->extension) {
156
            $basename = preg_replace('/'.$type.'/', '', $this->getName());
157
158
            $class = $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
159
            if (class_exists($class)) {
160
                $extension = new $class();
161
162
                // check naming convention
163
                $expectedAlias = Container::underscore($basename);
164
                if ($expectedAlias != $extension->getAlias()) {
165
                    throw new \LogicException(sprintf(
166
                        'The extension alias for the default extension of a %s must be the underscored version of the %s name ("%s" instead of "%s")',
167
                        $typeLower, $typeLower, $expectedAlias, $extension->getAlias()
168
                    ));
169
                }
170
171
                $this->extension = $extension;
172
            } else {
173
                $this->extension = false;
174
            }
175
        }
176
177
        if ($this->extension) {
178
            return $this->extension;
179
        }
180
    }
181
182
    public function registerCommands(Application $application)
183
    {
184
        if ($this->hasCommands()) {
185
            parent::registerCommands($application);
186
        }
187
    }
188
189
    /**
190
     * Get container.
191
     *
192
     * @return ContainerInterface
193
     */
194
    public function getContainer()
195
    {
196
        return $this->container;
197
    }
198
199
    /**
200
     * @return MetaData
201
     */
202
    public function getMetaData()
203
    {
204
        $scanner = new Scanner();
205
        $jsonPath = $this->getPath() . '/composer.json';
206
        $jsonContent = $scanner->decode($jsonPath);
207
        $metaData = new MetaData($jsonContent);
208
        if (!empty($this->container)) {
209
            $metaData->setTranslator($this->container->get('translator'));
210
        }
211
        $metaData->setDirectoryFromBundle($this);
212
        if (!empty($this->container) && $this->container->getParameter('installed')) {
213
            // overwrite composer.json settings with dynamic values from extension repository
214
            $extensionEntity = $this->container->get('zikula_extensions_module.extension_repository')->get($this->getName());
215
            if (!is_null($extensionEntity)) {
216
                $metaData->setUrl($extensionEntity->getUrl());
217
                $metaData->setDisplayName($extensionEntity->getDisplayname());
218
                $metaData->setDescription($extensionEntity->getDescription());
219
            }
220
        }
221
222
        return $metaData;
223
    }
224
}
225