|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the site package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Tinyissue\Repository; |
|
12
|
|
|
|
|
13
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
14
|
|
|
use Tinyissue\Contracts\Repository\RepositoryUpdater; |
|
|
|
|
|
|
15
|
|
|
use Tinyissue\Extensions\Auth\LoggedUser; |
|
16
|
|
|
|
|
17
|
|
|
abstract class Repository |
|
18
|
|
|
{ |
|
19
|
|
|
use LoggedUser; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Model |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $model; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(Model $model) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->model = $model; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return Model |
|
33
|
|
|
*/ |
|
34
|
|
|
public function getModel() |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->model; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// Declaration of Tinyissue\Repository\Repository::setModel() must be compatible with Tinyissue\Contracts\Repository\RepositoryInterface::setModel(Tinyissue\Contracts\Repository\Model $model) |
|
40
|
|
|
/** |
|
41
|
|
|
* @param Model $model |
|
42
|
|
|
* |
|
43
|
|
|
* @return $this |
|
44
|
|
|
*/ |
|
45
|
|
|
public function setModel(Model $model) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->model = $model; |
|
48
|
|
|
|
|
49
|
|
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected $updater; |
|
53
|
|
|
protected $counter; |
|
54
|
|
|
protected $updaterClass; |
|
55
|
|
|
protected $counterClass; |
|
56
|
|
|
|
|
57
|
|
|
protected function getRelatedRepository($property, $className) |
|
58
|
|
|
{ |
|
59
|
|
|
if (is_null($this->$property)) { |
|
60
|
|
|
$this->$property = new $this->{$className}($this->getModel()); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $this->$property; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return RepositoryUpdater |
|
68
|
|
|
*/ |
|
69
|
|
|
public function updater() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->getRelatedRepository('updater', 'updaterClass'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return RepositoryUpdater |
|
76
|
|
|
*/ |
|
77
|
|
|
public function counter() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->getRelatedRepository('counter', 'counterClass'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function load($id) |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->setModel($this->getById($id)); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function hasModel() |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->getModel() && $this->getModel()->id > 0; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getById($id) |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->model->find($id); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function requireById($id) |
|
98
|
|
|
{ |
|
99
|
|
|
$model = $this->getById($id); |
|
100
|
|
|
|
|
101
|
|
|
if (!$model) { |
|
102
|
|
|
throw new \DomainException('Un'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $model; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
// public function getAll() |
|
109
|
|
|
// { |
|
110
|
|
|
// return $this->model->all(); |
|
111
|
|
|
// } |
|
112
|
|
|
// |
|
113
|
|
|
// public function getPaginated($count) |
|
114
|
|
|
// { |
|
115
|
|
|
// return $this->model->paginate($count); |
|
116
|
|
|
// } |
|
117
|
|
|
|
|
118
|
|
|
public function newModel() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->getModel()->newInstance(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param array $columns |
|
|
|
|
|
|
125
|
|
|
* |
|
126
|
|
|
* @return mixed |
|
127
|
|
|
*/ |
|
128
|
|
|
public function all() |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->getModel()->orderBy('id')->all(); |
|
131
|
|
|
// TODO: Implement all() method. |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param int $perPage |
|
136
|
|
|
* @param array $columns |
|
137
|
|
|
* |
|
138
|
|
|
* @return mixed |
|
139
|
|
|
*/ |
|
140
|
|
|
public function paginate($perPage = 15, $columns = ['*']) |
|
|
|
|
|
|
141
|
|
|
{ |
|
142
|
|
|
// TODO: Implement paginate() method. |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param $id |
|
147
|
|
|
* @param array $columns |
|
148
|
|
|
* |
|
149
|
|
|
* @return mixed |
|
150
|
|
|
*/ |
|
151
|
|
|
public function find($id, $columns = ['*']) |
|
|
|
|
|
|
152
|
|
|
{ |
|
153
|
|
|
// TODO: Implement find() method. |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function __get($name) |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->getModel()->{$name}; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function __call($name, $arguments) |
|
162
|
|
|
{ |
|
163
|
|
|
$caller = false; |
|
164
|
|
|
|
|
165
|
|
|
if (strpos($name, 'count') === 0) { |
|
166
|
|
|
$caller = $this->counter(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
if (!is_null($this->upater) && method_exists($this->upater, $name)) { |
|
|
|
|
|
|
170
|
|
|
$caller = $this->updater(); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
if (!is_null($this->model) && method_exists($this->model, $name)) { |
|
174
|
|
|
$caller = $this->getModel(); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
if (false !== $caller) { |
|
178
|
|
|
return $caller->{$name}(...$arguments); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
throw new \DomainException(sprintf('Call to undefined method "%s".', $name)); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: