Skip to content

Commit 29fb025

Browse files
committed
Re-work resolveRouteBinding for Base Repository
- Update repository.mode.stub - Update Base Repository class - Update Repository Contract - Create RepositoryException
1 parent 2eb267e commit 29fb025

File tree

4 files changed

+133
-21
lines changed

4 files changed

+133
-21
lines changed

src/Console/stubs/repository.model.stub

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace DummyNamespace;
44

55
use DummyFullModelClass;
6-
use PPSpaces\Repositories\Model as Repository;
6+
use PPSpaces\Repositories\Repository;
77

88
class DummyClass extends Repository {
99

@@ -12,17 +12,15 @@ class DummyClass extends Repository {
1212
*
1313
* @var \DummyFullModelClass
1414
*/
15-
protected $repository;
15+
protected $model = "DummyFullModelClass";
1616

1717
/**
18-
* Create a new repository instance.
18+
* Scope a query for the model before executing
1919
*
20-
* @param \DummyFullModelClass $DummyModelVariable
20+
* @param \Illuminate\Database\Query\Builder $query
2121
* @return void
2222
*/
23-
public function __construct(DummyModelClass $DummyModelVariable) {
24-
$this->repository = $DummyModelVariable;
25-
}
23+
public function before($query) {}
2624

2725
/**
2826
* Get all of the models from the database.

src/Contracts/Model.php renamed to src/Contracts/Repository.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@
22

33
namespace PPSpaces\Contracts;
44

5-
interface Model
5+
interface Repository
66
{
77

8+
/**
9+
* Scope a query for the model before executing
10+
*
11+
* @param \Illuminate\Database\Query\Builder $query
12+
* @return void
13+
*/
14+
public function before($query);
15+
816
/**
917
* Get all of the models from the database.
1018
*
@@ -65,9 +73,18 @@ public function delete($id);
6573
public static function destroy($ids);
6674

6775
/**
68-
* Convert the model to its string representation.
76+
* Specify Model class name
6977
*
7078
* @return string
7179
*/
72-
public function __toString();
80+
public function model();
81+
82+
/**
83+
* Resolve the given model from the container.
84+
*
85+
* @return \Illuminate\Database\Eloquent\Builder
86+
*
87+
* @throws PPSpaces\Exceptions\RepositoryException
88+
*/
89+
public function makeRepository();
7390
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace PPSpaces\Exceptions;
4+
5+
use Exception;
6+
7+
class RepositoryException extends Exception
8+
{
9+
//
10+
}

src/Repositories/Model.php renamed to src/Repositories/Repository.php

Lines changed: 98 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,77 @@
22

33
namespace PPSpaces\Repositories;
44

5+
use ReflectionClass;
56
use JsonSerializable;
7+
8+
use Illuminate\Container\Container as App;
9+
10+
use Illuminate\Database\Eloquent\Model;
611
use Illuminate\Support\Traits\ForwardsCalls;
712
use Illuminate\Contracts\Routing\UrlRoutable;
8-
use PPSpaces\Contracts\Model as RepositoryContract;
913

10-
abstract class Model implements RepositoryContract, JsonSerializable, UrlRoutable {
14+
use PPSpaces\Exceptions\RepositoryException;
15+
use PPSpaces\Contracts\Repository as RepositoryContract;
16+
17+
abstract class Repository implements RepositoryContract, JsonSerializable, UrlRoutable {
18+
19+
// use ForwardsCalls;
1120

12-
use ForwardsCalls;
21+
/**
22+
* The application instance being facaded.
23+
*
24+
* @var \Illuminate\Container\Container
25+
*/
26+
protected $app;
27+
28+
/**
29+
* The Model class name.
30+
*
31+
* @var string
32+
*/
33+
protected $model;
1334

1435
/**
1536
* The repository instance.
1637
*
17-
* @var \Illuminate\Database\Eloquent\Model
38+
* @var mixed
1839
*/
1940
protected $repository;
2041

42+
/**
43+
* The model instance.
44+
*
45+
* @var \Illuminate\Database\Eloquent\Model
46+
*/
47+
protected $resolver;
48+
49+
/**
50+
* The resolved model instance.
51+
*
52+
* @var \Illuminate\Database\Eloquent\Model
53+
*/
54+
protected $resolved;
55+
2156
/**
2257
* Create a new repository instance.
2358
*
2459
* @param \Illuminate\Database\Eloquent\Model $model
2560
* @return void
2661
*/
27-
public function __construct($model) {
28-
$this->repository = $model;
62+
public function __construct(App $app) {
63+
$this->app = $app;
64+
65+
$this->makeRepository();
2966
}
3067

68+
/**
69+
* Scope a query for the model before executing
70+
*
71+
* @param \Illuminate\Database\Query\Builder $query
72+
* @return void
73+
*/
74+
public function before($query) {}
75+
3176
/**
3277
* Get all of the models from the database.
3378
*
@@ -100,14 +145,42 @@ public static function destroy($ids) {
100145
//
101146
}
102147

148+
/**
149+
* Specify Model class name
150+
*
151+
* @return string
152+
*/
153+
public function model() {
154+
return $this->model;
155+
}
156+
157+
/**
158+
* Resolve the given model from the container.
159+
*
160+
* @return \Illuminate\Database\Eloquent\Builder
161+
*
162+
* @throws PPSpaces\Exceptions\RepositoryException
163+
*/
164+
public function makeRepository() {
165+
$model = $this->app->make($this->model);
166+
167+
if (!$model instanceof Model) {
168+
throw new RepositoryException("Class {$this->model} must be an instance of Illuminate\\Database\\Eloquent\\Model");
169+
}
170+
171+
$this->repository = $model->newQuery();
172+
173+
$this->before($this->repository);
174+
}
175+
103176
/**
104177
* Get the value of the model's route key.
105178
*
106179
* @return mixed
107180
*/
108181
public function getRouteKey()
109182
{
110-
return $this->repository->getAttribute($this->getRouteKeyName());
183+
return $this->resolver->getAttribute($this->getRouteKeyName());
111184
}
112185

113186
/**
@@ -117,7 +190,7 @@ public function getRouteKey()
117190
*/
118191
public function getRouteKeyName()
119192
{
120-
return $this->repository->getKeyName();
193+
return $this->resolver->getKeyName();
121194
}
122195

123196
/**
@@ -128,7 +201,11 @@ public function getRouteKeyName()
128201
*/
129202
public function resolveRouteBinding($value)
130203
{
131-
return $this->repository->where($this->getRouteKeyName(), $value)->first();
204+
$this->resolver = (new $this->model);
205+
206+
$this->resolved = $this->resolver->where($this->getRouteKeyName(), $value)->first() ?? abort(404);
207+
208+
return $this;
132209
}
133210

134211
/**
@@ -138,7 +215,17 @@ public function resolveRouteBinding($value)
138215
*/
139216
public function jsonSerialize()
140217
{
141-
return $this->repository->toArray();
218+
return $this->resolved->toArray();
219+
}
220+
221+
/**
222+
* Convert the model instance to an array.
223+
*
224+
* @return array
225+
*/
226+
public function toArray()
227+
{
228+
return $this->resolved->toArray();
142229
}
143230

144231
/**
@@ -148,6 +235,6 @@ public function jsonSerialize()
148235
*/
149236
public function __toString()
150237
{
151-
return $this->repository->toJson();
238+
return $this->resolved->toJson();
152239
}
153240
}

0 commit comments

Comments
 (0)