Skip to content

Commit d2bcfe3

Browse files
committed
Support l5.2 and refactor a bit
1 parent b86e189 commit d2bcfe3

File tree

5 files changed

+47
-62
lines changed

5 files changed

+47
-62
lines changed

README.markdown

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
This is a Laravel 4-5 package for working with trees in a database.
88

9-
__[Support this project](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=5TJUM7FYU5VR2)__
9+
* Laravel 5.2 is supported since v4
10+
* Laravel 5.1 is supported in v3
11+
* Laravel 4 is supported in v2
12+
13+
__[Donate](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=5TJUM7FYU5VR2)__
1014

1115
__Contents:__
1216

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kalnoy/nestedset",
3-
"description": "Nested Set Model for Laravel 4",
3+
"description": "Nested Set Model for Laravel 4-5",
44
"keywords": ["laravel", "nested sets", "nsm", "database", "hierarchy"],
55
"license": "MIT",
66

@@ -13,9 +13,9 @@
1313

1414
"require": {
1515
"php": ">=5.5.9",
16-
"illuminate/support": "5.1 - 5.2",
17-
"illuminate/database": "5.1 - 5.2",
18-
"illuminate/events": "5.1 - 5.2"
16+
"illuminate/support": "5.2.*",
17+
"illuminate/database": "5.2.*",
18+
"illuminate/events": "5.2.*"
1919
},
2020

2121
"autoload": { "psr-4": { "Kalnoy\\Nestedset\\": "src/" } },

src/Node.php

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace Kalnoy\Nestedset;
44

55
use Exception;
6+
use Illuminate\Database\Eloquent\Builder;
67
use Illuminate\Database\Eloquent\Relations\BelongsTo;
78
use Illuminate\Database\Eloquent\Relations\HasMany;
9+
use Illuminate\Database\Eloquent\SoftDeletingScope;
810
use LogicException;
911
use Illuminate\Database\Eloquent\Model as Eloquent;
1012
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
@@ -46,24 +48,6 @@ class Node extends Eloquent
4648
*/
4749
const AFTER = 'after';
4850

49-
/**
50-
* Whether model uses soft delete.
51-
*
52-
* @var bool
53-
*
54-
* @since 1.1
55-
*/
56-
static protected $_softDelete;
57-
58-
/**
59-
* Whether the node is being deleted.
60-
*
61-
* @since 2.0
62-
*
63-
* @var bool
64-
*/
65-
static protected $deleting;
66-
6751
/**
6852
* Pending operation.
6953
*
@@ -97,23 +81,9 @@ protected static function boot()
9781
{
9882
parent::boot();
9983

100-
static::$_softDelete = static::getIsSoftDelete();
101-
10284
static::signOnEvents();
10385
}
10486

105-
/**
106-
* Get whether model uses soft delete.
107-
*
108-
* @return bool
109-
*/
110-
protected static function getIsSoftDelete()
111-
{
112-
$instance = new static;
113-
114-
return method_exists($instance, 'withTrashed');
115-
}
116-
11787
/**
11888
* Sign on model events.
11989
*/
@@ -132,7 +102,7 @@ protected static function signOnEvents()
132102
$model->deleteDescendants();
133103
});
134104

