Skip to content

Commit 654dfbc

Browse files
committed
Merge branch 'feature/base-rework' into develop
2 parents 2eb267e + 33f05c2 commit 654dfbc

File tree

4 files changed

+150
-22
lines changed

4 files changed

+150
-22
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 initializeRepository();
7390
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace PPSpaces\Exceptions;
4+
5+
use Exception;
6+
7+
class RepositoryException extends Exception
8+
{
9+
/**
10+
* Report the exception.
11+
*
12+
* @return void
13+
*/
14+
public function report()
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Render the exception into an HTTP response.
21+
*
22+
* @param \Illuminate\Http\Request
23+
* @return \Illuminate\Http\Response
24+
*/
25+
public function render($request)
26+
{
27+
// return response(...);
28+
}
29+
}

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

Lines changed: 96 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,74 @@
22

33
namespace PPSpaces\Repositories;
44

5+
use ReflectionClass;
56
use JsonSerializable;
6-
use Illuminate\Support\Traits\ForwardsCalls;
7+
8+
use Illuminate\Container\Container as App;
9+
10+
use Illuminate\Database\Eloquent\Model;
711
use 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

Comments
 (0)