1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Sco\Admin\Repositories; |
5
|
|
|
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
8
|
|
|
use Illuminate\Foundation\Application; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use Sco\Admin\Contracts\RepositoryInterface; |
11
|
|
|
use Sco\Admin\Contracts\WithModel; |
12
|
|
|
|
13
|
|
|
class Repository implements RepositoryInterface, WithModel |
14
|
|
|
{ |
15
|
|
|
protected $app; |
16
|
|
|
|
17
|
|
|
protected $model; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Model |
21
|
|
|
*/ |
22
|
|
|
protected $class; |
23
|
|
|
|
24
|
|
|
protected $with = []; |
25
|
|
|
|
26
|
|
|
protected $globalScopes = []; |
27
|
|
|
|
28
|
|
|
public function __construct(Application $app) |
29
|
|
|
{ |
30
|
|
|
$this->app = $app; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getModel() |
34
|
|
|
{ |
35
|
|
|
return $this->model; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function setModel(Model $model) |
39
|
|
|
{ |
40
|
|
|
$this->model = $model; |
41
|
|
|
$this->class = get_class($model); |
|
|
|
|
42
|
|
|
|
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return string[] |
48
|
|
|
*/ |
49
|
|
|
public function getWith() |
50
|
|
|
{ |
51
|
|
|
return $this->with; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function with($relations) |
55
|
|
|
{ |
56
|
|
|
$this->with = array_flatten(func_get_args()); |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getClass() |
62
|
|
|
{ |
63
|
|
|
return $this->class; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function setClass($class) |
67
|
|
|
{ |
68
|
|
|
if (!class_exists($class)) { |
69
|
|
|
throw new InvalidArgumentException("Model class {$class} not found."); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->class = $class; |
73
|
|
|
$this->setModel( |
74
|
|
|
new $class() |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function getQuery() |
84
|
|
|
{ |
85
|
|
|
$model = $this->getModel(); |
86
|
|
|
foreach ($this->getGlobalScopes() as $identifier => $scope) { |
87
|
|
|
$model::addGlobalScope($identifier, $scope); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $model->query()->with($this->getWith()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function addGlobalScope($scopes) |
94
|
|
|
{ |
95
|
|
|
$this->globalScopes = $scopes; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getGlobalScopes() |
99
|
|
|
{ |
100
|
|
|
return $this->globalScopes; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function find($id) |
104
|
|
|
{ |
105
|
|
|
$query = $this->getQuery(); |
106
|
|
|
if ($this->isRestorable()) { |
107
|
|
|
$query->withTrashed(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $query->find($id); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function findOrFail($id) |
114
|
|
|
{ |
115
|
|
|
$query = $this->getQuery(); |
116
|
|
|
if ($this->isRestorable()) { |
117
|
|
|
$query->withTrashed(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $query->findOrFail($id); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
|
|
public function findOnlyTrashed($id) |
127
|
|
|
{ |
128
|
|
|
return $this->getQuery()->onlyTrashed()->findOrFail($id); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/*public function store() |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function update() |
136
|
|
|
{ |
137
|
|
|
}*/ |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
public function forceDelete($id) |
141
|
|
|
{ |
142
|
|
|
return $this->findOnlyTrashed($id)->forceDelete(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function restore($id) |
146
|
|
|
{ |
147
|
|
|
return $this->findOnlyTrashed($id)->restore(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
public function isRestorable() |
152
|
|
|
{ |
153
|
|
|
return in_array(SoftDeletes::class, class_uses_recursive($this->getClass())); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..