vendor/pimcore/pimcore/models/Staticroute/Dao.php line 32

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\Staticroute;
  15. use Pimcore\Config\LocationAwareConfigRepository;
  16. use Pimcore\Model;
  17. use Pimcore\Model\Exception\NotFoundException;
  18. use Symfony\Component\Uid\Uuid as Uid;
  19. /**
  20.  * @internal
  21.  *
  22.  * @property \Pimcore\Model\Staticroute $model
  23.  */
  24. class Dao extends Model\Dao\PimcoreLocationAwareConfigDao
  25. {
  26.     private const CONFIG_KEY 'staticroutes';
  27.     public function configure()
  28.     {
  29.         $config \Pimcore::getContainer()->getParameter('pimcore.config');
  30.         $storageConfig LocationAwareConfigRepository::getStorageConfigurationCompatibilityLayer(
  31.             $config,
  32.             self::CONFIG_KEY,
  33.             'PIMCORE_CONFIG_STORAGE_DIR_STATICROUTES',
  34.             'PIMCORE_WRITE_TARGET_STATICROUTES'
  35.         );
  36.         parent::configure([
  37.             'containerConfig' => $config['staticroutes']['definitions'],
  38.             'settingsStoreScope' => 'pimcore_staticroutes',
  39.             'storageDirectory' => $storageConfig,
  40.             'legacyConfigFile' => 'staticroutes.php',
  41.         ]);
  42.     }
  43.     /**
  44.      * Deletes object from database
  45.      */
  46.     public function delete()
  47.     {
  48.         $this->deleteData($this->model->getId());
  49.     }
  50.     /**
  51.      * @deprecated duplicate work, use the listing instead
  52.      *
  53.      * @return Model\Staticroute[]
  54.      */
  55.     public function getAll()
  56.     {
  57.         $list = new Model\Staticroute\Listing();
  58.         $list $list->load();
  59.         return $list;
  60.     }
  61.     /**
  62.      * Get the data for the object from database for the given id
  63.      *
  64.      * @param string|null $id
  65.      *
  66.      * @throws NotFoundException
  67.      */
  68.     public function getById($id null)
  69.     {
  70.         if ($id != null) {
  71.             $this->model->setId($id);
  72.         }
  73.         $data $this->getDataByName($this->model->getId());
  74.         if ($data && $id != null) {
  75.             $data['id'] = $id;
  76.         }
  77.         if ($data) {
  78.             $this->assignVariablesToModel($data);
  79.         } else {
  80.             throw new Model\Exception\NotFoundException(sprintf(
  81.                 'Static Route with ID "%s" does not exist.',
  82.                 $this->model->getId()
  83.             ));
  84.         }
  85.     }
  86.     /**
  87.      * @param string|null $name
  88.      * @param int|null $siteId
  89.      *
  90.      * @throws NotFoundException
  91.      */
  92.     public function getByName($name null$siteId null)
  93.     {
  94.         if ($name != null) {
  95.             $this->model->setName($name);
  96.         }
  97.         $name $this->model->getName();
  98.         $totalList = new Listing();
  99.         $totalList $totalList->load();
  100.         $data array_filter($totalList, function (Model\Staticroute $row) use ($name$siteId) {
  101.             if ($row->getName() == $name) {
  102.                 if (empty($row->getSiteId()) || in_array($siteId$row->getSiteId())) {
  103.                     return true;
  104.                 }
  105.             }
  106.             return false;
  107.         });
  108.         usort($data, function (Model\Staticroute $aModel\Staticroute $b) {
  109.             if ($a->getSiteId() == $b->getSiteId()) {
  110.                 return 0;
  111.             }
  112.             return ($a->getSiteId() < $b->getSiteId()) ? : -1;
  113.         });
  114.         if (count($data) && $data[0]->getId()) {
  115.             $this->assignVariablesToModel($data[0]->getObjectVars());
  116.         } else {
  117.             throw new NotFoundException(sprintf(
  118.                 'Static route config with name "%s" does not exist.',
  119.                 $this->model->getName()
  120.             ));
  121.         }
  122.     }
  123.     /**
  124.      * {@inheritdoc}
  125.      */
  126.     protected function prepareDataStructureForYaml(string $id$data)
  127.     {
  128.         return [
  129.             'pimcore' => [
  130.                 'staticroutes' => [
  131.                     'definitions' => [
  132.                         $id => $data,
  133.                     ],
  134.                 ],
  135.             ],
  136.         ];
  137.     }
  138.     /**
  139.      * @throws \Exception
  140.      */
  141.     public function save()
  142.     {
  143.         if (!$this->model->getId()) {
  144.             $this->model->setId(Uid::v4());
  145.         }
  146.         $ts time();
  147.         if (!$this->model->getCreationDate()) {
  148.             $this->model->setCreationDate($ts);
  149.         }
  150.         $this->model->setModificationDate($ts);
  151.         $dataRaw $this->model->getObjectVars();
  152.         $data = [];
  153.         $allowedProperties = ['name''pattern''reverse''controller',
  154.             'variables''defaults''siteId''priority''methods''creationDate''modificationDate', ];
  155.         foreach ($dataRaw as $key => $value) {
  156.             if (in_array($key$allowedProperties)) {
  157.                 $data[$key] = $value;
  158.             }
  159.         }
  160.         $this->saveData($this->model->getId(), $data);
  161.     }
  162. }