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