Completed
Push — master ( 3ead92...7a85fc )
by Axel
13:10 queued 07:59
created

AbstractExtension::getRoutingConfig()   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 - 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
    public function getContainerExtension()
74
    {
75
        if (null === $this->extension) {
76
            $type = $this->getNameType();
77
            $typeLower = mb_strtolower($type);
78
            $basename = preg_replace('/' . $type . '/', '', $this->getName());
79
80
            $class = $this->getNamespace() . '\\DependencyInjection\\' . $basename . 'Extension';
81
            if (class_exists($class)) {
82
                $extension = new $class();
83
84
                // check naming convention
85
                $expectedAlias = Container::underscore($basename);
86
                if ($expectedAlias !== $extension->getAlias()) {
87
                    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()));
88
                }
89
90
                $this->extension = $extension;
91
            } else {
92
                $this->extension = false;
93
            }
94
        }
95
96
        return $this->extension ?: null;
97
    }
98
99
    public function getContainer(): ContainerInterface
100
    {
101
        return $this->container;
102
    }
103
104
    /**
105
     * Add the bundle's stylesheet to the page assets.
106
     */
107
    public function addStylesheet(string $name = 'style.css'): void
108
    {
109
        try {
110
            $styleSheet = $this->getContainer()->get(Asset::class)->resolve('@' . $this->getName() . ":css/${name}");
111
        } catch (InvalidArgumentException $exception) {
112
            $styleSheet = '';
113
        }
114
        if (!empty($styleSheet)) {
115
            $weight = $this instanceof AbstractTheme ? AssetBag::WEIGHT_THEME_STYLESHEET : AssetBag::WEIGHT_DEFAULT;
116
            $this->container->get('zikula_core.common.theme.assets_css')->add([$styleSheet => $weight]);
117
        }
118
    }
119
120
    public function getMetaData(): MetaData
121
    {
122
        $scanner = new Scanner();
123
        $jsonPath = $this->getPath() . '/composer.json';
124
        $jsonContent = $scanner->decode($jsonPath);
125
        $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

125
        $metaData = new MetaData(/** @scrutinizer ignore-type */ $jsonContent);
Loading history...
126
        if (!empty($this->container)) {
127
            $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

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