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 Sco\Admin\Contracts\RepositoryInterface; |
10
|
|
|
use Sco\Admin\Exceptions\RepositoryException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @method static \Illuminate\Database\Eloquent\Model getKeyName() |
14
|
|
|
*/ |
15
|
|
|
class Repository implements RepositoryInterface |
16
|
|
|
{ |
17
|
|
|
protected $app; |
18
|
|
|
|
19
|
|
|
protected $model; |
20
|
|
|
|
21
|
|
|
protected $class; |
22
|
|
|
|
23
|
|
|
public function __construct(Application $app) |
24
|
|
|
{ |
25
|
|
|
$this->app = $app; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getModel() |
29
|
|
|
{ |
30
|
|
|
return $this->model; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function setModel(Model $model) |
34
|
|
|
{ |
35
|
|
|
$this->model = $model; |
36
|
|
|
$this->class = get_class($model); |
37
|
|
|
|
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getClass() |
42
|
|
|
{ |
43
|
|
|
return $this->class; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function setClass($class) |
47
|
|
|
{ |
48
|
|
|
if (!class_exists($class)) { |
49
|
|
|
throw new RepositoryException("Class {$class} not found."); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->class = $class; |
53
|
|
|
$this->setModel( |
54
|
|
|
new $class() |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $id |
62
|
|
|
* |
63
|
|
|
* @return Model |
64
|
|
|
*/ |
65
|
|
|
public function findOnlyTrashed($id) |
66
|
|
|
{ |
67
|
|
|
return $this->getModel()->onlyTrashed()->findOrFail($id); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
public function store() |
72
|
|
|
{ |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function update() |
76
|
|
|
{ |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
|
81
|
|
|
public function forceDelete($id) |
82
|
|
|
{ |
83
|
|
|
return $this->findOnlyTrashed($id)->forceDelete(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function restore($id) |
87
|
|
|
{ |
88
|
|
|
return $this->findOnlyTrashed($id)->restore(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
public function isRestorable() |
93
|
|
|
{ |
94
|
|
|
return in_array(SoftDeletes::class, class_uses_recursive($this->getClass())); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Handle dynamic method calls into the model. |
99
|
|
|
* |
100
|
|
|
* @param string $method |
101
|
|
|
* @param array $parameters |
102
|
|
|
* |
103
|
|
|
* @return mixed |
104
|
|
|
*/ |
105
|
|
|
public function __call($method, $parameters) |
106
|
|
|
{ |
107
|
|
|
return $this->getModel()->$method(...$parameters); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Handle dynamic static method calls into the method. |
112
|
|
|
* |
113
|
|
|
* @param string $method |
114
|
|
|
* @param array $parameters |
115
|
|
|
* |
116
|
|
|
* @return mixed |
117
|
|
|
*/ |
118
|
|
|
public static function __callStatic($method, $parameters) |
119
|
|
|
{ |
120
|
|
|
return (new static)->$method(...$parameters); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
This check looks for function calls that miss required arguments.