vendor/pimcore/pimcore/models/DataObject/Data/BlockElement.php line 34

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\DataObject\Data;
  15. use DeepCopy\DeepCopy;
  16. use DeepCopy\Filter\SetNullFilter;
  17. use DeepCopy\Matcher\PropertyNameMatcher;
  18. use Pimcore\Cache\Core\CacheMarshallerInterface;
  19. use Pimcore\Cache\RuntimeCache;
  20. use Pimcore\Model\AbstractModel;
  21. use Pimcore\Model\DataObject\OwnerAwareFieldInterface;
  22. use Pimcore\Model\DataObject\Traits\OwnerAwareFieldTrait;
  23. use Pimcore\Model\Element\AbstractElement;
  24. use Pimcore\Model\Element\DeepCopy\UnmarshalMatcher;
  25. use Pimcore\Model\Element\ElementDescriptor;
  26. use Pimcore\Model\Element\ElementDumpStateInterface;
  27. use Pimcore\Model\Element\ElementInterface;
  28. use Pimcore\Model\Element\Service;
  29. use Pimcore\Model\Version\SetDumpStateFilter;
  30. class BlockElement extends AbstractModel implements OwnerAwareFieldInterfaceCacheMarshallerInterface
  31. {
  32.     use OwnerAwareFieldTrait;
  33.     /**
  34.      * @var string
  35.      */
  36.     protected $name;
  37.     /**
  38.      * @var string
  39.      */
  40.     protected $type;
  41.     /**
  42.      * @var mixed
  43.      */
  44.     protected $data;
  45.     /**
  46.      * @internal
  47.      *
  48.      * @var bool
  49.      */
  50.     protected $needsRenewReferences false;
  51.     /**
  52.      * BlockElement constructor.
  53.      *
  54.      * @param string $name
  55.      * @param string $type
  56.      * @param mixed $data
  57.      */
  58.     public function __construct($name$type$data)
  59.     {
  60.         $this->name $name;
  61.         $this->type $type;
  62.         $this->data $data;
  63.         $this->markMeDirty();
  64.     }
  65.     /**
  66.      * @return string
  67.      */
  68.     public function getName()
  69.     {
  70.         return $this->name;
  71.     }
  72.     /**
  73.      * @param string $name
  74.      */
  75.     public function setName($name)
  76.     {
  77.         if ($name != $this->name) {
  78.             $this->name $name;
  79.             $this->markMeDirty();
  80.         }
  81.     }
  82.     /**
  83.      * @return string
  84.      */
  85.     public function getType()
  86.     {
  87.         return $this->type;
  88.     }
  89.     /**
  90.      * @param string $type
  91.      */
  92.     public function setType($type)
  93.     {
  94.         if ($type != $this->type) {
  95.             $this->type $type;
  96.             $this->markMeDirty();
  97.         }
  98.     }
  99.     /**
  100.      * @return mixed
  101.      */
  102.     public function getData()
  103.     {
  104.         if ($this->needsRenewReferences) {
  105.             $this->needsRenewReferences false;
  106.             $this->renewReferences();
  107.         }
  108.         return $this->data;
  109.     }
  110.     /**
  111.      * @param mixed $data
  112.      */
  113.     public function setData($data)
  114.     {
  115.         $this->data $data;
  116.         $this->markMeDirty();
  117.     }
  118.     protected function renewReferences()
  119.     {
  120.         $copier = new DeepCopy();
  121.         $copier->skipUncloneable(true);
  122.         $copier->addTypeFilter(
  123.             new \DeepCopy\TypeFilter\ReplaceFilter(
  124.                 function ($currentValue) {
  125.                     if ($currentValue instanceof ElementDescriptor) {
  126.                         $cacheKey $currentValue->getCacheKey();
  127.                         $cacheKeyRenewed $cacheKey '_blockElementRenewed';
  128.                         if (!RuntimeCache::isRegistered($cacheKeyRenewed)) {
  129.                             if (RuntimeCache::isRegistered($cacheKey)) {
  130.                                 // we don't want the copy from the runtime but cache is fine
  131.                                 RuntimeCache::getInstance()->offsetUnset($cacheKey);
  132.                             }
  133.                             RuntimeCache::save(true$cacheKeyRenewed);
  134.                         }
  135.                         $renewedElement Service::getElementById($currentValue->getType(), $currentValue->getId());
  136.                         return $renewedElement;
  137.                     }
  138.                     return $currentValue;
  139.                 }
  140.             ),
  141.             new UnmarshalMatcher()
  142.         );
  143.         $copier->addFilter(new \DeepCopy\Filter\KeepFilter(), new class() implements \DeepCopy\Matcher\Matcher {
  144.             public function matches($object$property)
  145.             {
  146.                 return $object instanceof AbstractElement;
  147.             }
  148.         });
  149.         $this->data $copier->copy($this->data);
  150.     }
  151.     /**
  152.      * @return string
  153.      */
  154.     public function __toString()
  155.     {
  156.         return $this->name '; ' $this->type;
  157.     }
  158.     public function __wakeup()
  159.     {
  160.         $this->needsRenewReferences true;
  161.         if ($this->data instanceof OwnerAwareFieldInterface) {
  162.             $this->data->_setOwner($this);
  163.             $this->data->_setOwnerFieldname($this->getName());
  164.             $this->data->_setOwnerLanguage(null);
  165.         }
  166.     }
  167.     /**
  168.      * @internal
  169.      *
  170.      * @return bool
  171.      */
  172.     public function getNeedsRenewReferences(): bool
  173.     {
  174.         return $this->needsRenewReferences;
  175.     }
  176.     /**
  177.      * @internal
  178.      *
  179.      * @param bool $needsRenewReferences
  180.      */
  181.     public function setNeedsRenewReferences(bool $needsRenewReferences)
  182.     {
  183.         $this->needsRenewReferences = (bool) $needsRenewReferences;
  184.     }
  185.     /**
  186.      * @param string $language
  187.      */
  188.     public function setLanguage(string $language)
  189.     {
  190.         $this->_language $language;
  191.     }
  192.     /**
  193.      * @return mixed
  194.      */
  195.     public function marshalForCache()
  196.     {
  197.         $this->needsRenewReferences true;
  198.         $context = [
  199.             'source' => __METHOD__,
  200.             'conversion' => false,
  201.         ];
  202.         $copier Service::getDeepCopyInstance($this$context);
  203.         $copier->addFilter(new SetDumpStateFilter(false), new \DeepCopy\Matcher\PropertyMatcher(ElementDumpStateInterface::class, ElementDumpStateInterface::DUMP_STATE_PROPERTY_NAME));
  204.         $copier->addTypeFilter(
  205.             new \DeepCopy\TypeFilter\ReplaceFilter(
  206.                 function ($currentValue) {
  207.                     if ($currentValue instanceof ElementInterface) {
  208.                         $elementType Service::getElementType($currentValue);
  209.                         $descriptor = new ElementDescriptor($elementType$currentValue->getId());
  210.                         return $descriptor;
  211.                     }
  212.                     return $currentValue;
  213.                 }
  214.             ),
  215.             new \Pimcore\Model\Element\DeepCopy\MarshalMatcher(nullnull)
  216.         );
  217.         $copier->addFilter(new SetNullFilter(), new PropertyNameMatcher('_owner'));
  218.         $data $copier->copy($this);
  219.         return $data;
  220.     }
  221. }