vendor/pimcore/pimcore/models/Document/Hardlink/Service.php line 145

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\Document\Hardlink;
  15. use Pimcore\Model\Document;
  16. use Pimcore\Tool\Serialize;
  17. class Service
  18. {
  19.     /**
  20.      * @param Document $doc
  21.      *
  22.      * @return Document\Hardlink\Wrapper\WrapperInterface|null
  23.      *
  24.      * @throws \Exception
  25.      */
  26.     public static function wrap(Document $doc)
  27.     {
  28.         if ($doc instanceof Document\Hardlink) {
  29.             if ($sourceDoc $doc->getSourceDocument()) {
  30.                 /** @var Document\Hardlink\Wrapper\Hardlink $destDoc */
  31.                 $destDoc self::upperCastDocument($sourceDoc);
  32.                 $destDoc->setKey($doc->getKey());
  33.                 $destDoc->setPath($doc->getRealPath());
  34.                 $destDoc->initDao(get_class($sourceDoc), true);
  35.                 $destDoc->setHardLinkSource($doc);
  36.                 $destDoc->setSourceDocument($sourceDoc);
  37.                 return $destDoc;
  38.             }
  39.         } else {
  40.             $destDoc self::upperCastDocument($doc);
  41.             $destDoc->initDao(get_class($doc), true);
  42.             $destDoc->setSourceDocument($doc);
  43.             return $destDoc;
  44.         }
  45.         return null;
  46.     }
  47.     /**
  48.      * @internal
  49.      *
  50.      * @static
  51.      *
  52.      * @param Document $doc
  53.      *
  54.      * @return Document\Hardlink\Wrapper\WrapperInterface
  55.      */
  56.     public static function upperCastDocument(Document $doc)
  57.     {
  58.         $to_class 'Pimcore\\Model\\Document\\Hardlink\\Wrapper\\' ucfirst($doc->getType());
  59.         $old_serialized_prefix 'O:'.strlen(get_class($doc));
  60.         $old_serialized_prefix .= ':"'.get_class($doc).'":';
  61.         // unset eventually existing children, because of performance reasons when serializing the document
  62.         $doc->setChildren(null);
  63.         $old_serialized_object Serialize::serialize($doc);
  64.         $new_serialized_object 'O:'.strlen($to_class).':"'.$to_class '":';
  65.         $new_serialized_object .= substr($old_serialized_objectstrlen($old_serialized_prefix));
  66.         $document Serialize::unserialize($new_serialized_object);
  67.         return $document;
  68.     }
  69.     /**
  70.      * @internal
  71.      *
  72.      * this is used to get children below a hardlink by a path
  73.      * for example: the requested path is /de/service/contact but /de/service is a hardlink to /en/service
  74.      * then $hardlink would be /en/service and $path /de/service/contact and this function returns then /en/service/contact
  75.      *
  76.      * @param Document\Hardlink $hardlink
  77.      * @param string $path
  78.      *
  79.      * @return Document\Hardlink\Wrapper\WrapperInterface|null
  80.      */
  81.     public static function getChildByPath(Document\Hardlink $hardlink$path)
  82.     {
  83.         if ($hardlink->getChildrenFromSource() && $hardlink->getSourceDocument()) {
  84.             $hardlinkRealPath preg_replace('@^' preg_quote($hardlink->getRealFullPath(), '@') . '@'$hardlink->getSourceDocument()->getRealFullPath(), $path);
  85.             $hardLinkedDocument Document::getByPath($hardlinkRealPath);
  86.             if ($hardLinkedDocument instanceof Document) {
  87.                 $hardLinkedDocument self::wrap($hardLinkedDocument);
  88.                 $hardLinkedDocument->setHardLinkSource($hardlink);
  89.                 $_path $path != '/' $_path dirname($path) : $path;
  90.                 $_path str_replace('\\''/'$_path); // windows patch
  91.                 $_path .= $_path != '/' '/' '';
  92.                 $hardLinkedDocument->setPath($_path);
  93.                 return $hardLinkedDocument;
  94.             }
  95.         }
  96.         return null;
  97.     }
  98.     /**
  99.      * @internal
  100.      *
  101.      * @param Document\Hardlink $hardlink
  102.      * @param string $path
  103.      *
  104.      * @return Document\Hardlink\Wrapper\WrapperInterface|null
  105.      */
  106.     public static function getNearestChildByPath(Document\Hardlink $hardlink$path)
  107.     {
  108.         if ($hardlink->getChildrenFromSource() && $hardlink->getSourceDocument()) {
  109.             $hardlinkRealPath preg_replace('@^' preg_quote($hardlink->getRealFullPath(), '@') . '@'$hardlink->getSourceDocument()->getRealFullPath(), $path);
  110.             $pathes = [];
  111.             $pathes[] = '/';
  112.             $pathParts explode('/'$hardlinkRealPath);
  113.             $tmpPathes = [];
  114.             foreach ($pathParts as $pathPart) {
  115.                 $tmpPathes[] = $pathPart;
  116.                 $t implode('/'$tmpPathes);
  117.                 $pathes[] = $t;
  118.             }
  119.             $pathes array_reverse($pathes);
  120.             foreach ($pathes as $p) {
  121.                 $hardLinkedDocument Document::getByPath($p);
  122.                 if ($hardLinkedDocument instanceof Document) {
  123.                     $hardLinkedDocument self::wrap($hardLinkedDocument);
  124.                     $hardLinkedDocument->setHardLinkSource($hardlink);
  125.                     $_path $path != '/' $_path dirname($p) : $p;
  126.                     $_path str_replace('\\''/'$_path); // windows patch
  127.                     $_path .= $_path != '/' '/' '';
  128.                     $_path preg_replace('@^' preg_quote($hardlink->getSourceDocument()->getRealPath(), '@') . '@'$hardlink->getRealPath(), $_path);
  129.                     $hardLinkedDocument->setPath($_path);
  130.                     return $hardLinkedDocument;
  131.                 }
  132.             }
  133.         }
  134.         return null;
  135.     }
  136. }