1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelDoctrine\Fluent\Extensions\Gedmo; |
4
|
|
|
|
5
|
|
|
use LaravelDoctrine\Fluent\Buildable; |
6
|
|
|
use LaravelDoctrine\Fluent\Builders\Builder; |
7
|
|
|
use LaravelDoctrine\Fluent\Extensions\Extension; |
8
|
|
|
use LaravelDoctrine\Fluent\Fluent; |
9
|
|
|
|
10
|
|
|
class Tree implements Buildable, Extension |
11
|
|
|
{ |
12
|
|
|
const MACRO_METHOD = 'tree'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var Buildable |
16
|
|
|
*/ |
17
|
|
|
private $strategy; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Fluent |
21
|
|
|
*/ |
22
|
|
|
private $builder; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param Fluent $builder |
26
|
|
|
*/ |
27
|
9 |
|
public function __construct(Fluent $builder) |
28
|
|
|
{ |
29
|
9 |
|
$this->builder = $builder; |
30
|
9 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Enable extension |
34
|
|
|
*/ |
35
|
|
|
public static function enable() |
36
|
|
|
{ |
37
|
83 |
|
Builder::macro(self::MACRO_METHOD, function (Fluent $builder, callable $callback = null) { |
38
|
3 |
|
$tree = new static($builder); |
39
|
|
|
|
40
|
3 |
|
if (is_callable($callback)) { |
41
|
1 |
|
call_user_func($callback, $tree); |
42
|
1 |
|
} |
43
|
|
|
|
44
|
3 |
|
return $tree; |
45
|
83 |
|
}); |
46
|
|
|
|
47
|
83 |
|
NestedSet::enable(); |
48
|
83 |
|
TreePath::enable(); |
49
|
83 |
|
TreePathSource::enable(); |
50
|
83 |
|
TreePathHash::enable(); |
51
|
83 |
|
TreeLockTime::enable(); |
52
|
83 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Sets the tree strategy to NestedSet, returning it for further customization. |
56
|
|
|
* See {@link https://en.wikipedia.org/wiki/Nested_set_model} for more information on nested set trees. |
57
|
|
|
* |
58
|
|
|
* @return NestedSet |
59
|
|
|
*/ |
60
|
3 |
|
public function asNestedSet() |
61
|
1 |
|
{ |
62
|
3 |
|
return $this->strategy(new NestedSet($this->builder)); |
63
|
1 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Sets the tree strategy to MaterializedPath, returning it for further customization. |
67
|
|
|
* See {@link https://bojanz.wordpress.com/2014/04/25/storing-hierarchical-data-materialized-path/} for |
68
|
|
|
* more information on materialized path trees. |
69
|
|
|
* |
70
|
|
|
* @return MaterializedPath |
71
|
|
|
*/ |
72
|
2 |
|
public function asMaterializedPath() |
73
|
|
|
{ |
74
|
2 |
|
return $this->strategy(new MaterializedPath($this->builder)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Sets the tree strategy to ClosureTable, returning it for further customization. |
79
|
|
|
* See {@link https://coderwall.com/p/lixing/closure-tables-for-browsing-trees-in-sql} for more |
80
|
|
|
* information on closure table trees. |
81
|
|
|
* |
82
|
|
|
* @return ClosureTable |
83
|
|
|
*/ |
84
|
2 |
|
public function asClosureTable() |
85
|
|
|
{ |
86
|
2 |
|
return $this->strategy(new ClosureTable($this->builder)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Execute the build process |
91
|
|
|
*/ |
92
|
4 |
|
public function build() |
93
|
|
|
{ |
94
|
4 |
|
$this->strategy->build(); |
95
|
4 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param Buildable $strategy |
99
|
|
|
* |
100
|
|
|
* @return Buildable |
101
|
|
|
*/ |
102
|
7 |
|
protected function strategy(Buildable $strategy) |
103
|
|
|
{ |
104
|
7 |
|
return $this->strategy = $strategy; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|