Skip to content

Commit

Permalink
Merge pull request #1534 from dpfaffenbauer/issues/deep-copy
Browse files Browse the repository at this point in the history
[ResourceBundle] add Pimcore DeepCopy Subscriber to fix doctrine collections clone problem
  • Loading branch information
dpfaffenbauer authored Dec 2, 2020
2 parents 12f1f87 + 1a1587d commit 8d9a60b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) 2015-2020 Dominik Pfaffenbauer (https://www.pfaffenbauer.at)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/

namespace CoreShop\Bundle\ResourceBundle\EventListener;

use DeepCopy\DeepCopy;
use DeepCopy\Filter\Doctrine\DoctrineCollectionFilter;
use DeepCopy\Matcher\PropertyTypeMatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

class DeepCopySubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
//TODO: Change to Pimcore\Event\SystemEvents::SERVICE_PRE_GET_DEEP_COPY later
//TODO: Event has been added with 6.8.5, min requirement is 6.6 for now.
return [
'pimcore.system.service.preGetDeepCopy' => 'addDoctrineCollectionFilter',
];
}

public function addDoctrineCollectionFilter(GenericEvent $event)
{
$context = $event->getArgument('context');

//Only add if not already been added
if (!($context['defaultFilters'] ?? false)) {
/**
* @var DeepCopy $copier
*/
$copier = $event->getArgument('copier');
$copier->addFilter(new DoctrineCollectionFilter(),
new PropertyTypeMatcher('Doctrine\Common\Collections\Collection'));
$event->setArgument('copier', $copier);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,7 @@ services:
decorates: 'jms_serializer.metadata.annotation_driver'
arguments:
- '@CoreShop\Bundle\ResourceBundle\Serialization\Driver\PimcoreDataObjectDriver.inner'

CoreShop\Bundle\ResourceBundle\EventListener\DeepCopySubscriber:
tags:
- { name: kernel.event_subscriber }

0 comments on commit 8d9a60b

Please sign in to comment.