22
33namespace PPSpaces \Repositories ;
44
5+ use ReflectionClass ;
56use JsonSerializable ;
6- use Illuminate \Support \Traits \ForwardsCalls ;
7+
8+ use Illuminate \Container \Container as App ;
9+
10+ use Illuminate \Database \Eloquent \Model ;
711use Illuminate \Contracts \Routing \UrlRoutable ;
8- use PPSpaces \Contracts \Model as RepositoryContract ;
912
10- abstract class Model implements RepositoryContract, JsonSerializable, UrlRoutable {
13+ use PPSpaces \Exceptions \RepositoryException ;
14+ use PPSpaces \Contracts \Repository as RepositoryContract ;
1115
12- use ForwardsCalls;
16+ abstract class Repository implements RepositoryContract, JsonSerializable, UrlRoutable {
17+
18+ /**
19+ * The application instance being facaded.
20+ *
21+ * @var \Illuminate\Container\Container
22+ */
23+ protected $ app ;
24+
25+ /**
26+ * The Model class name.
27+ *
28+ * @var string
29+ */
30+ protected $ model ;
1331
1432 /**
1533 * The repository instance.
1634 *
17- * @var \Illuminate\Database\Eloquent\Model
35+ * @var mixed
1836 */
1937 protected $ repository ;
2038
39+ /**
40+ * The model instance.
41+ *
42+ * @var \Illuminate\Database\Eloquent\Model
43+ */
44+ protected $ resolver ;
45+
46+ /**
47+ * The resolved model instance.
48+ *
49+ * @var \Illuminate\Database\Eloquent\Model
50+ */
51+ protected $ resolved ;
52+
2153 /**
2254 * Create a new repository instance.
2355 *
2456 * @param \Illuminate\Database\Eloquent\Model $model
2557 * @return void
2658 */
27- public function __construct ($ model ) {
28- $ this ->repository = $ model ;
59+ public function __construct (App $ app ) {
60+ $ this ->app = $ app ;
61+
62+ $ this ->initializeRepository ();
2963 }
3064
65+ /**
66+ * Scope a query for the model before executing
67+ *
68+ * @param \Illuminate\Database\Query\Builder $query
69+ * @return void
70+ */
71+ public function before ($ query ) {}
72+
3173 /**
3274 * Get all of the models from the database.
3375 *
@@ -100,14 +142,42 @@ public static function destroy($ids) {
100142 //
101143 }
102144
145+ /**
146+ * Specify Model class name
147+ *
148+ * @return string
149+ */
150+ public function model () {
151+ return $ this ->model ;
152+ }
153+
154+ /**
155+ * Resolve the given model from the container.
156+ *
157+ * @return \Illuminate\Database\Eloquent\Builder
158+ *
159+ * @throws PPSpaces\Exceptions\RepositoryException
160+ */
161+ public function initializeRepository () {
162+ $ model = $ this ->app ->make ($ this ->model );
163+
164+ if (!$ model instanceof Model) {
165+ throw new RepositoryException ("Class {$ this ->model } must be an instance of Illuminate \\Database \\Eloquent \\Model " );
166+ }
167+
168+ $ this ->repository = $ model ->newQuery ();
169+
170+ $ this ->before ($ this ->repository );
171+ }
172+
103173 /**
104174 * Get the value of the model's route key.
105175 *
106176 * @return mixed
107177 */
108178 public function getRouteKey ()
109179 {
110- return $ this ->repository ->getAttribute ($ this ->getRouteKeyName ());
180+ return $ this ->resolver ->getAttribute ($ this ->getRouteKeyName ());
111181 }
112182
113183 /**
@@ -117,7 +187,7 @@ public function getRouteKey()
117187 */
118188 public function getRouteKeyName ()
119189 {
120- return $ this ->repository ->getKeyName ();
190+ return $ this ->resolver ->getKeyName ();
121191 }
122192
123193 /**
@@ -128,7 +198,11 @@ public function getRouteKeyName()
128198 */
129199 public function resolveRouteBinding ($ value )
130200 {
131- return $ this ->repository ->where ($ this ->getRouteKeyName (), $ value )->first ();
201+ $ this ->resolver = (new $ this ->model );
202+
203+ $ this ->resolved = $ this ->resolver ->where ($ this ->getRouteKeyName (), $ value )->first () ?? abort (404 );
204+
205+ return $ this ;
132206 }
133207
134208 /**
@@ -138,7 +212,17 @@ public function resolveRouteBinding($value)
138212 */
139213 public function jsonSerialize ()
140214 {
141- return $ this ->repository ->toArray ();
215+ return $ this ->resolved ->toArray ();
216+ }
217+
218+ /**
219+ * Convert the model instance to an array.
220+ *
221+ * @return array
222+ */
223+ public function toArray ()
224+ {
225+ return $ this ->resolved ->toArray ();
142226 }
143227
144228 /**
@@ -148,6 +232,6 @@ public function jsonSerialize()
148232 */
149233 public function __toString ()
150234 {
151- return $ this ->repository ->toJson ();
235+ return $ this ->resolved ->toJson ();
152236 }
153237}
0 commit comments