2019-04-20 19:37:24 +02:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
2022-11-29 22:28:53 +01:00
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
2020-02-22 18:14:36 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-01-05 15:46:58 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-12-18 17:28:42 +01:00
|
|
|
namespace App\Services\Trees;
|
2019-04-20 19:37:24 +02:00
|
|
|
|
2020-02-01 19:48:07 +01:00
|
|
|
use App\Entity\Base\AbstractStructuralDBElement;
|
2019-04-20 19:37:24 +02:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
|
|
|
|
class StructuralElementRecursionHelper
|
|
|
|
{
|
2023-06-11 14:15:46 +02:00
|
|
|
public function __construct(protected EntityManagerInterface $em)
|
2019-04-20 19:37:24 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-04-15 23:14:53 +02:00
|
|
|
* Executes a function (callable) recursivly for $element and every of its children.
|
2019-04-20 19:37:24 +02:00
|
|
|
*
|
2020-02-01 19:48:07 +01:00
|
|
|
* @param AbstractStructuralDBElement $element The element on which the func should be executed
|
2020-03-15 13:56:31 +01:00
|
|
|
* @param callable $func The function which should be executed for each element.
|
|
|
|
* $func has the signature function(StructuralDBElement $element) : void
|
|
|
|
* @param int $max_depth The maximum depth for which should be recursivly called. So if this is set to 5, after the
|
|
|
|
* 5th level the execution is stopped.
|
2022-08-14 19:09:07 +02:00
|
|
|
* @param bool $call_from_bottom If set to true the bottom elements (elements with high level) will be called first.
|
2020-03-15 13:56:31 +01:00
|
|
|
* Set to false if you want to call the top elements first.
|
2019-04-20 19:37:24 +02:00
|
|
|
*/
|
2022-08-14 19:09:07 +02:00
|
|
|
public function execute(AbstractStructuralDBElement $element, callable $func, int $max_depth = -1, bool $call_from_bottom = true): void
|
2019-04-20 19:37:24 +02:00
|
|
|
{
|
|
|
|
//Cancel if we reached our maximal allowed level. Must be zero because -1 is infinity levels
|
2020-01-05 15:46:58 +01:00
|
|
|
if (0 === $max_depth) {
|
2019-04-20 19:37:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Get children of the current class:
|
|
|
|
$children = $element->getChildren();
|
|
|
|
|
|
|
|
//If we should call from top we execute the func here.
|
2020-08-21 21:36:22 +02:00
|
|
|
if (!$call_from_bottom) {
|
2019-04-20 19:37:24 +02:00
|
|
|
$func($element);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($children as $child) {
|
|
|
|
$this->execute($child, $func, $max_depth - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Otherwise we call it here
|
|
|
|
if ($call_from_bottom) {
|
|
|
|
$func($element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes the $element and all its subelements recursivly.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-02-01 19:48:07 +01:00
|
|
|
* @param AbstractStructuralDBElement $element the element which should be deleted
|
2020-03-15 13:56:31 +01:00
|
|
|
* @param bool $flush When set to true the changes will also be flushed to DB. Set to false if you want to flush
|
|
|
|
* later.
|
2019-04-20 19:37:24 +02:00
|
|
|
*/
|
2020-02-01 19:48:07 +01:00
|
|
|
public function delete(AbstractStructuralDBElement $element, bool $flush = true): void
|
2019-04-20 19:37:24 +02:00
|
|
|
{
|
|
|
|
$em = $this->em;
|
|
|
|
|
2020-02-01 19:48:07 +01:00
|
|
|
$this->execute($element, static function (AbstractStructuralDBElement $element) use ($em): void {
|
2019-04-20 19:37:24 +02:00
|
|
|
$em->remove($element);
|
|
|
|
});
|
|
|
|
|
2019-11-09 00:47:20 +01:00
|
|
|
if ($flush) {
|
2019-04-20 19:37:24 +02:00
|
|
|
$em->flush();
|
|
|
|
}
|
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
}
|