Completed
Push — master ( c06421...915b7c )
by Axel
05:50
created

AbstractExtension::getBaseName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\ExtensionsModule;
15
16
use InvalidArgumentException;
17
use LogicException;
18
use Symfony\Component\DependencyInjection\Container;
19
use Symfony\Component\DependencyInjection\ContainerInterface;
20
use Symfony\Component\HttpKernel\Bundle\Bundle;
21
use Zikula\Bundle\CoreBundle\Composer\MetaData;
22
use Zikula\Bundle\CoreBundle\Composer\Scanner;
23
use Zikula\ExtensionsModule\Entity\Repository\ExtensionRepository;
24
use Zikula\ThemeModule\Engine\Asset;
25
use Zikula\ThemeModule\Engine\AssetBag;
26
27
abstract class AbstractExtension extends Bundle
28
{
29
    public function getInstallerClass(): string
30
    {
31
        $ns = $this->getNamespace();
32
        $class = $ns . '\\' . mb_substr($ns, mb_strrpos($ns, '\\') + 1, mb_strlen($ns)) . 'Installer';
33
34
        return $class;
35
    }
36
37
    public function getRoutingConfig(): string
38
    {
39
        return '@' . $this->name . '/Resources/config/routing.yaml';
40
    }
41
42
    /**
43
     * Gets the translation path.
44
     */
45
    public function getLocalePath(): string
46
    {
47
        return $this->getPath() . '/Resources/locale';
48
    }
49
50
    public function getViewsPath(): string
51
    {
52
        return $this->getPath() . '/Resources/views';
53
    }
54
55
    public function getConfigPath(): string
56
    {
57
        return $this->getPath() . '/Resources/config';
58
    }
59
60
    /**
61
     * Get the asset path relative to /public e.g. /modules/acmefoo.
62
     */
63
    public function getRelativeAssetPath(): string
64
    {
65
        return mb_strtolower($this->getNameType() . 's/' . mb_substr($this->getName(), 0, -mb_strlen($this->getNameType())));
66
    }
67
68
    public function getNameType(): string
69
    {
70
        return 'Bundle';
71
    }
72
73
    protected function getBaseName()
74
    {
75
        return preg_replace('/' . $this->getNameType() . '$/', '', $this->getName());
76
    }
77
78
    protected function getContainerExtensionClass()
79
    {
80
        return $this->getNamespace() . '\\DependencyInjection\\' . $this->getBaseName() . 'Extension';
81
    }
82
83
    public function getContainerExtension()
84
    {
85
        if (null === $this->extension) {
86
            $extension = $this->createContainerExtension();
87
88
            if (null !== $extension) {
89
                // check naming convention
90
                $basename = $this->getBaseName();
91
                $expectedAlias = Container::underscore($basename);
92
93
                if ($expectedAlias !== $extension->getAlias()) {
94
                    throw new LogicException(sprintf('The extension alias for the default extension of a %s must be the underscored version of the %s name ("%s" instead of "%s")', $typeLower, $typeLower, $expectedAlias, $extension->getAlias()));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $typeLower seems to be never defined.
Loading history...
95
                }
96
97
                $this->extension = $extension;
98
            } else {
99
                $this->extension = false;
100
            }
101
        }
102
103
        return $this->extension ?: null;
104
    }
105
106
    public function getContainer(): ContainerInterface
107
    {
108
        return $this->container;
109
    }
110
111
    /**
112
     * Add the bundle's stylesheet to the page assets.
113
     */
114
    public function addStylesheet(string $name = 'style.css'): void
115
    {
116
        try {
117
            $styleSheet = $this->getContainer()->get(Asset::class)->resolve('@' . $this->getName() . ":css/${name}");
118
        } catch (InvalidArgumentException $exception) {
119
            $styleSheet = '';
120
        }
121
        if (!empty($styleSheet)) {
122
            $weight = $this instanceof AbstractTheme ? AssetBag::WEIGHT_THEME_STYLESHEET : AssetBag::WEIGHT_DEFAULT;
123
            $this->container->get('zikula_core.common.theme.assets_css')->add([$styleSheet => $weight]);
124
        }
125
    }
126
127
    public function getMetaData(): MetaData
128
    {
129
        $scanner = new Scanner();
130
        $jsonPath = $this->getPath() . '/composer.json';
131
        $jsonContent = $scanner->decode($jsonPath);
132
        $metaData = new MetaData($jsonContent);
0 ignored issues
show
Bug introduced by
It seems like $jsonContent can also be of type boolean; however, parameter $json of Zikula\Bundle\CoreBundle...MetaData::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

132
        $metaData = new MetaData(/** @scrutinizer ignore-type */ $jsonContent);
Loading history...
133
        if (!empty($this->container)) {
134
            $metaData->setTranslator($this->container->get('translator'));
0 ignored issues
show
Bug introduced by
It seems like $this->container->get('translator') can also be of type null; however, parameter $translator of Zikula\Bundle\CoreBundle...taData::setTranslator() does only seem to accept Symfony\Contracts\Translation\TranslatorInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

134
            $metaData->setTranslator(/** @scrutinizer ignore-type */ $this->container->get('translator'));
Loading history...
135
        }
136
        if (!empty($this->container) && '0.0.0' !== $this->container->getParameter('installed')) {
137
            // overwrite composer.json settings with dynamic values from extension repository
138
            $extensionEntity = $this->container->get(ExtensionRepository::class)->get($this->getName());
139
            if (null !== $extensionEntity) {
140
                $metaData->setUrl($extensionEntity->getUrl());
141
                $metaData->setDisplayName($extensionEntity->getDisplayname());
142
                $metaData->setDescription($extensionEntity->getDescription());
143
                $metaData->setIcon($extensionEntity->getIcon());
144
            }
145
        }
146
147
        return $metaData;
148
    }
149
}
150