-
Notifications
You must be signed in to change notification settings - Fork 461
/
Copy pathMove.php
396 lines (329 loc) · 10.1 KB
/
Move.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<?php
namespace Baum;
use \Illuminate\Events\Dispatcher;
/**
* Move
*/
class Move {
/**
* Node on which the move operation will be performed
*
* @var \Baum\Node
*/
protected $node = NULL;
/**
* Destination node
*
* @var \Baum\Node | int
*/
protected $target = NULL;
/**
* Move target position, one of: child, left, right, root
*
* @var string
*/
protected $position = NULL;
/**
* Memoized 1st boundary.
*
* @var int
*/
protected $_bound1 = NULL;
/**
* Memoized 2nd boundary.
*
* @var int
*/
protected $_bound2 = NULL;
/**
* Memoized boundaries array.
*
* @var array
*/
protected $_boundaries = NULL;
/**
* The event dispatcher instance.
*
* @var \Illuminate\Events\Dispatcher
*/
protected static $dispatcher;
/**
* Create a new Move class instance.
*
* @param \Baum\Node $node
* @param \Baum\Node|int $target
* @param string $position
* @return void
*/
public function __construct($node, $target, $position) {
$this->node = $node;
$this->target = $this->resolveNode($target);
$this->position = $position;
$this->setEventDispatcher($node->getEventDispatcher());
}
/**
* Easy static accessor for performing a move operation.
*
* @param \Baum\Node $node
* @param \Baum\Node|int $target
* @param string $position
* @return \Baum\Node
*/
public static function to($node, $target, $position) {
$instance = new static($node, $target, $position);
return $instance->perform();
}
/**
* Perform the move operation.
*
* @return \Baum\Node
*/
public function perform() {
$this->guardAgainstImpossibleMove();
if ( $this->fireMoveEvent('moving') === false )
return $this->node;
if ( $this->hasChange() ) {
$self = $this;
$this->node->getConnection()->transaction(function() use ($self) {
$self->updateStructure();
});
$this->target->reload();
$this->node->setDepthWithSubtree();
$this->node->reload();
}
$this->fireMoveEvent('moved', false);
return $this->node;
}
/**
* Runs the SQL query associated with the update of the indexes affected
* by the move operation.
*
* @return int
*/
public function updateStructure() {
list($a, $b, $c, $d) = $this->boundaries();
// select the rows between the leftmost & the rightmost boundaries and apply a lock
$this->applyLockBetween($a, $d);
$connection = $this->node->getConnection();
$grammar = $connection->getQueryGrammar();
$currentId = $this->quoteIdentifier($this->node->getKey());
$parentId = $this->quoteIdentifier($this->parentId());
$leftColumn = $this->node->getLeftColumnName();
$rightColumn = $this->node->getRightColumnName();
$parentColumn = $this->node->getParentColumnName();
$wrappedLeft = $grammar->wrap($leftColumn);
$wrappedRight = $grammar->wrap($rightColumn);
$wrappedParent = $grammar->wrap($parentColumn);
$wrappedId = $grammar->wrap($this->node->getKeyName());
$lftSql = "CASE
WHEN $wrappedLeft BETWEEN $a AND $b THEN $wrappedLeft + $d - $b
WHEN $wrappedLeft BETWEEN $c AND $d THEN $wrappedLeft + $a - $c
ELSE $wrappedLeft END";
$rgtSql = "CASE
WHEN $wrappedRight BETWEEN $a AND $b THEN $wrappedRight + $d - $b
WHEN $wrappedRight BETWEEN $c AND $d THEN $wrappedRight + $a - $c
ELSE $wrappedRight END";
$parentSql = "CASE
WHEN $wrappedId = $currentId THEN $parentId
ELSE $wrappedParent END";
$updateConditions = array(
$leftColumn => $connection->raw($lftSql),
$rightColumn => $connection->raw($rgtSql),
$parentColumn => $connection->raw($parentSql)
);
if ( $this->node->timestamps )
$updateConditions[$this->node->getUpdatedAtColumn()] = $this->node->freshTimestamp();
return $this->node
->newNestedSetQuery()
->where(function($query) use ($leftColumn, $rightColumn, $a, $d) {
$query->whereBetween($leftColumn, array($a, $d))
->orWhereBetween($rightColumn, array($a, $d));
})
->update($updateConditions);
}
/**
* Resolves suplied node. Basically returns the node unchanged if
* supplied parameter is an instance of \Baum\Node. Otherwise it will try
* to find the node in the database.
*
* @param \Baum\node|int
* @return \Baum\Node
*/
protected function resolveNode($node) {
if ( $node instanceof \Baum\Node ) return $node->reload();
return $this->node->newNestedSetQuery()->find($node);
}
/**
* Check wether the current move is possible and if not, rais an exception.
*
* @return void
*/
protected function guardAgainstImpossibleMove() {
if ( !$this->node->exists )
throw new MoveNotPossibleException('A new node cannot be moved.');
if ( array_search($this->position, array('child', 'left', 'right', 'root')) === FALSE )
throw new MoveNotPossibleException("Position should be one of ['child', 'left', 'right'] but is {$this->position}.");
if ( !$this->promotingToRoot() ) {
if ( is_null($this->target) ) {
if ( $this->position === 'left' || $this->position === 'right' )
throw new MoveNotPossibleException("Could not resolve target node. This node cannot move any further to the {$this->position}.");
else
throw new MoveNotPossibleException('Could not resolve target node.');
}
if ( $this->node->equals($this->target) )
throw new MoveNotPossibleException('A node cannot be moved to itself.');
if ( $this->target->insideSubtree($this->node) )
throw new MoveNotPossibleException('A node cannot be moved to a descendant of itself (inside moved tree).');
if ( !$this->node->inSameScope($this->target) )
throw new MoveNotPossibleException('A node cannot be moved to a different scope.');
}
}
/**
* Computes the boundary.
*
* @return int
*/
protected function bound1() {
if ( !is_null($this->_bound1) ) return $this->_bound1;
switch ( $this->position ) {
case 'child':
$this->_bound1 = $this->target->getRight();
break;
case 'left':
$this->_bound1 = $this->target->getLeft();
break;
case 'right':
$this->_bound1 = $this->target->getRight() + 1;
break;
case 'root':
$this->_bound1 = $this->node->newNestedSetQuery()->max($this->node->getRightColumnName()) + 1;
break;
}
$this->_bound1 = (($this->_bound1 > $this->node->getRight()) ? $this->_bound1 - 1 : $this->_bound1);
return $this->_bound1;
}
/**
* Computes the other boundary.
* TODO: Maybe find a better name for this... ¿?
*
* @return int
*/
protected function bound2() {
if ( !is_null($this->_bound2) ) return $this->_bound2;
$this->_bound2 = (($this->bound1() > $this->node->getRight()) ? $this->node->getRight() + 1 : $this->node->getLeft() - 1);
return $this->_bound2;
}
/**
* Computes the boundaries array.
*
* @return array
*/
protected function boundaries() {
if ( !is_null($this->_boundaries) ) return $this->_boundaries;
// we have defined the boundaries of two non-overlapping intervals,
// so sorting puts both the intervals and their boundaries in order
$this->_boundaries = array(
$this->node->getLeft() ,
$this->node->getRight() ,
$this->bound1() ,
$this->bound2()
);
sort($this->_boundaries);
return $this->_boundaries;
}
/**
* Computes the new parent id for the node being moved.
*
* @return int
*/
protected function parentId() {
switch( $this->position ) {
case 'root':
return NULL;
case 'child':
return $this->target->getKey();
default:
return $this->target->getParentId();
}
}
/**
* Check wether there should be changes in the downward tree structure.
*
* @return boolean
*/
protected function hasChange() {
return !( $this->bound1() == $this->node->getRight() || $this->bound1() == $this->node->getLeft() );
}
/**
* Check if we are promoting the provided instance to a root node.
*
* @return boolean
*/
protected function promotingToRoot() {
return ($this->position == 'root');
}
/**
* Get the event dispatcher instance.
*
* @return \Illuminate\Events\Dispatcher
*/
public static function getEventDispatcher() {
return static::$dispatcher;
}
/**
* Set the event dispatcher instance.
*
* @param \Illuminate\Events\Dispatcher
* @return void
*/
public static function setEventDispatcher(Dispatcher $dispatcher) {
static::$dispatcher = $dispatcher;
}
/**
* Fire the given move event for the model.
*
* @param string $event
* @param bool $halt
* @return mixed
*/
protected function fireMoveEvent($event, $halt = true) {
if ( !isset(static::$dispatcher) ) return true;
// Basically the same as \Illuminate\Database\Eloquent\Model->fireModelEvent
// but we relay the event into the node instance.
$event = "eloquent.{$event}: ".get_class($this->node);
if (version_compare(app()->version(), '5.8.0', '>=')) {
$method = $halt ? 'until' : 'dispatch';
} else {
$method = $halt ? 'until' : 'fire';
}
return static::$dispatcher->$method($event, $this->node);
}
/**
* Quotes an identifier for being used in a database query.
*
* @param mixed $value
* @return string
*/
protected function quoteIdentifier($value) {
if ( is_null($value) )
return 'NULL';
$connection = $this->node->getConnection();
$pdo = $connection->getPdo();
return $pdo->quote($value);
}
/**
* Applies a lock to the rows between the supplied index boundaries.
*
* @param int $lft
* @param int $rgt
* @return void
*/
protected function applyLockBetween($lft, $rgt) {
$this->node->newQuery()
->where($this->node->getLeftColumnName(), '>=', $lft)
->where($this->node->getRightColumnName(), '<=', $rgt)
->select($this->node->getKeyName())
->lockForUpdate()
->get();
}
}