Skip to content

Commit

Permalink
Deprecate ModelManagerInterface collection-related methods (#6312)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Aug 24, 2020
1 parent d0faf8a commit 243f272
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 9 deletions.
9 changes: 9 additions & 0 deletions UPGRADE-3.x.md
@@ -1,6 +1,15 @@
UPGRADE 3.x
===========

## Deprecated `Sonata\AdminBundle\Model\ModelManagerInterface` collection-related methods.

Use:
- `new \Doctrine\Common\Collections\ArrayCollection()` instead of `getModelCollectionInstance($class)`
- `$collection->removeElement($element)` instead of `collectionRemoveElement($collection, $element)`
- `$collection->add($element)` instead of `collectionAddElement($collection, $element)`
- `$collection->contains($element)` instead of `collectionHasElement($collection, $element)`
- `$collection->clear()` instead of `collectionClear($collection)`

UPGRADE FROM 3.73 to 3.74
=========================

Expand Down
3 changes: 2 additions & 1 deletion src/Form/DataTransformer/ModelToIdPropertyTransformer.php
Expand Up @@ -13,6 +13,7 @@

namespace Sonata\AdminBundle\Form\DataTransformer;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Util\ClassUtils;
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Symfony\Component\Form\DataTransformerInterface;
Expand Down Expand Up @@ -73,7 +74,7 @@ public function __construct(

public function reverseTransform($value)
{
$collection = $this->modelManager->getModelCollectionInstance($this->className);
$collection = new ArrayCollection();

if (empty($value)) {
if ($this->multiple) {
Expand Down
3 changes: 2 additions & 1 deletion src/Form/DataTransformer/ModelsToArrayTransformer.php
Expand Up @@ -13,6 +13,7 @@

namespace Sonata\AdminBundle\Form\DataTransformer;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Util\ClassUtils;
use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceLoader;
use Sonata\AdminBundle\Model\ModelManagerInterface;
Expand Down Expand Up @@ -146,7 +147,7 @@ public function reverseTransform($keys)
throw new UnexpectedTypeException($keys, 'array');
}

$collection = $this->modelManager->getModelCollectionInstance($this->class);
$collection = new ArrayCollection();
$notFound = [];

// optimize this into a SELECT WHERE IN query
Expand Down
29 changes: 22 additions & 7 deletions src/Form/EventListener/MergeCollectionListener.php
Expand Up @@ -13,6 +13,7 @@

namespace Sonata\AdminBundle\Form\EventListener;

use Doctrine\Common\Collections\Collection;
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvent;
Expand All @@ -26,12 +27,23 @@
class MergeCollectionListener implements EventSubscriberInterface
{
/**
* @var ModelManagerInterface
* @var ModelManagerInterface|null
*/
protected $modelManager;

public function __construct(ModelManagerInterface $modelManager)
/**
* NEXT_MAJOR: Remove this constructor and the modelManager property.
*/
public function __construct(?ModelManagerInterface $modelManager = null)
{
if (null !== $modelManager) {
@trigger_error(sprintf(
'Passing argument 1 to %s() is deprecated since sonata-project/admin-bundle 3.x'
.' and will be ignored in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);
}

$this->modelManager = $modelManager;
}

Expand All @@ -45,27 +57,30 @@ public static function getSubscribedEvents()
public function onBind(FormEvent $event)
{
$collection = $event->getForm()->getData();
\assert(null === $collection || $collection instanceof Collection);

$data = $event->getData();
\assert($data instanceof Collection);

// looks like there is no way to remove other listeners
$event->stopPropagation();

if (!$collection) {
$collection = $data;
} elseif (0 === \count($data)) {
$this->modelManager->collectionClear($collection);
$collection->clear();
} else {
// merge $data into $collection
foreach ($collection as $model) {
if (!$this->modelManager->collectionHasElement($data, $model)) {
$this->modelManager->collectionRemoveElement($collection, $model);
if (!$data->contains($model)) {
$collection->removeElement($model);
} else {
$this->modelManager->collectionRemoveElement($data, $model);
$data->removeElement($model);
}
}

foreach ($data as $model) {
$this->modelManager->collectionAddElement($collection, $model);
$collection->add($model);
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/Model/ModelManagerInterface.php
Expand Up @@ -169,13 +169,21 @@ public function getUrlSafeIdentifier($model);
public function getModelInstance($class);

/**
* NEXT_MAJOR: Remove this method.
*
* @deprecated since sonata-project/admin-bundle 3.x. To be removed in 4.0. Use doctrine/collections instead.
*
* @param string $class
*
* @return array|\ArrayAccess
*/
public function getModelCollectionInstance($class);

/**
* NEXT_MAJOR: Remove this method.
*
* @deprecated since sonata-project/admin-bundle 3.x. To be removed in 4.0. Use doctrine/collections instead.
*
* Removes an element from the collection.
*
* @param array $collection
Expand All @@ -184,6 +192,10 @@ public function getModelCollectionInstance($class);
public function collectionRemoveElement(&$collection, &$element);

/**
* NEXT_MAJOR: Remove this method.
*
* @deprecated since sonata-project/admin-bundle 3.x. To be removed in 4.0. Use doctrine/collections instead.
*
* Add an element from the collection.
*
* @param array $collection
Expand All @@ -192,6 +204,10 @@ public function collectionRemoveElement(&$collection, &$element);
public function collectionAddElement(&$collection, &$element);

/**
* NEXT_MAJOR: Remove this method.
*
* @deprecated since sonata-project/admin-bundle 3.x. To be removed in 4.0. Use doctrine/collections instead.
*
* Check if the element exists in the collection.
*
* @param array $collection
Expand All @@ -202,6 +218,10 @@ public function collectionAddElement(&$collection, &$element);
public function collectionHasElement(&$collection, &$element);

/**
* NEXT_MAJOR: Remove this method.
*
* @deprecated since sonata-project/admin-bundle 3.x. To be removed in 4.0. Use doctrine/collections instead.
*
* Clear the collection.
*
* @param array $collection
Expand Down
15 changes: 15 additions & 0 deletions tests/App/Model/ModelManager.php
Expand Up @@ -124,23 +124,38 @@ public function getModelInstance($class)
}
}

/**
* NEXT_MAJOR: Remove this method.
*/
public function getModelCollectionInstance($class)
{
return [];
}

/**
* NEXT_MAJOR: Remove this method.
*/
public function collectionRemoveElement(&$collection, &$element): void
{
}

/**
* NEXT_MAJOR: Remove this method.
*/
public function collectionAddElement(&$collection, &$element): void
{
}

/**
* NEXT_MAJOR: Remove this method.
*/
public function collectionHasElement(&$collection, &$element): void
{
}

/**
* NEXT_MAJOR: Remove this method.
*/
public function collectionClear(&$collection): void
{
}
Expand Down

0 comments on commit 243f272

Please sign in to comment.