135-
if (static::$_softDelete) {
105+
if (static::usesSoftDelete()) {
136106
static::restoring(function (Node $model) {
137107
static::$deletedAt = $model->{$model->getDeletedAtColumn()};
138108
});
@@ -146,7 +116,7 @@ protected static function signOnEvents()
146116
/**
147117
* {@inheritdoc}
148118
*
149-
* Saves a node in a transaction.
119+
* Saves a node in transaction.
150120
*/
151121
public function save(array $options = array())
152122
{
@@ -158,7 +128,7 @@ public function save(array $options = array())
158128
/**
159129
* {@inheritdoc}
160130
*
161-
* Delete a node in transaction if model is not soft deleting.
131+
* Delete a node in transaction.
162132
*/
163133
public function delete()
164134
{
@@ -208,6 +178,22 @@ protected function callPendingAction()
208178
$this->moved = call_user_func_array([ $this, $method ], $parameters);
209179
}
210180

181+
/**
182+
* @return bool
183+
*/
184+
public static function usesSoftDelete()
185+
{
186+
static $softDelete;
187+
188+
if (is_null($softDelete)) {
189+
$instance = new static;
190+
191+
return $softDelete = method_exists($instance, 'withTrashed');
192+
}
193+
194+
return $softDelete;
195+
}
196+
211197
/**
212198
* Make a root node.
213199
*/
@@ -685,24 +671,19 @@ protected function insertNode($position)
685671
*/
686672
protected function deleteDescendants()
687673
{
688-
if (static::$deleting) return;
689-
690674
$lft = $this->getLft();
691675
$rgt = $this->getRgt();
692676

693-
// Make sure that inner nodes are just deleted and don't touch the tree
694-
// This makes sense in Laravel 4.2
695-
static::$deleting = true;
696-
677+
/** @var QueryBuilder $query */
697678
$query = $this->newQuery()->whereNodeBetween([ $lft + 1, $rgt ]);
698679

699-
if (static::$_softDelete && $this->forceDeleting) {
700-
$query->withTrashed()->forceDelete();
701-
} else {
702-
$query->delete();
680+
// Remove soft deleting scope when user forced delete so that descendants
681+
// are also deleted physically
682+
if ($this->usesSoftDelete() && $this->forceDeleting) {
683+
$query->withoutGlobalScope(SoftDeletingScope::class);
703684
}
704685

705-
static::$deleting = false;
686+
$query->applyScopes()->delete();
706687

707688
if ($this->hardDeleting()) {
708689
$height = $rgt - $lft + 1;
@@ -726,6 +707,7 @@ protected function restoreDescendants($deletedAt)
726707
$this->newQuery()
727708
->whereNodeBetween([ $this->getLft() + 1, $this->getRgt() ])
728709
->where($this->getDeletedAtColumn(), '>=', $deletedAt)
710+
->applyScopes()
729711
->restore();
730712
}
731713

@@ -748,7 +730,7 @@ public function newEloquentBuilder($query)
748730
*/
749731
public function newServiceQuery()
750732
{
751-
return static::$_softDelete ? $this->withTrashed() : $this->newQuery();
733+
return $this->usesSoftDelete() ? $this->withTrashed() : $this->newQuery();
752734
}
753735

754736
/**
@@ -779,9 +761,8 @@ public function newFromBuilder($attributes = array(), $connection = null)
779761
*
780762
* @param Node $parent
781763
*/
782-
public static function create(array $attributes = array(),
783-
Node $parent = null
784-
) {
764+
public static function create(array $attributes = [], Node $parent = null)
765+
{
785766
$children = array_pull($attributes, 'children');
786767

787768
$instance = new static($attributes);
@@ -1146,7 +1127,7 @@ protected function getArrayableRelations()
11461127
*/
11471128
protected function hardDeleting()
11481129
{
1149-
return ! static::$_softDelete or $this->forceDeleting;
1130+
return ! $this->usesSoftDelete() || $this->forceDeleting;
11501131
}
11511132

11521133
/**

tests/NodeTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ public function assertNodeReceivesValidValues($node)
9999
*/
100100
public function findCategory($name, $withTrashed = false)
101101
{
102-
$q = Category::whereName($name);
102+
$q = new Category;
103103

104-
if ($withTrashed) $q->withTrashed();
104+
$q = $withTrashed ? $q->withTrashed() : $q->newQuery();
105105

106-
return $q->first();
106+
return $q->whereName($name)->first();
107107
}
108108

109109
public function testTreeNotBroken()

tests/models/Category.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22

3-
class Category extends Kalnoy\Nestedset\Node {
4-
3+
class Category extends Kalnoy\Nestedset\Node
4+
{
55
use \Illuminate\Database\Eloquent\SoftDeletes;
66

7-
protected $fillable = array('name', 'parent_id');
7+
protected $fillable = array( 'name', 'parent_id' );
88

99
public $timestamps = false;
1010

0 commit comments

Comments
 (0)