Completed
Push — master ( 121b59...921fdf )
by Craig
07:11
created

AbstractBundle::getInstallerClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 9.4285
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
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 View Code Duplication
        if (!in_array($state, [self::STATE_ACTIVE, self::STATE_DISABLED, self::STATE_MISSING])) {
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...
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
    public function getBasePath()
115
    {
116
        if (null === $this->basePath) {
117
            $ns = str_replace('\\', '/', $this->getNamespace());
118
            $path = str_replace('\\', '/', $this->getPath());
119
            $this->basePath = substr($path, 0, strrpos($path, $ns) - 1);
120
        }
121
122
        return $this->basePath;
123
    }
124
125
    public function getNameType()
126
    {
127
        return 'Bundle';
128
    }
129
130
    protected function hasCommands()
131
    {
132
        return false;
133
    }
134
135
    public function getContainerExtension()
136
    {
137
        $type = $this->getNameType();
138
        $typeLower = strtolower($type);
139
        if (null === $this->extension) {
140
            $basename = preg_replace('/'.$type.'/', '', $this->getName());
141
142
            $class = $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
143
            if (class_exists($class)) {
144
                $extension = new $class();
145
146
                // check naming convention
147
                $expectedAlias = Container::underscore($basename);
148
                if ($expectedAlias != $extension->getAlias()) {
149
                    throw new \LogicException(sprintf(
150
                        'The extension alias for the default extension of a %s must be the underscored version of the %s name ("%s" instead of "%s")',
151
                        $typeLower, $typeLower, $expectedAlias, $extension->getAlias()
152
                    ));
153
                }
154
155
                $this->extension = $extension;
156
            } else {
157
                $this->extension = false;
158
            }
159
        }
160
161
        if ($this->extension) {
162
            return $this->extension;
163
        }
164
    }
165
166
    public function registerCommands(Application $application)
167
    {
168
        if ($this->hasCommands()) {
169
            parent::registerCommands($application);
170
        }
171
    }
172
173
    /**
174
     * Get container.
175
     *
176
     * @return ContainerInterface
177
     */
178
    public function getContainer()
179
    {
180
        return $this->container;
181
    }
182
183
    /**
184
     * @return MetaData
185
     */
186
    public function getMetaData()
187
    {
188
        $scanner = new Scanner();
189
        $jsonPath = $this->getPath() . '/composer.json';
190
        $jsonContent = $scanner->decode($jsonPath);
191
        $metaData = new MetaData($jsonContent);
192
        if (!empty($this->container)) {
193
            $metaData->setTranslator($this->container->get('translator'));
194
        }
195
        if (!empty($this->container) && $this->container->getParameter('installed')) {
196
            // overwrite composer.json settings with dynamic values from extension repository
197
            $extensionEntity = $this->container->get('zikula_extensions_module.extension_repository')->get($this->getName());
198
            if (!is_null($extensionEntity)) {
199
                $metaData->setUrl($extensionEntity->getUrl());
200
                $metaData->setDisplayName($extensionEntity->getDisplayname());
201
                $metaData->setDescription($extensionEntity->getDescription());
202
            }
203
        }
204
205
        return $metaData;
206
    }
207
}
208