vendor/pimcore/pimcore/lib/Extension/Bundle/Installer/SettingsStoreAwareInstaller.php line 153

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Extension\Bundle\Installer;
  15. use Doctrine\Migrations\DependencyFactory;
  16. use Doctrine\Migrations\Version\Direction;
  17. use Doctrine\Migrations\Version\ExecutionResult;
  18. use Pimcore\Migrations\FilteredMigrationsRepository;
  19. use Pimcore\Migrations\FilteredTableMetadataStorage;
  20. use Pimcore\Model\Tool\SettingsStore;
  21. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  22. abstract class SettingsStoreAwareInstaller extends AbstractInstaller
  23. {
  24.     /**
  25.      * @var BundleInterface
  26.      */
  27.     protected $bundle;
  28.     /**
  29.      * @var FilteredMigrationsRepository
  30.      */
  31.     protected $migrationRepository;
  32.     /**
  33.      * @var FilteredTableMetadataStorage
  34.      */
  35.     protected $tableMetadataStorage;
  36.     /**
  37.      * @var DependencyFactory
  38.      */
  39.     protected $dependencyFactory;
  40.     public function __construct(BundleInterface $bundle)
  41.     {
  42.         parent::__construct();
  43.         $this->bundle $bundle;
  44.     }
  45.     /**
  46.      * @param FilteredMigrationsRepository $migrationRepository
  47.      *
  48.      * @required
  49.      */
  50.     public function setMigrationRepository(FilteredMigrationsRepository $migrationRepository): void
  51.     {
  52.         $this->migrationRepository $migrationRepository;
  53.     }
  54.     /**
  55.      * @param FilteredTableMetadataStorage $tableMetadataStorage
  56.      *
  57.      * @required
  58.      */
  59.     public function setTableMetadataStorage(FilteredTableMetadataStorage $tableMetadataStorage): void
  60.     {
  61.         $this->tableMetadataStorage $tableMetadataStorage;
  62.     }
  63.     /**
  64.      * @param DependencyFactory $dependencyFactory
  65.      *
  66.      * @required
  67.      */
  68.     public function setDependencyFactory(DependencyFactory $dependencyFactory): void
  69.     {
  70.         $this->dependencyFactory $dependencyFactory;
  71.     }
  72.     protected function getSettingsStoreInstallationId(): string
  73.     {
  74.         return 'BUNDLE_INSTALLED__' $this->bundle->getNamespace() . '\\' $this->bundle->getName();
  75.     }
  76.     public function getLastMigrationVersionClassName(): ?string
  77.     {
  78.         return null;
  79.     }
  80.     protected function markInstalled()
  81.     {
  82.         $migrationVersion $this->getLastMigrationVersionClassName();
  83.         if ($migrationVersion) {
  84.             $this->migrationRepository->setPrefix($this->bundle->getNamespace());
  85.             $this->tableMetadataStorage->setPrefix($this->bundle->getNamespace());
  86.             $migrations $this->dependencyFactory->getMigrationRepository()->getMigrations();
  87.             $executedMigrations $this->dependencyFactory->getMetadataStorage()->getExecutedMigrations();
  88.             foreach ($migrations->getItems() as $migration) {
  89.                 $version $migration->getVersion();
  90.                 if (!$executedMigrations->hasMigration($version)) {
  91.                     $migrationResult = new ExecutionResult($versionDirection::UP);
  92.                     $this->dependencyFactory->getMetadataStorage()->complete($migrationResult);
  93.                 }
  94.                 if ((string)$version === $migrationVersion) {
  95.                     break;
  96.                 }
  97.             }
  98.         }
  99.         SettingsStore::set($this->getSettingsStoreInstallationId(), true'bool''pimcore');
  100.     }
  101.     protected function markUninstalled()
  102.     {
  103.         SettingsStore::set($this->getSettingsStoreInstallationId(), false'bool''pimcore');
  104.         $migrationVersion $this->getLastMigrationVersionClassName();
  105.         if ($migrationVersion) {
  106.             $this->tableMetadataStorage->setPrefix($this->bundle->getNamespace());
  107.             $executedMigrations $this->dependencyFactory->getMetadataStorage()->getExecutedMigrations();
  108.             foreach ($executedMigrations->getItems() as $migration) {
  109.                 $migrationResult = new ExecutionResult($migration->getVersion(), Direction::DOWN);
  110.                 $this->dependencyFactory->getMetadataStorage()->complete($migrationResult);
  111.             }
  112.         }
  113.     }
  114.     public function install()
  115.     {
  116.         parent::install();
  117.         $this->markInstalled();
  118.     }
  119.     public function uninstall()
  120.     {
  121.         parent::uninstall();
  122.         $this->markUninstalled();
  123.     }
  124.     /**
  125.      * @return bool
  126.      */
  127.     public function isInstalled()
  128.     {
  129.         $installSetting SettingsStore::get($this->getSettingsStoreInstallationId(), 'pimcore');
  130.         return (bool) ($installSetting $installSetting->getData() : false);
  131.     }
  132.     /**
  133.      * @return bool
  134.      */
  135.     public function canBeInstalled()
  136.     {
  137.         return !$this->isInstalled();
  138.     }
  139.     /**
  140.      * @return bool
  141.      */
  142.     public function canBeUninstalled()
  143.     {
  144.         return $this->isInstalled();
  145.     }
  146. }