This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractExtension.php
140 lines (118 loc) · 4.39 KB
/
AbstractExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
declare(strict_types=1);
/*
* This file is part of the Zikula package.
*
* Copyright Zikula - https://ziku.la/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zikula\ExtensionsModule;
use InvalidArgumentException;
use LogicException;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use function Symfony\Component\String\s;
use Zikula\Bundle\CoreBundle\Composer\MetaData;
use Zikula\Bundle\CoreBundle\Composer\Scanner;
use Zikula\ExtensionsModule\Helper\MetaDataHelper;
use Zikula\ThemeModule\Engine\Asset;
use Zikula\ThemeModule\Engine\AssetBag;
abstract class AbstractExtension extends Bundle
{
public function getInstallerClass(): string
{
$ns = $this->getNamespace();
$installerName = s($ns)->afterLast('\\')->append('Installer');
$class = $ns . '\\' . $installerName->toString();
return $class;
}
public function getRoutingConfig(): string
{
return '@' . $this->name . '/Resources/config/routing.yaml';
}
/**
* Gets the translation path.
*/
public function getLocalePath(): string
{
return $this->getPath() . '/Resources/locale';
}
public function getViewsPath(): string
{
return $this->getPath() . '/Resources/views';
}
public function getConfigPath(): string
{
return $this->getPath() . '/Resources/config';
}
/**
* Get the asset path relative to /public e.g. /modules/acmefoo.
*/
public function getRelativeAssetPath(): string
{
$folder = $this->getNameType() . 's/';
$name = s($this->getName())->beforeLast($this->getNameType());
return s($folder . $name)->lower()->toString();
}
public function getNameType(): string
{
return 'Bundle';
}
public function getContainerExtension()
{
if (null === $this->extension) {
$type = $this->getNameType();
$typeLower = mb_strtolower($type);
$basename = preg_replace('/' . $type . '/', '', $this->getName());
$class = $this->getNamespace() . '\\DependencyInjection\\' . $basename . 'Extension';
if (class_exists($class)) {
$extension = new $class();
// check naming convention
$expectedAlias = Container::underscore($basename);
if ($expectedAlias !== $extension->getAlias()) {
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()));
}
$this->extension = $extension;
} else {
$this->extension = false;
}
}
return $this->extension ?: null;
}
public function getContainer(): ContainerInterface
{
return $this->container;
}
/**
* Add the bundle's stylesheet to the page assets.
*/
public function addStylesheet(string $name = 'style.css'): void
{
try {
$styleSheet = $this->getContainer()->get(Asset::class)->resolve('@' . $this->getName() . ":css/${name}");
} catch (InvalidArgumentException $exception) {
$styleSheet = '';
}
if (!empty($styleSheet)) {
$weight = $this instanceof AbstractTheme ? AssetBag::WEIGHT_THEME_STYLESHEET : AssetBag::WEIGHT_DEFAULT;
$this->container->get('zikula_core.common.theme.assets_css')->add([$styleSheet => $weight]);
}
}
public function getMetaData(bool $applyDynamicSettings = true): MetaData
{
$scanner = new Scanner();
$jsonPath = $this->getPath() . '/composer.json';
$jsonContent = $scanner->decode($jsonPath);
$metaData = new MetaData($jsonContent);
if (!empty($this->container) && $this->container->has('translator')) {
$metaData->setTranslator($this->container->get('translator'));
}
if (!empty($this->container) && $this->container->has(MetaDataHelper::class) && $applyDynamicSettings) {
$metaData = $this->container->get(MetaDataHelper::class)->setDynamicMetaData($metaData);
}
return $metaData;
}
}