-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathPlugin.php
More file actions
134 lines (110 loc) · 4.06 KB
/
Plugin.php
File metadata and controls
134 lines (110 loc) · 4.06 KB
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
<?php declare(strict_types=1);
namespace Shopware\Core\Framework;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\Framework\Plugin\PluginException;
use Shopware\Core\Kernel;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
#[Package('framework')]
abstract class Plugin extends Bundle
{
/**
* @internal
*/
final public function __construct(
private readonly bool $active,
private string $basePath,
?string $projectDir = null
) {
if ($projectDir && mb_strpos($this->basePath, '/') !== 0) {
$this->basePath = rtrim($projectDir, '/') . '/' . $this->basePath;
}
$this->path = $this->computePluginClassPath();
}
final public function isActive(): bool
{
return $this->active;
}
public function install(InstallContext $installContext): void
{
}
public function postInstall(InstallContext $installContext): void
{
}
public function update(UpdateContext $updateContext): void
{
}
public function postUpdate(UpdateContext $updateContext): void
{
}
public function activate(ActivateContext $activateContext): void
{
}
public function deactivate(DeactivateContext $deactivateContext): void
{
}
public function uninstall(UninstallContext $uninstallContext): void
{
}
public function configureRoutes(RoutingConfigurator $routes, string $environment): void
{
if (!$this->isActive()) {
return;
}
parent::configureRoutes($routes, $environment);
}
/**
* By default the container is rebuild during plugin activation and deactivation to allow the plugin to access
* its own services. If you are absolutely sure you do not require this feature for you plugin you might want
* to overwrite this method and return false to improve the activation/deactivation of your plugin. This change will
* only have an affect in the system context (CLI)
*/
public function rebuildContainer(): bool
{
return true;
}
/**
* Some plugins need to provide 3rd party dependencies.
* If needed, return true and Shopware will execute `composer require` during the plugin installation.
* When the plugins gets uninstalled, Shopware executes `composer remove`
*/
public function executeComposerCommands(): bool
{
return false;
}
public function removeMigrations(): void
{
// namespace should not start with `shopware`
if (str_starts_with(mb_strtolower($this->getMigrationNamespace()), 'shopware') && !str_starts_with(mb_strtolower($this->getMigrationNamespace()), 'shopware\commercial')) {
throw PluginException::cannotDeleteShopwareMigrations();
}
$class = addcslashes($this->getMigrationNamespace(), '\\_%') . '%';
Kernel::getConnection()->executeStatement('DELETE FROM migration WHERE class LIKE :class', ['class' => $class]);
}
public function getBasePath(): string
{
return $this->basePath;
}
/**
* @return array<string, list<string>>
*/
public function enrichPrivileges(): array
{
return [];
}
private function computePluginClassPath(): string
{
$canonicalizedPluginClassPath = $this->getPath();
$canonicalizedPluginPath = realpath($this->basePath);
if ($canonicalizedPluginPath !== false && mb_strpos($canonicalizedPluginClassPath, $canonicalizedPluginPath) === 0) {
$relativePluginClassPath = mb_substr($canonicalizedPluginClassPath, mb_strlen($canonicalizedPluginPath));
return Path::join($this->basePath, $relativePluginClassPath);
}
return $canonicalizedPluginClassPath;
}
}