1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelDoctrine\Fluent\Extensions\Gedmo; |
4
|
|
|
|
5
|
|
|
use Gedmo\Exception\InvalidMappingException; |
6
|
|
|
use Gedmo\Tree\Mapping\Driver\Fluent as FluentDriver; |
7
|
|
|
use LaravelDoctrine\Fluent\Buildable; |
8
|
|
|
use LaravelDoctrine\Fluent\Builders\Builder; |
9
|
|
|
use LaravelDoctrine\Fluent\Extensions\ExtensibleClassMetadata; |
10
|
|
|
use LaravelDoctrine\Fluent\Fluent; |
11
|
|
|
|
12
|
|
|
class Tree implements Buildable |
13
|
|
|
{ |
14
|
|
|
const MACRO_METHOD = 'tree'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* List of tree strategies available |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $strategies = [ |
22
|
|
|
'nested', |
23
|
|
|
'closure', |
24
|
|
|
'materializedPath', |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ExtensibleClassMetadata |
29
|
|
|
*/ |
30
|
|
|
protected $classMetadata; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $strategy; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var bool |
39
|
|
|
*/ |
40
|
|
|
protected $activateLocking = false; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var int |
44
|
|
|
*/ |
45
|
|
|
protected $lockingTimeout = 3; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $closure; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param ExtensibleClassMetadata $classMetadata |
54
|
|
|
* @param string $strategy |
55
|
|
|
*/ |
56
|
5 |
|
public function __construct(ExtensibleClassMetadata $classMetadata, $strategy = 'nested') |
57
|
|
|
{ |
58
|
5 |
|
$this->classMetadata = $classMetadata; |
59
|
5 |
|
$this->strategy = $strategy; |
60
|
5 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Enable extension |
64
|
|
|
*/ |
65
|
|
|
public static function enable() |
66
|
|
|
{ |
67
|
1 |
|
Builder::macro(self::MACRO_METHOD, function (Fluent $fluent, $strategy = 'nested') { |
68
|
1 |
|
return new static($fluent->getBuilder()->getClassMetadata(), $strategy); |
|
|
|
|
69
|
1 |
|
}); |
70
|
|
|
|
71
|
1 |
|
TreeLeft::enable(); |
72
|
1 |
|
TreeRight::enable(); |
73
|
1 |
|
TreeLevel::enable(); |
74
|
1 |
|
TreeRoot::enable(); |
75
|
1 |
|
TreeParent::enable(); |
76
|
1 |
|
TreePath::enable(); |
77
|
1 |
|
TreePathSource::enable(); |
78
|
1 |
|
TreePathHash::enable(); |
79
|
1 |
|
TreeLockTime::enable(); |
80
|
1 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Execute the build process |
84
|
|
|
*/ |
85
|
3 |
View Code Duplication |
public function build() |
|
|
|
|
86
|
|
|
{ |
87
|
3 |
|
if (!in_array($this->strategy, $this->strategies)) { |
88
|
1 |
|
throw new InvalidMappingException("Tree type: $this->strategy is not available."); |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
$this->classMetadata->appendExtension($this->getExtensionName(), [ |
92
|
2 |
|
'strategy' => $this->strategy, |
93
|
2 |
|
'activate_locking' => $this->activateLocking, |
94
|
2 |
|
'locking_timeout' => $this->lockingTimeout, |
95
|
2 |
|
'closure' => $this->closure |
96
|
2 |
|
]); |
97
|
2 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Return the name of the actual extension. |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
2 |
|
public function getExtensionName() |
105
|
|
|
{ |
106
|
2 |
|
return FluentDriver::EXTENSION_NAME; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param bool $activateLocking |
111
|
|
|
* @return Tree |
112
|
|
|
*/ |
113
|
1 |
|
public function activateLocking($activateLocking = true) |
114
|
|
|
{ |
115
|
1 |
|
$this->activateLocking = $activateLocking; |
116
|
|
|
|
117
|
1 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $strategy |
122
|
|
|
* @return Tree |
123
|
|
|
*/ |
124
|
2 |
|
public function strategy($strategy) |
125
|
|
|
{ |
126
|
2 |
|
$this->strategy = $strategy; |
127
|
|
|
|
128
|
2 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param int $lockingTimeout |
133
|
|
|
* @return Tree |
134
|
|
|
*/ |
135
|
2 |
|
public function lockingTimeout($lockingTimeout) |
136
|
|
|
{ |
137
|
2 |
|
if ($lockingTimeout < 1) { |
138
|
1 |
|
throw new InvalidMappingException("Tree Locking Timeout must be at least of 1 second."); |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
$this->lockingTimeout = $lockingTimeout; |
142
|
|
|
|
143
|
1 |
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $closure |
148
|
|
|
* @return Tree |
149
|
|
|
*/ |
150
|
1 |
|
public function closure($closure) |
151
|
|
|
{ |
152
|
1 |
|
$this->closure = $closure; |
153
|
|
|
|
154
|
1 |
|
return $this; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.