vendor/shopware/core/Framework/Plugin.php line 76

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework;
  3. use Shopware\Core\Framework\Log\Package;
  4. use Shopware\Core\Framework\Parameter\AdditionalBundleParameters;
  5. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  6. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  7. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  8. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  9. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  10. use Shopware\Core\Kernel;
  11. use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
  12. #[Package('core')]
  13. abstract class Plugin extends Bundle
  14. {
  15.     /**
  16.      * @var bool
  17.      */
  18.     private $active;
  19.     /**
  20.      * @var string
  21.      */
  22.     private $basePath;
  23.     /**
  24.      * @internal
  25.      */
  26.     final public function __construct(bool $activestring $basePath, ?string $projectDir null)
  27.     {
  28.         $this->active $active;
  29.         $this->basePath $basePath;
  30.         if ($projectDir && mb_strpos($this->basePath'/') !== 0) {
  31.             $this->basePath rtrim($projectDir'/') . '/' $this->basePath;
  32.         }
  33.         $this->path $this->computePluginClassPath();
  34.     }
  35.     final public function isActive(): bool
  36.     {
  37.         return $this->active;
  38.     }
  39.     public function install(InstallContext $installContext): void
  40.     {
  41.     }
  42.     public function postInstall(InstallContext $installContext): void
  43.     {
  44.     }
  45.     public function update(UpdateContext $updateContext): void
  46.     {
  47.     }
  48.     public function postUpdate(UpdateContext $updateContext): void
  49.     {
  50.     }
  51.     public function activate(ActivateContext $activateContext): void
  52.     {
  53.     }
  54.     public function deactivate(DeactivateContext $deactivateContext): void
  55.     {
  56.     }
  57.     public function uninstall(UninstallContext $uninstallContext): void
  58.     {
  59.     }
  60.     public function configureRoutes(RoutingConfigurator $routesstring $environment): void
  61.     {
  62.         if (!$this->isActive()) {
  63.             return;
  64.         }
  65.         parent::configureRoutes($routes$environment);
  66.     }
  67.     /**
  68.      * @return Bundle[]
  69.      */
  70.     public function getAdditionalBundles(AdditionalBundleParameters $parameters): array
  71.     {
  72.         return [];
  73.     }
  74.     /**
  75.      * By default the container is rebuild during plugin activation and deactivation to allow the plugin to access
  76.      * its own services. If you are absolutely sure you do not require this feature for you plugin you might want
  77.      * to overwrite this method and return false to improve the activation/deactivation of your plugin. This change will
  78.      * only have an affect in the system context (CLI)
  79.      */
  80.     public function rebuildContainer(): bool
  81.     {
  82.         return true;
  83.     }
  84.     /**
  85.      * Some plugins need to provide 3rd party dependencies.
  86.      * If needed, return true and Shopware will execute `composer require` during the plugin installation.
  87.      * When the plugins gets uninstalled, Shopware executes `composer remove`
  88.      */
  89.     public function executeComposerCommands(): bool
  90.     {
  91.         return false;
  92.     }
  93.     final public function removeMigrations(): void
  94.     {
  95.         // namespace should not start with `shopware`
  96.         if (str_starts_with(mb_strtolower($this->getMigrationNamespace()), 'shopware') && !str_starts_with(mb_strtolower($this->getMigrationNamespace()), 'shopware\commercial')) {
  97.             throw new \RuntimeException('Deleting Shopware migrations is not allowed');
  98.         }
  99.         $class addcslashes($this->getMigrationNamespace(), '\\_%') . '%';
  100.         Kernel::getConnection()->executeUpdate('DELETE FROM migration WHERE class LIKE :class', ['class' => $class]);
  101.     }
  102.     public function getBasePath(): string
  103.     {
  104.         return $this->basePath;
  105.     }
  106.     public function enrichPrivileges(): array
  107.     {
  108.         return [];
  109.     }
  110.     /**
  111.      * Used to configure the BaseUrl for the Admin Extension API
  112.      */
  113.     public function getAdminBaseUrl(): ?string
  114.     {
  115.         return null;
  116.     }
  117.     private function computePluginClassPath(): string
  118.     {
  119.         $canonicalizedPluginClassPath parent::getPath();
  120.         $canonicalizedPluginPath realpath($this->basePath);
  121.         if ($canonicalizedPluginPath !== false && mb_strpos($canonicalizedPluginClassPath$canonicalizedPluginPath) === 0) {
  122.             $relativePluginClassPath mb_substr($canonicalizedPluginClassPathmb_strlen($canonicalizedPluginPath));
  123.             return $this->basePath $relativePluginClassPath;
  124.         }
  125.         return parent::getPath();
  126.     }
  127. }