vendor/sulu/sulu/src/Sulu/Component/Webspace/Settings/SettingsManager.php line 72

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Component\Webspace\Settings;
  11. use PHPCR\NodeInterface;
  12. use Sulu\Bundle\DocumentManagerBundle\Session\SessionManagerInterface;
  13. use Sulu\Component\PHPCR\SessionManager\SessionManagerInterface as DeprecatedSessionManagerInterface;
  14. /**
  15.  * Manages settings on top the webspace node.
  16.  */
  17. class SettingsManager implements SettingsManagerInterface
  18. {
  19.     /**
  20.      * @var SessionManagerInterface
  21.      */
  22.     private $sessionManager;
  23.     /**
  24.      * @var DeprecatedSessionManagerInterface
  25.      */
  26.     private $deprecatedSessionManager;
  27.     public function __construct(
  28.         SessionManagerInterface $sessionManager,
  29.         DeprecatedSessionManagerInterface $deprecatedSessionManager
  30.     ) {
  31.         $this->sessionManager $sessionManager;
  32.         $this->deprecatedSessionManager $deprecatedSessionManager;
  33.     }
  34.     public function save($webspaceKey$key$data)
  35.     {
  36.         $propertyName $this->getPropertyName($key);
  37.         $value $data;
  38.         if (!($data instanceof NodeInterface)) {
  39.             $value \json_encode($data);
  40.         }
  41.         $this->sessionManager->setNodeProperty(
  42.             $this->deprecatedSessionManager->getWebspacePath($webspaceKey),
  43.             $propertyName,
  44.             $value
  45.         );
  46.         $this->sessionManager->flush();
  47.     }
  48.     public function remove($webspaceKey$key)
  49.     {
  50.         $propertyName $this->getPropertyName($key);
  51.         $this->sessionManager->setNodeProperty(
  52.             $this->deprecatedSessionManager->getWebspacePath($webspaceKey),
  53.             $propertyName,
  54.             null
  55.         );
  56.         $this->sessionManager->flush();
  57.     }
  58.     public function load($webspaceKey$key)
  59.     {
  60.         $propertyName $this->getPropertyName($key);
  61.         $value $this->deprecatedSessionManager->getWebspaceNode($webspaceKey)->getPropertyValueWithDefault(
  62.             $propertyName,
  63.             'null'
  64.         );
  65.         return $this->decodeValue($value);
  66.     }
  67.     public function loadString($webspaceKey$key)
  68.     {
  69.         $propertyName $this->getPropertyName($key);
  70.         $webspaceNode $this->deprecatedSessionManager->getWebspaceNode($webspaceKey);
  71.         if (!$webspaceNode->hasProperty($propertyName)) {
  72.             return;
  73.         }
  74.         return $webspaceNode->getProperty($propertyName)->getString();
  75.     }
  76.     public function loadByWildcard($webspaceKey$wildcard)
  77.     {
  78.         $properties $this->deprecatedSessionManager->getWebspaceNode($webspaceKey)->getProperties(
  79.             $this->getPropertyName($wildcard)
  80.         );
  81.         $data = [];
  82.         foreach ($properties as $property) {
  83.             $data[\substr($property->getName(), 9)] = $this->decodeValue($property->getValue());
  84.         }
  85.         return $data;
  86.     }
  87.     public function loadStringByWildcard($webspaceKey$wildcard)
  88.     {
  89.         $webspaceNode $this->deprecatedSessionManager->getWebspaceNode($webspaceKey);
  90.         $properties $webspaceNode->getProperties($this->getPropertyName($wildcard));
  91.         $data = [];
  92.         foreach ($properties as $property) {
  93.             $data[\substr($property->getName(), 9)] = $property->getString();
  94.         }
  95.         return $data;
  96.     }
  97.     /**
  98.      * Returns decoded value.
  99.      */
  100.     private function decodeValue($value)
  101.     {
  102.         if ($value instanceof NodeInterface) {
  103.             return $value;
  104.         }
  105.         return \json_decode($valuetrue);
  106.     }
  107.     /**
  108.      * Returns phpcr-propertyname for given key.
  109.      *
  110.      * @param string $key
  111.      *
  112.      * @return string
  113.      */
  114.     private function getPropertyName($key)
  115.     {
  116.         return \sprintf('settings:%s'$key);
  117.     }
  118. }