Skip to content

Commit 22b0c89

Browse files
committed
Merge branch 'develop'
2 parents d94cde9 + 2353d09 commit 22b0c89

File tree

6 files changed

+258
-56
lines changed

6 files changed

+258
-56
lines changed

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
# Changelog
22

3+
## 1.0.0 - 2019-04-11
4+
5+
- Major overhauled of the `Base Repository` class
6+
- Add **Route Model Binding** to the Repository for faster data query
7+
- Add `before` hook to add query scope
8+
9+
### UPGRADE
10+
11+
```diff
12+
- use PPSpaces\Repositories\Model as Repository;
13+
+ use PPSpaces\Repositories\Repository;
14+
```
15+
16+
### DEPRECIATED
17+
18+
- Drop the use of injecting **Model** from the `__construct`
19+
- repository.model.stub: `all` method will not be part of the generated Repository class
20+
321
## 0.0.9 - 2019-03-27
422

523
- Add `CHANGELOG.md`, It's better to have this

README.md

Lines changed: 96 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
- [Use Case](#use-case)
1414
- [Help](#help)
1515

16+
## Upgrade Notice
17+
18+
> NOTE: The reason why I modified how the `Repository` is being created because I want the `Repository` to use `Route Model Binding` for faster data query.
19+
20+
- [How to upgrade?](#how-to-upgrade)
21+
1622
## What is the Repository Design Pattern
1723

1824
![Repository Design Pattern](assets/repository_pattern.png)
@@ -92,28 +98,47 @@ Update `UserRepository` logic:
9298
```php
9399
namespace App\Http\Repositories;
94100

95-
use App\User;
96101
use PPSpaces\Repositories\Model as Repository;
97102

98103
class UserRepository extends Repository {
99104

100-
protected $user;
101-
102-
public function __construct(User $user) {
103-
$this->user = $user;
105+
/**
106+
* The user model instance.
107+
*
108+
* @var \App\User
109+
*/
110+
protected $model = "App\User";
111+
112+
/**
113+
* Scope a query for the model before executing
114+
*
115+
* @param \Illuminate\Database\Query\Builder $query
116+
* @return void
117+
*/
118+
public function before($query) {
119+
$query->role('staff');
104120
}
105121

106-
public function all($columns = ['*']) {
107-
return $this->user
108-
->role('staff')
109-
->paginate();
122+
/**
123+
* Get all of the models from the database.
124+
*
125+
* @param array|mixed $columns
126+
* @return \Illuminate\Database\Eloquent\Collection|static[]
127+
*/
128+
public function get($columns = ['*']) {
129+
$users = $this->repository
130+
->active()
131+
->orderBy('updated_at', 'DESC')
132+
->get();
133+
134+
return $users;
110135
}
111136
}
112137
```
113138

114139
> NOTE: Check `PPSpaces\Repositories\Model` for available methods that you may override. Keep in mind that you still have access to all Model instance that you've created. The `$this->user` is the instance of your `\App\User` model.
115140
116-
Within your `UserController` assume you have a resource controller created. Inject the `UserRepository` to the contoller.
141+
Within your `UserController` assume you have a resource controller created. Inject the `UserRepository` to the contoller. Now you can access the repository in your controller method:
117142

118143
```php
119144
use App\Http\Repositories\UserRepository;
@@ -126,15 +151,73 @@ class UserController extends Controller
126151
{
127152
$this->users = $users;
128153
}
154+
155+
public function index()
156+
{
157+
return $this->users->get();
158+
}
129159
}
130160
```
131161

132-
Now you can access the repository in your controller method:
162+
Or alternatively, you may use **Route Model Binding** on the controller actions whose `type-hinted` variable names match a route segment name.
163+
164+
> Read more about [Route Model Binding](https://laravel.com/docs/master/routing#route-model-binding) here
133165
134166
```php
135-
public function index()
167+
public function index(UserRepository $user)
136168
{
137-
return $this->users->all();
169+
return $user->get();
170+
}
171+
172+
public function show(UserRepository $user)
173+
{
174+
// Authorizing the repository model
175+
// Check https://laravel.com/docs/master/authorization
176+
$this->authorize('view', $user->model());
177+
178+
// This $user will resolved by the id provided by the router
179+
// e.g. /api/user/1
180+
// $user will be the result of $user->id === 1
181+
return $user;
182+
}
183+
```
184+
185+
## How to upgrade?
186+
187+
> Upgrade from `v0.0.9` or earilier to `v1.0.0`
188+
189+
### What you need to do
190+
191+
```diff
192+
namespace App\Http\Repositories;
193+
194+
- use App\User;
195+
196+
- use PPSpaces\Repositories\Model as Repository;
197+
+ use PPSpaces\Repositories\Repository;
198+
199+
class UserRepository extends Repository {
200+
201+
+ /**
202+
+ * The user model instance.
203+
+ *
204+
+ * @var \App\User
205+
+ */
206+
+ protected $model = "App\User";
207+
208+
- protected $user;
209+
210+
- public function __construct(User $user) {
211+
- $this->user = $user;
212+
- }
213+
214+
public function index()
215+
{
216+
// `$this->users->all()` will always resolved the same result as `$this->users->get()`
217+
- return $this->users->all();
218+
+ return $this->users->get();
219+
}
220+
138221
}
139222
```
140223

src/Console/stubs/repository.model.stub

Lines changed: 5 additions & 17 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,27 +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-
}
26-
27-
/**
28-
* Get all of the models from the database.
29-
*
30-
* @param array|mixed $columns
31-
* @return \Illuminate\Database\Eloquent\Collection|static[]
32-
*/
33-
public function all($columns = ['*']) {
34-
return $this->get($columns);
35-
}
23+
public function before($query) {}
3624

3725
/**
3826
* Dynamically retrieve attributes on the model.

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+
}

0 commit comments

Comments
 (0)