vendor/pimcore/pimcore/lib/Model/Dao/PimcoreLocationAwareConfigDao.php line 89

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\Model\Dao;
  15. use Pimcore\Config;
  16. abstract class PimcoreLocationAwareConfigDao implements DaoInterface
  17. {
  18.     use DaoTrait;
  19.     private static array $cache = [];
  20.     protected ?string $settingsStoreScope null;
  21.     protected ?string $dataSource null;
  22.     private ?string $id null;
  23.     private Config\LocationAwareConfigRepository $locationAwareConfigRepository;
  24.     /**
  25.      * {@inheritdoc}
  26.      */
  27.     public function configure()
  28.     {
  29.         $params func_get_arg(0);
  30.         $this->settingsStoreScope $params['settingsStoreScope'] ?? 'pimcore_config';
  31.         if (!isset(self::$cache[$this->settingsStoreScope])) {
  32.             // initialize runtime cache
  33.             self::$cache[$this->settingsStoreScope] = [];
  34.         }
  35.         $this->locationAwareConfigRepository = new Config\LocationAwareConfigRepository(
  36.             $params['containerConfig'] ?? [],
  37.             $this->settingsStoreScope,
  38.             $params['storageDirectory'] ?? null,
  39.             $params['writeTargetEnvVariableName'] ?? null,
  40.             $params['defaultWriteLocation'] ?? null,
  41.             $params['legacyConfigFile'] ?? null
  42.         );
  43.     }
  44.     /**
  45.      * @param string $id
  46.      *
  47.      * @return mixed
  48.      */
  49.     protected function getDataByName(string $id)
  50.     {
  51.         $this->id $id;
  52.         if (isset(self::$cache[$this->settingsStoreScope][$id])) {
  53.             $this->dataSource self::$cache[$this->settingsStoreScope][$id]['datasource'];
  54.             return self::$cache[$this->settingsStoreScope][$id]['data'];
  55.         }
  56.         list($data$this->dataSource) = $this->locationAwareConfigRepository->loadConfigByKey($id);
  57.         if ($data) {
  58.             self::$cache[$this->settingsStoreScope][$id] = [
  59.                 'datasource' => $this->dataSource,
  60.                 'data' => $data,
  61.             ];
  62.         }
  63.         return $data;
  64.     }
  65.     /**
  66.      * @return array
  67.      */
  68.     protected function loadIdList(): array
  69.     {
  70.         return $this->locationAwareConfigRepository->fetchAllKeys();
  71.     }
  72.     /**
  73.      * Removes config with corresponding id from the cache.
  74.      * A new cache entry will be generated upon requesting the config again.
  75.      *
  76.      * @param string $id
  77.      */
  78.     protected function invalidateCache(string $id): void
  79.     {
  80.         unset(self::$cache[$this->settingsStoreScope][$id]);
  81.     }
  82.     /**
  83.      * @param string $id
  84.      * @param array $data
  85.      *
  86.      * @throws \Exception
  87.      */
  88.     protected function saveData(string $id$data)
  89.     {
  90.         $dao $this;
  91.         $this->invalidateCache($id);
  92.         $this->locationAwareConfigRepository->saveConfig($id$data, function ($id$data) use ($dao) {
  93.             return $dao->prepareDataStructureForYaml($id$data);
  94.         });
  95.     }
  96.     /**
  97.      * Hook to prepare config data structure for yaml
  98.      *
  99.      * @param string $id
  100.      * @param mixed $data
  101.      *
  102.      * @return mixed
  103.      */
  104.     protected function prepareDataStructureForYaml(string $id$data)
  105.     {
  106.         return $data;
  107.     }
  108.     /**
  109.      * @return string Can be either yaml (var/config/...) or "settings-store". defaults to "yaml"
  110.      *
  111.      * @throws \Exception
  112.      */
  113.     public function getWriteTarget(): string
  114.     {
  115.         return $this->locationAwareConfigRepository->getWriteTarget();
  116.     }
  117.     /**
  118.      * @return bool
  119.      */
  120.     public function isWriteable(): bool
  121.     {
  122.         return $this->locationAwareConfigRepository->isWriteable($this->id$this->dataSource);
  123.     }
  124.     /**
  125.      * @param string $id
  126.      *
  127.      * @throws \Exception
  128.      */
  129.     protected function deleteData(string $id): void
  130.     {
  131.         $this->invalidateCache($id);
  132.         $this->locationAwareConfigRepository->deleteData($id$this->dataSource);
  133.     }
  134. }