Skip to content

Commit

Permalink
Merge pull request #19 from zicht/release/2.8.x
Browse files Browse the repository at this point in the history
Release/2.8.x
  • Loading branch information
pbergman authored Feb 7, 2019
2 parents f8be8b9 + d7594e4 commit b6f22d5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,6 @@ Nothing so far
## 2.8.8 - Wed, 8 Mar 2018 13:40 GMT
- Refactored the default fallback from `[none]` to `%kernel.default_locale%` to make behavior valid in donner 3.0.
- Updated previous missing changelog entries

## 2.8.10
- bug fix for the `duplicate on tree root` bug when removing pages (with linked menu items)
57 changes: 49 additions & 8 deletions src/Zicht/Bundle/MenuBundle/Manager/MenuManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
namespace Zicht\Bundle\MenuBundle\Manager;

use Doctrine\Bundle\DoctrineBundle\Registry;
use Doctrine\Common\Persistence\ObjectRepository;
use Gedmo\Tree\Entity\Repository\NestedTreeRepository;
use Zicht\Bundle\MenuBundle\Entity\MenuItem;

/**
Expand All @@ -16,14 +18,24 @@
*/
class MenuManager
{
/** @var int */
const REMOVE = 0x01;
/** @var int */
const ADD = 0x02;
/** @var \SplQueue */
private $queue;
/** @var Registry */
protected $doctrine;

/**
* Constructor.
* MenuManager constructor.
*
* @param Registry $doctrine
*/
public function __construct(Registry $doctrine)
{
$this->doctrine = $doctrine;
$this->queue = new \SplQueue();
}


Expand All @@ -35,9 +47,7 @@ public function __construct(Registry $doctrine)
*/
public function addItem(MenuItem $item)
{
$manager = $this->doctrine->getManager();
$manager->persist($item);
$manager->flush();
$this->queue->enqueue([$item, self::ADD]);
}


Expand All @@ -49,9 +59,7 @@ public function addItem(MenuItem $item)
*/
public function removeItem(MenuItem $item)
{
$manager = $this->doctrine->getManager();
$manager->remove($item);
$manager->flush();
$this->queue->enqueue([$item, self::REMOVE]);
}


Expand All @@ -62,9 +70,42 @@ public function removeItem(MenuItem $item)
*/
public function flush($flushEntityManager = false)
{
// Do nothing, the items have already been flushed when addItem() and removeItem() were called
$this->queue->rewind();

while ($this->queue->valid()) {
list($item, $mode) = $this->queue->dequeue();
switch ($mode) {
case self::REMOVE:
if (($repo = $this->getRepository(get_class($item))) && $repo instanceof NestedTreeRepository) {
$repo->removeFromTree($item);
} else {
$this->doctrine->getManager()->remove($item);
}
break;
case self::ADD:
$this->doctrine->getManager()->persist($item);
break;
}
$this->queue->next();
}

if ($flushEntityManager) {
$this->doctrine->getManager()->flush();
}
}

/**
* @param string $className
* @return ObjectRepository
*/
private function getRepository($className)
{
if (null !== $manager = $this->doctrine->getManagerForClass($className)) {
return $manager->getRepository($className);
}

return null;
}

/**
* Find an item by a path
Expand Down

0 comments on commit b6f22d5

Please sign in to comment.