vendor/pimcore/data-hub/src/Configuration/Dao.php line 246

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\Bundle\DataHubBundle\Configuration;
  15. use Pimcore\Bundle\DataHubBundle\Configuration;
  16. use Pimcore\Config;
  17. use Pimcore\Model;
  18. use Symfony\Component\Uid\Uuid as Uid;
  19. /**
  20.  * Class Dao
  21.  *
  22.  * @package Pimcore\Bundle\DataHubBundle\Configuration
  23.  *
  24.  * @property Configuration $model
  25.  */
  26. class Dao extends Model\Dao\PimcoreLocationAwareConfigDao
  27. {
  28.     public const ROOT_PATH '/';
  29.     /**
  30.      * path to the configuration file
  31.      */
  32.     public const CONFIG_FILE 'datahub-configurations.php';
  33.     /**
  34.      * @var null|array
  35.      */
  36.     private static $_config null;
  37.     /**
  38.      * @deprecated Will be removed in Pimcore 11
  39.      */
  40.     private const LEGACY_FILE 'datahub-configurations.php';
  41.     /**
  42.      * @deprecated Will be removed as soon as Pimcore 10.6 isnĀ“t supported anymore.
  43.      */
  44.     public const CONFIG_PATH PIMCORE_CONFIGURATION_DIRECTORY '/data_hub';
  45.     public function configure(): void
  46.     {
  47.         $config \Pimcore::getContainer()->getParameter('pimcore_data_hub');
  48.         if (\Pimcore\Version::getMajorVersion() >= 11) {
  49.             $storageConfig $config['config_location']['data_hub'];
  50.             parent::configure([
  51.                 'containerConfig' => $config['configurations'] ?? [],
  52.                 'settingsStoreScope' => 'pimcore_data_hub',
  53.                 'storageConfig' => $storageConfig,
  54.             ]);
  55.         } else {
  56.             $storageConfig Config\LocationAwareConfigRepository::getStorageConfigurationCompatibilityLayer(
  57.                 $config,
  58.                 'data_hub',
  59.                 'PIMCORE_CONFIG_STORAGE_DIR_DATA_HUB',
  60.                 'PIMCORE_WRITE_TARGET_DATA_HUB'
  61.             );
  62.             parent::configure([
  63.                 'containerConfig' => $config['configurations'] ?? [],
  64.                 'settingsStoreScope' => 'pimcore_data_hub',
  65.                 'storageDirectory' => $storageConfig,
  66.                 'legacyConfigFile' => self::LEGACY_FILE,
  67.             ]);
  68.         }
  69.     }
  70.     /**
  71.      * save a configuration.
  72.      */
  73.     public function save(): void
  74.     {
  75.         if (!$this->model->getName()) {
  76.             $this->model->setName(Uid::v4());
  77.         }
  78.         $ts time();
  79.         if (!$this->model->getCreationDate()) {
  80.             $this->model->setCreationDate($ts);
  81.         }
  82.         $this->model->setModificationDate($ts);
  83.         $data $this->model->getObjectVars();
  84.         $this->saveData($this->model->getName(), $data);
  85.     }
  86.     /**
  87.      * delete a configuration.
  88.      */
  89.     public function delete(): void
  90.     {
  91.         $this->deleteData($this->model->getName());
  92.     }
  93.     /**
  94.      * @param array $data
  95.      *
  96.      * @return void
  97.      */
  98.     public function setVariables($data)
  99.     {
  100.         $this->model->setConfiguration($data);
  101.         $this->model->setName($data['general']['name'] ?? '');
  102.         $this->model->setType($data['general']['type'] ?? '');
  103.         $this->model->setPath($data['general']['path'] ?? '');
  104.         $this->model->setModificationDate($data['general']['modificationDate'] ?? null);
  105.         $this->model->setCreationDate($data['general']['createDate'] ?? null);
  106.         $this->model->setGroup($data['general']['group'] ?? '');
  107.     }
  108.     /**
  109.      * @internal
  110.      *
  111.      * gets a configuration by name.
  112.      *
  113.      * @param string $name
  114.      *
  115.      */
  116.     public function loadByName($name)
  117.     {
  118.         $data $this->getDataByName($name);
  119.         if (!$data) {
  120.             $data $this->getDataByName('list');
  121.             $data $data[$name] ?? null;
  122.         }
  123.         if ($data) {
  124.             $this->setVariables($data);
  125.         } else {
  126.             throw new Model\Exception\NotFoundException('Configuration with name: ' $name ' does not exist');
  127.         }
  128.     }
  129.     /**
  130.      * @deprecated Will be removed in Pimcore 11
  131.      *
  132.      * get a configuration by name.
  133.      *
  134.      * TODO: remove this static function and rename "loadByName" to "getByName"
  135.      *
  136.      * @param string $name
  137.      *
  138.      */
  139.     public static function getByName($name)
  140.     {
  141.         try {
  142.             $config = new Configuration(nullnull);
  143.             $config->getDao()->loadByName($name);
  144.             return $config;
  145.         } catch (\Pimcore\Model\Exception\NotFoundException $e) {
  146.             return null;
  147.         }
  148.     }
  149.     /**
  150.      *
  151.      * @return int
  152.      *
  153.      *@deprecated will be removed with pimcore 11
  154.      *
  155.      * get latest modification date of configuration file.
  156.      *
  157.      */
  158.     public static function getConfigModificationDate()
  159.     {
  160.         return 0;
  161.     }
  162.     /**
  163.      * get the whole configuration file content.
  164.      *
  165.      * @return array
  166.      */
  167.     private function &getConfig()
  168.     {
  169.         if (self::$_config) {
  170.             return self::$_config;
  171.         }
  172.         $config = [];
  173.         $list $this->loadIdList();
  174.         foreach ($list as $name) {
  175.             $data $this->getDataByName($name);
  176.             if ($this->dataSource !== Config\LocationAwareConfigRepository::LOCATION_SETTINGS_STORE
  177.                 && $this->dataSource !== Config\LocationAwareConfigRepository::LOCATION_SYMFONY_CONFIG) {
  178.                 if ($name === 'folders') {
  179.                     unset($data[$name]);
  180.                 } else {
  181.                     foreach ($data as $key => $legacyItem) {
  182.                         $config[$key] = $legacyItem;
  183.                     }
  184.                 }
  185.             } else {
  186.                 $config[$name] = $data;
  187.             }
  188.         }
  189.         self::$_config $config;
  190.         return self::$_config;
  191.     }
  192.     /**
  193.      * get the list of configurations.
  194.      *
  195.      */
  196.     public function loadList(): array
  197.     {
  198.         $list = [];
  199.         $configs = &$this->getConfig();
  200.         foreach ($configs as $item) {
  201.             $name $item['general']['name'];
  202.             $configuration Configuration::getByName($name);
  203.             $list[$name] = $configuration;
  204.         }
  205.         return $list;
  206.     }
  207.     /**
  208.      * @deprecated Will be removed in Pimcore 11
  209.      *
  210.      * get the list of configurations.
  211.      *
  212.      * TODO: remove this static function and rename "loadList" to "getList"
  213.      *
  214.      *
  215.      */
  216.     public static function getList(): array
  217.     {
  218.         $configuration = new Configuration(nullnull);
  219.         return $configuration->getDao()->loadList();
  220.     }
  221.     /**
  222.      * @param mixed $data
  223.      *
  224.      * @return array[][][]
  225.      */
  226.     protected function prepareDataStructureForYaml(string $id$data): mixed
  227.     {
  228.         return [
  229.             'pimcore_data_hub' => [
  230.                 'configurations' => [
  231.                     $id => $data,
  232.                 ],
  233.             ],
  234.         ];
  235.     }
  236. }