vendor/pimcore/pimcore/lib/Extension/Document/Areabrick/AreabrickManager.php line 167

Open in your IDE?
  1. <?php
  2. declare(strict_types 1);
  3. /**
  4.  * Pimcore
  5.  *
  6.  * This source file is available under two different licenses:
  7.  * - GNU General Public License version 3 (GPLv3)
  8.  * - Pimcore Commercial License (PCL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  14.  */
  15. namespace Pimcore\Extension\Document\Areabrick;
  16. use Pimcore\Extension;
  17. use Pimcore\Extension\Document\Areabrick\Exception\BrickNotFoundException;
  18. use Pimcore\Extension\Document\Areabrick\Exception\ConfigurationException;
  19. use Psr\Container\ContainerInterface;
  20. /**
  21.  * @internal
  22.  */
  23. class AreabrickManager implements AreabrickManagerInterface
  24. {
  25.     /**
  26.      * @deprecated
  27.      *
  28.      * @var Extension\Config
  29.      */
  30.     protected $config;
  31.     /**
  32.      * @var ContainerInterface
  33.      */
  34.     protected $container;
  35.     /**
  36.      * @var AreabrickInterface[]
  37.      */
  38.     protected $bricks = [];
  39.     /**
  40.      * @var array
  41.      */
  42.     protected $brickServiceIds = [];
  43.     /**
  44.      * @param Extension\Config $config
  45.      * @param ContainerInterface $container
  46.      */
  47.     public function __construct(Extension\Config $configContainerInterface $container)
  48.     {
  49.         $this->config $config;
  50.         $this->container $container;
  51.     }
  52.     /**
  53.      * {@inheritdoc}
  54.      */
  55.     public function register(string $idAreabrickInterface $brick)
  56.     {
  57.         if (array_key_exists($id$this->bricks)) {
  58.             throw new ConfigurationException(sprintf(
  59.                 'Areabrick %s is already registered as %s (trying to add %s)',
  60.                 $id,
  61.                 get_class($this->bricks[$id]),
  62.                 get_class($brick)
  63.             ));
  64.         }
  65.         if (array_key_exists($id$this->brickServiceIds)) {
  66.             throw new ConfigurationException(sprintf(
  67.                 'Areabrick %s is already registered as service %s (trying to add %s)',
  68.                 $id,
  69.                 $this->brickServiceIds[$id],
  70.                 get_class($brick)
  71.             ));
  72.         }
  73.         $brick->setId($id);
  74.         $this->bricks[$id] = $brick;
  75.     }
  76.     /**
  77.      * {@inheritdoc}
  78.      */
  79.     public function registerService(string $idstring $serviceId)
  80.     {
  81.         if (array_key_exists($id$this->bricks)) {
  82.             throw new ConfigurationException(sprintf(
  83.                 'Areabrick %s is already registered as %s (trying to add service %s)',
  84.                 $id,
  85.                 get_class($this->bricks[$id]),
  86.                 $serviceId
  87.             ));
  88.         }
  89.         if (array_key_exists($id$this->brickServiceIds)) {
  90.             throw new ConfigurationException(sprintf(
  91.                 'Areabrick %s is already registered as service %s (trying to add service %s)',
  92.                 $id,
  93.                 $this->brickServiceIds[$id],
  94.                 $serviceId
  95.             ));
  96.         }
  97.         $this->brickServiceIds[$id] = $serviceId;
  98.     }
  99.     /**
  100.      * {@inheritdoc}
  101.      */
  102.     public function getBrick(string $id): AreabrickInterface
  103.     {
  104.         $brick null;
  105.         if (isset($this->bricks[$id])) {
  106.             $brick $this->bricks[$id];
  107.         } else {
  108.             $brick $this->loadServiceBrick($id);
  109.         }
  110.         if (null === $brick) {
  111.             throw new BrickNotFoundException(sprintf('Areabrick %s is not registered'$id));
  112.         }
  113.         return $brick;
  114.     }
  115.     /**
  116.      * {@inheritdoc}
  117.      */
  118.     public function getBricks(): array
  119.     {
  120.         if (count($this->brickServiceIds) > 0) {
  121.             $this->loadServiceBricks();
  122.         }
  123.         return $this->bricks;
  124.     }
  125.     /**
  126.      * {@inheritdoc}
  127.      */
  128.     public function getBrickIds(): array
  129.     {
  130.         $ids array_merge(
  131.             array_keys($this->bricks),
  132.             array_keys($this->brickServiceIds)
  133.         );
  134.         return $ids;
  135.     }
  136.     /**
  137.      * Loads brick from container
  138.      *
  139.      * @param string $id
  140.      *
  141.      * @return AreabrickInterface|null
  142.      */
  143.     protected function loadServiceBrick(string $id)
  144.     {
  145.         if (!isset($this->brickServiceIds[$id])) {
  146.             return null;
  147.         }
  148.         $serviceId $this->brickServiceIds[$id];
  149.         if (!$this->container->has($id)) {
  150.             throw new ConfigurationException(sprintf(
  151.                 'Definition for areabrick %s (defined as service %s) does not exist',
  152.                 $id,
  153.                 $serviceId
  154.             ));
  155.         }
  156.         $brick $this->container->get($id);
  157.         if (!($brick instanceof AreabrickInterface)) {
  158.             throw new ConfigurationException(sprintf(
  159.                 'Definition for areabrick %s (defined as service %s) does not implement AreabrickInterface (got %s)',
  160.                 $id,
  161.                 $serviceId,
  162.                 is_object($brick) ? get_class($brick) : gettype($brick)
  163.             ));
  164.         }
  165.         $brick->setId($id);
  166.         // move brick to map of loaded bricks
  167.         unset($this->brickServiceIds[$id]);
  168.         $this->bricks[$id] = $brick;
  169.         return $brick;
  170.     }
  171.     /**
  172.      * Loads all brick instances registered as service definitions
  173.      */
  174.     protected function loadServiceBricks()
  175.     {
  176.         foreach ($this->brickServiceIds as $id => $serviceId) {
  177.             $this->loadServiceBrick($id);
  178.         }
  179.     }
  180.     /**
  181.      * @deprecated will be removed in Pimcore 11
  182.      *
  183.      * {@inheritdoc}
  184.      */
  185.     public function enable(string $id)
  186.     {
  187.         $this->setState($idtrue);
  188.     }
  189.     /**
  190.      * @deprecated will be removed in Pimcore 11
  191.      *
  192.      * {@inheritdoc}
  193.      */
  194.     public function disable(string $id)
  195.     {
  196.         $this->setState($idfalse);
  197.     }
  198.     /**
  199.      * @deprecated will be removed in Pimcore 11
  200.      *
  201.      * {@inheritdoc}
  202.      */
  203.     public function setState(string $idbool $state)
  204.     {
  205.         // load the brick to make sure it exists
  206.         $brick $this->getBrick($id);
  207.         $config $this->getBrickConfig();
  208.         if ($state) {
  209.             if (isset($config[$brick->getId()])) {
  210.                 unset($config[$brick->getId()]);
  211.             }
  212.         } else {
  213.             $config[$brick->getId()] = false;
  214.         }
  215.         $this->setBrickConfig($config);
  216.     }
  217.     /**
  218.      * @deprecated will be removed in Pimcore 11
  219.      *
  220.      * {@inheritdoc}
  221.      */
  222.     public function isEnabled(string $id): bool
  223.     {
  224.         $config $this->getBrickConfig();
  225.         $enabled true;
  226.         if (isset($config[$id]) && $config[$id] === false) {
  227.             $enabled false;
  228.         }
  229.         return $enabled;
  230.     }
  231.     /**
  232.      * @deprecated
  233.      *
  234.      * @return array
  235.      */
  236.     private function getBrickConfig()
  237.     {
  238.         $config $this->config->loadConfig();
  239.         if (isset($config->areabrick)) {
  240.             return $config->areabrick->toArray();
  241.         }
  242.         return [];
  243.     }
  244.     /**
  245.      * @deprecated
  246.      *
  247.      * @param array $config
  248.      */
  249.     private function setBrickConfig(array $config)
  250.     {
  251.         $cfg $this->config->loadConfig();
  252.         $cfg->areabrick $config;
  253.         $this->config->saveConfig($cfg);
  254.     }
  255. }