1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* part-db version 0.1 |
5
|
|
|
* Copyright (C) 2005 Christoph Lechner |
6
|
|
|
* http://www.cl-projects.de/ |
7
|
|
|
* |
8
|
|
|
* part-db version 0.2+ |
9
|
|
|
* Copyright (C) 2009 K. Jacobs and others (see authors.php) |
10
|
|
|
* http://code.google.com/p/part-db/ |
11
|
|
|
* |
12
|
|
|
* Part-DB Version 0.4+ |
13
|
|
|
* Copyright (C) 2016 - 2019 Jan Böhmer |
14
|
|
|
* https://github.com/jbtronics |
15
|
|
|
* |
16
|
|
|
* This program is free software; you can redistribute it and/or |
17
|
|
|
* modify it under the terms of the GNU General Public License |
18
|
|
|
* as published by the Free Software Foundation; either version 2 |
19
|
|
|
* of the License, or (at your option) any later version. |
20
|
|
|
* |
21
|
|
|
* This program is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU General Public License |
27
|
|
|
* along with this program; if not, write to the Free Software |
28
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
namespace App\Services; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
use App\Entity\StructuralDBElement; |
36
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
37
|
|
|
|
38
|
|
|
class StructuralElementRecursionHelper |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
protected $em; |
42
|
|
|
|
43
|
|
|
public function __construct(EntityManagerInterface $em) |
44
|
|
|
{ |
45
|
|
|
$this->em = $em; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Executes an function (callable) recursivly for $element and every of its children. |
50
|
|
|
* |
51
|
|
|
* @param StructuralDBElement $element The element on which the func should be executed |
52
|
|
|
* @param callable $func The function which should be executed for each element. |
53
|
|
|
* $func has the signature function(StructuralDBElement $element) : void |
54
|
|
|
* @param int $max_depth The maximum depth for which should be recursivly called. So if this is set to 5, after the |
55
|
|
|
* 5th level the execution is stopped. |
56
|
|
|
* @param bool $call_from_bottom If set to true the bottom elements (elements with high level) will be called first. |
57
|
|
|
* Set to false if you want to call the top elements first. |
58
|
|
|
*/ |
59
|
|
|
public function execute(StructuralDBElement $element, callable $func, int $max_depth = -1, $call_from_bottom = true) : void |
60
|
|
|
{ |
61
|
|
|
//Cancel if we reached our maximal allowed level. Must be zero because -1 is infinity levels |
62
|
|
|
if ($max_depth == 0) { |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
//Get children of the current class: |
67
|
|
|
$children = $element->getChildren(); |
68
|
|
|
|
69
|
|
|
//If we should call from top we execute the func here. |
70
|
|
|
if (!$call_from_bottom) { |
71
|
|
|
$func($element); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
foreach ($children as $child) { |
75
|
|
|
$this->execute($child, $func, $max_depth - 1); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
//Otherwise we call it here |
79
|
|
|
if ($call_from_bottom) { |
80
|
|
|
$func($element); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Deletes the $element and all its subelements recursivly. |
86
|
|
|
* @param StructuralDBElement $element The element which should be deleted. |
87
|
|
|
* @param bool $flush When set to true the changes will also be flushed to DB. Set to false if you want to flush |
88
|
|
|
* later. |
89
|
|
|
*/ |
90
|
|
|
public function delete(StructuralDBElement $element, bool $flush = true) : void |
91
|
|
|
{ |
92
|
|
|
$em = $this->em; |
93
|
|
|
|
94
|
|
|
$this->execute($element, static function(StructuralDBElement $element) use ($em) { |
95
|
|
|
$em->remove($element); |
96
|
|
|
}); |
97
|
|
|
|
98
|
|
|
if($flush) { |
99
|
|
|
$em->flush(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |