Completed
Push — master ( d0afaf...772e08 )
by Craig
05:16
created

AbstractBundle::getRelativeAssetPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - 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\Core;
15
16
use InvalidArgumentException;
17
use LogicException;
18
use Symfony\Component\Console\Application;
19
use Symfony\Component\DependencyInjection\Container;
20
use Symfony\Component\DependencyInjection\ContainerInterface;
21
use Symfony\Component\HttpKernel\Bundle\Bundle;
22
use Zikula\Bundle\CoreBundle\Bundle\MetaData;
23
use Zikula\Bundle\CoreBundle\Bundle\Scanner;
24
use Zikula\Common\Translator\Translator;
25
use Zikula\ExtensionsModule\Entity\Repository\ExtensionRepository;
26
use Zikula\ThemeModule\AbstractTheme;
27
use Zikula\ThemeModule\Engine\Asset;
28
use Zikula\ThemeModule\Engine\AssetBag;
29
30
abstract class AbstractBundle extends Bundle
31
{
32
    public function getInstallerClass(): string
33
    {
34
        $ns = $this->getNamespace();
35
        $class = $ns . '\\' . mb_substr($ns, mb_strrpos($ns, '\\') + 1, mb_strlen($ns)) . 'Installer';
36
37
        return $class;
38
    }
39
40
    public function getRoutingConfig(): string
41
    {
42
        return '@' . $this->name . '/Resources/config/routing.yml';
43
    }
44
45
    public function getTranslationDomain(): string
46
    {
47
        return mb_strtolower($this->getName());
48
    }
49
50
    /**
51
     * Gets the translation path.
52
     */
53
    public function getLocalePath(): string
54
    {
55
        return $this->getPath() . '/Resources/locale';
56
    }
57
58
    public function getViewsPath(): string
59
    {
60
        return $this->getPath() . '/Resources/views';
61
    }
62
63
    public function getConfigPath(): string
64
    {
65
        return $this->getPath() . '/Resources/config';
66
    }
67
68
    /**
69
     * Get the asset path relative to /web e.g. /modules/acmefoo.
70
     */
71
    public function getRelativeAssetPath(): string
72
    {
73
        return mb_strtolower($this->getNameType() . 's/' . mb_substr($this->getName(), 0, -mb_strlen($this->getNameType())));
74
    }
75
76
    public function getNameType(): string
77
    {
78
        return 'Bundle';
79
    }
80
81
    protected function hasCommands(): bool
82
    {
83
        return false;
84
    }
85
86
    public function getContainerExtension()
87
    {
88
        if (null === $this->extension) {
89
            $type = $this->getNameType();
90
            $typeLower = mb_strtolower($type);
91
            $basename = preg_replace('/' . $type . '/', '', $this->getName());
92
93
            $class = $this->getNamespace() . '\\DependencyInjection\\' . $basename . 'Extension';
94
            if (class_exists($class)) {
95
                $extension = new $class();
96
97
                // check naming convention
98
                $expectedAlias = Container::underscore($basename);
99
                if ($expectedAlias !== $extension->getAlias()) {
100
                    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()));
101
                }
102
103
                $this->extension = $extension;
104
            } else {
105
                $this->extension = false;
106
            }
107
        }
108
109
        if ($this->extension) {
110
            return $this->extension;
111
        }
112
    }
113
114
    public function registerCommands(Application $application): void
115
    {
116
        if ($this->hasCommands()) {
117
            parent::registerCommands($application);
118
        }
119
    }
120
121
    public function getContainer(): ContainerInterface
122
    {
123
        return $this->container;
124
    }
125
126
    /**
127
     * Add the bundle's stylesheet to the page assets.
128
     */
129
    public function addStylesheet(string $name = 'style.css'): void
130
    {
131
        try {
132
            $styleSheet = $this->getContainer()->get(Asset::class)->resolve('@' . $this->getName() . ":css/${name}");
133
        } catch (InvalidArgumentException $exception) {
134
            $styleSheet = '';
135
        }
136
        if (!empty($styleSheet)) {
137
            $weight = $this instanceof AbstractTheme ? AssetBag::WEIGHT_THEME_STYLESHEET : AssetBag::WEIGHT_DEFAULT;
138
            $this->container->get('zikula_core.common.theme.assets_css')->add([$styleSheet => $weight]);
139
        }
140
    }
141
142
    public function getMetaData(): MetaData
143
    {
144
        $scanner = new Scanner();
145
        $jsonPath = $this->getPath() . '/composer.json';
146
        $jsonContent = $scanner->decode($jsonPath);
147
        $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

147
        $metaData = new MetaData(/** @scrutinizer ignore-type */ $jsonContent);
Loading history...
148
        if (!empty($this->container)) {
149
            $metaData->setTranslator($this->container->get(Translator::class));
0 ignored issues
show
Bug introduced by
It seems like $this->container->get(Zi...ator\Translator::class) can also be of type null; however, parameter $translator of Zikula\Bundle\CoreBundle...taData::setTranslator() does only seem to accept Zikula\Common\Translator\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

149
            $metaData->setTranslator(/** @scrutinizer ignore-type */ $this->container->get(Translator::class));
Loading history...
150
        }
151
        if (!empty($this->container) && $this->container->getParameter('installed')) {
152
            // overwrite composer.json settings with dynamic values from extension repository
153
            $extensionEntity = $this->container->get(ExtensionRepository::class)->get($this->getName());
154
            if (null !== $extensionEntity) {
155
                $metaData->setUrl($extensionEntity->getUrl());
156
                $metaData->setDisplayName($extensionEntity->getDisplayname());
157
                $metaData->setDescription($extensionEntity->getDescription());
158
            }
159
        }
160
161
        return $metaData;
162
    }
163
}
164