"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/layout_builder/src/LayoutTempstoreRepository.php" (26 Nov 2020, 3140 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "LayoutTempstoreRepository.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\layout_builder;
    4 
    5 use Drupal\Core\TempStore\SharedTempStoreFactory;
    6 
    7 /**
    8  * Provides a mechanism for loading layouts from tempstore.
    9  */
   10 class LayoutTempstoreRepository implements LayoutTempstoreRepositoryInterface {
   11 
   12   /**
   13    * The shared tempstore factory.
   14    *
   15    * @var \Drupal\Core\TempStore\SharedTempStoreFactory
   16    */
   17   protected $tempStoreFactory;
   18 
   19   /**
   20    * LayoutTempstoreRepository constructor.
   21    *
   22    * @param \Drupal\Core\TempStore\SharedTempStoreFactory $temp_store_factory
   23    *   The shared tempstore factory.
   24    */
   25   public function __construct(SharedTempStoreFactory $temp_store_factory) {
   26     $this->tempStoreFactory = $temp_store_factory;
   27   }
   28 
   29   /**
   30    * {@inheritdoc}
   31    */
   32   public function get(SectionStorageInterface $section_storage) {
   33     $key = $this->getKey($section_storage);
   34     $tempstore = $this->getTempstore($section_storage)->get($key);
   35     if (!empty($tempstore['section_storage'])) {
   36       $storage_type = $section_storage->getStorageType();
   37       $section_storage = $tempstore['section_storage'];
   38 
   39       if (!($section_storage instanceof SectionStorageInterface)) {
   40         throw new \UnexpectedValueException(sprintf('The entry with storage type "%s" and ID "%s" is invalid', $storage_type, $key));
   41       }
   42     }
   43     return $section_storage;
   44   }
   45 
   46   /**
   47    * {@inheritdoc}
   48    */
   49   public function has(SectionStorageInterface $section_storage) {
   50     $key = $this->getKey($section_storage);
   51     $tempstore = $this->getTempstore($section_storage)->get($key);
   52     return !empty($tempstore['section_storage']);
   53   }
   54 
   55   /**
   56    * {@inheritdoc}
   57    */
   58   public function set(SectionStorageInterface $section_storage) {
   59     $key = $this->getKey($section_storage);
   60     $this->getTempstore($section_storage)->set($key, ['section_storage' => $section_storage]);
   61   }
   62 
   63   /**
   64    * {@inheritdoc}
   65    */
   66   public function delete(SectionStorageInterface $section_storage) {
   67     $key = $this->getKey($section_storage);
   68     $this->getTempstore($section_storage)->delete($key);
   69   }
   70 
   71   /**
   72    * Gets the shared tempstore.
   73    *
   74    * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
   75    *   The section storage.
   76    *
   77    * @return \Drupal\Core\TempStore\SharedTempStore
   78    *   The tempstore.
   79    */
   80   protected function getTempstore(SectionStorageInterface $section_storage) {
   81     $collection = 'layout_builder.section_storage.' . $section_storage->getStorageType();
   82     return $this->tempStoreFactory->get($collection);
   83   }
   84 
   85   /**
   86    * Gets the string to use as the tempstore key.
   87    *
   88    * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
   89    *   The section storage.
   90    *
   91    * @return string
   92    *   A unique string representing the section storage. This should include as
   93    *   much identifying information as possible about this particular storage,
   94    *   including information like the current language.
   95    */
   96   protected function getKey(SectionStorageInterface $section_storage) {
   97     if ($section_storage instanceof TempStoreIdentifierInterface) {
   98       return $section_storage->getTempstoreKey();
   99     }
  100 
  101     return $section_storage->getStorageId();
  102   }
  103 
  104 }