Skip to content

Commit a7cbc45

Browse files
authored
Develop ready to release as next (#26)
* Added Created, Updated and Deleted events, Moved request to each method to allow overloading: * Apply fixes from StyleCI * Scrutinizer updates * Update controller.stub * Feature/requests mapping (#13) * Event accepts request object and any type of request can now be pushed through * Apply fixes from StyleCI (#12) * Request should have a constructor for instances with models * Update BaseRepository.php * Apply fixes from StyleCI (#15) * Fixed namespace type declaration * Fixed namespace type declaration * Fixed namespace type declaration * Feature/requests mapping (#17) * Event accepts request object and any type of request can now be pushed through * Apply fixes from StyleCI (#12) * Request should have a constructor for instances with models * Update BaseRepository.php * Apply fixes from StyleCI (#18) * Feature/policy collections update (#19) * Develop (#14) * Added Created, Updated and Deleted events, Moved request to each method to allow overloading: * Apply fixes from StyleCI * Scrutinizer updates * Update controller.stub * Feature/requests mapping (#13) * Event accepts request object and any type of request can now be pushed through * Apply fixes from StyleCI (#12) * Request should have a constructor for instances with models * Update BaseRepository.php * Apply fixes from StyleCI (#15) * Fixed namespace type declaration * Fixed namespace type declaration * Fixed namespace type declaration * Develop To Master (#16) * Added Created, Updated and Deleted events, Moved request to each method to allow overloading: * Apply fixes from StyleCI * Scrutinizer updates * Update controller.stub * Feature/requests mapping (#13) * Event accepts request object and any type of request can now be pushed through * Apply fixes from StyleCI (#12) * Request should have a constructor for instances with models * Update BaseRepository.php * Apply fixes from StyleCI (#15) * Fixed namespace type declaration * Fixed namespace type declaration * Fixed namespace type declaration * Added resource collections and policy check. * Updated Readme * Formatting * Apply fixes from StyleCI (#20) * Scrutinizer fixes - deprecation / namesapce * Feature/policy collections update (#22) * Develop (#14) * Added Created, Updated and Deleted events, Moved request to each method to allow overloading: * Apply fixes from StyleCI * Scrutinizer updates * Update controller.stub * Feature/requests mapping (#13) * Event accepts request object and any type of request can now be pushed through * Apply fixes from StyleCI (#12) * Request should have a constructor for instances with models * Update BaseRepository.php * Apply fixes from StyleCI (#15) * Fixed namespace type declaration * Fixed namespace type declaration * Fixed namespace type declaration * Develop To Master (#16) * Added Created, Updated and Deleted events, Moved request to each method to allow overloading: * Apply fixes from StyleCI * Scrutinizer updates * Update controller.stub * Feature/requests mapping (#13) * Event accepts request object and any type of request can now be pushed through * Apply fixes from StyleCI (#12) * Request should have a constructor for instances with models * Update BaseRepository.php * Apply fixes from StyleCI (#15) * Fixed namespace type declaration * Fixed namespace type declaration * Fixed namespace type declaration * Added resource collections and policy check. * Updated Readme * Formatting * Apply fixes from StyleCI (#20) * Scrutinizer fixes - deprecation / namesapce * Added middleware and query qualifiers * Policy Update * Post adding new and artisan updated, code cleanup * Apply fixes from StyleCI (#21) * Scrutiniser fixes * Put now handles relations * Fix return types * Minor updates to fix relationships and make command * Sanke case vs camel case * Apply fixes from StyleCI (#23) * Minor bugfix for case sensitifity * Fixed make command * model is static * Apply fixes from StyleCI (#24) * Scrutiniser fixes * Apply fixes from StyleCI (#25) * Apply fixes from StyleCI (#27) * Policiies auth mehtod without users * user not needed * Tidy up uri parser class * Fixes * fix more
1 parent e2eca7f commit a7cbc45

23 files changed

+1151
-537
lines changed

README.md

+45-8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,36 @@ You can override the methods by simply putting in your own methods to override -
4242
* PUT (class::update) - triggers a new `Phpsa\LaravelApiController\Events\Updated` Event which has the updated record available as `$record`
4343
* DELETE (class::destry) - triggers a new `Phpsa\LaravelApiController\Events\Deleted` Event which has the deleted record available as `$record`
4444

45+
## Policies
46+
47+
Policies: https://laravel.com/docs/6.x/authorization#generating-policies
48+
49+
Generate with `php artisan make:policy PostPolicy --model=Post`
50+
51+
* Get list - calls the `viewAny` policy
52+
* Get single - calls the `view` policy
53+
* Post New - calls the `create` policy
54+
* Put Update - calls the `update` policy
55+
* Delete item - calls the `delete` policy
56+
57+
Query/Data modifiers in policies for the api endpoints
58+
59+
* `qualifyCollectionQueryWithUser($user, $repository)` -> return void - add any queries to the repository (ie ->where('x','))
60+
* `qualifyItemQueryWithUser($user, $repository)`-> return void - add any queries to the repository (ie ->where('x','))
61+
* `qualifyStoreDataWithUser($data)` - return the updated data array
62+
* `qualifyUpdateDataWithUser($data)` - return the updated data array
63+
64+
## Resources / Collections (Transforming)
65+
Resources: https://laravel.com/docs/6.x/eloquent-resources
66+
67+
Generate with
68+
`php artisan make:resource UserResource` and `php artisan make:resource UserCollection`
69+
70+
in your controller override the following params:
71+
```php
72+
protected $resourceSingle = UserResource::class;
73+
protected $resourceCollection = UserCollection::class;
74+
```
4575

4676
## Filtering
4777

@@ -106,13 +136,16 @@ see https://laravel.com/docs/5.8/validation#conditionally-adding-rules
106136

107137
The following parameters are set in the Base Api controller and can be overwritten by your Controller on a case by case basis:
108138

109-
* `protected $resourceKeySingular = 'data';` Resource key for an item.
110-
* `protected $resourceKeyPlural = 'data';` Resource key for a collection.
111-
* `protected $defaultFields = ['*'];` Default Fields to respond with
112-
* `protected $defaultSort = null;` Set the default sorting for queries.
113-
* `protected $defaultLimit = 25;` Number of items displayed at once if not specified. (0 = maximumLimit)
114-
* `protected $maximumLimit = 0;` Maximum limit that can be set via $_GET['limit']. - this ties in with the defaultLimit aswell, and if wanting to disable pagination , both should be 0. ) will allow all records to be returned in a single call.
115-
* `protected $unguard = false;` Do we need to unguard the model before create/update?
139+
* **DEPRECATED** `protected $resourceKeySingular = 'data';`
140+
* **DEPRECATED** `protected $resourceKeyPlural = 'data';`
141+
142+
* `protected $resourceSingle = JsonResource::class;` Collection to use for your single resource
143+
* `protected $resourceCollection = ResourceCollection::class;` Collection to use for your resource collection
144+
* `protected $defaultFields = ['*'];` Default Fields to respond with
145+
* `protected $defaultSort = null;` Set the default sorting for queries.
146+
* `protected $defaultLimit = 25;` Number of items displayed at once if not specified. (0 = maximumLimit)
147+
* `protected $maximumLimit = 0;` Maximum limit that can be set via $_GET['limit']. - this ties in with the defaultLimit aswell, and if wanting to disable pagination , both should be 0. ) will allow all records to be returned in a single call.
148+
* `protected $unguard = false;` Do we need to unguard the model before create/update?
116149

117150
## Security
118151

@@ -122,7 +155,11 @@ instead of using the issue tracker.
122155
## Credits
123156

124157
- [Craig G Smith](https://github.com/phpsa)
158+
- [Phil Taylor]()
125159
- [All contributors](https://github.com/phpsa/laravel-api-controller/graphs/contributors)
126160

127-
[badge_laravel]: https://img.shields.io/badge/Laravel-5.8%20to%205.8-orange.svg?style=flat-square
161+
## Sponsors
162+
- [Custom D](https://customd.com)
163+
164+
[badge_laravel]: https://img.shields.io/badge/Laravel-5.8%20to%206-orange.svg?style=flat-square
128165
[badge_issues]: https://img.shields.io/github/issues/ARCANEDEV/Support.svg?style=flat-square

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
],
1717
"require": {
1818
"php": ">=7.1.3",
19-
"illuminate/support": "~5.8.0|~6.0|~6.1"
19+
"illuminate/support": "~5.8.0|^6.0"
2020
},
2121
"require-dev": {
2222
"orchestra/testbench": "~3.8.0",

config/laravel-api-controller.php

+10-21
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
<?php
22

33
return [
4-
/*
5-
* Relative path from the app directory to api controllers directory.
6-
*/
7-
'controllers_dir' => 'Http/Controllers/Api',
8-
/*
9-
* Relative path from the app directory to the api routes file.
10-
*/
11-
'routes_file' => '../routes/api.php',
12-
/*
13-
* Relative path from the app directory to the models directory. Typically it's either 'Models' or ''.
14-
*/
15-
'models_base_dir' => 'Models',
16-
/*
17-
* Relative path from the base directory to the api controller stub.
18-
*/
19-
'controller_stub' => 'vendor/phpsa/laravel-api-controller/src/Generator/stubs/controller.stub',
20-
/*
21-
* Relative path from the base directory to the route stub.
22-
*/
23-
'route_stub' => 'vendor/phpsa/laravel-api-controller/src/Generator/stubs/route.stub',
4+
// Relative path from the app directory to api controllers directory.
5+
'controllers_dir' => 'Http/Controllers/Api',
6+
// Relative path from the app directory to the api routes file.
7+
'routes_file' => '../routes/api.php',
8+
// Relative path from the app directory to the models directory. Typically it's either 'Models' or ''.
9+
'models_base_dir' => 'Models',
10+
// Relative path from the base directory to the api controller stub.
11+
'controller_stub' => 'vendor/phpsa/laravel-api-controller/src/Generator/stubs/controller.stub',
12+
// Relative path from the base directory to the route stub.
13+
'route_stub' => 'vendor/phpsa/laravel-api-controller/src/Generator/stubs/route.stub',
2414

2515
'parameters' => [
2616
'include' => 'include',
@@ -30,5 +20,4 @@
3020
'page' => 'page',
3121
'group' => 'group',
3222
],
33-
3423
];

src/Contracts/ModelRepository.php

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
namespace Phpsa\LaravelApiController\Contracts;
4+
5+
use Illuminate\Support\Facades\Schema;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Phpsa\LaravelApiController\Exceptions\ApiException;
8+
use Phpsa\LaravelApiController\Repository\BaseRepository;
9+
10+
trait ModelRepository
11+
{
12+
/**
13+
* Eloquent model instance.
14+
*
15+
* @var mixed|Model instance
16+
*/
17+
protected static $model;
18+
19+
/**
20+
* Repository instance.
21+
*
22+
* @var mixed|BaseRepository
23+
*/
24+
protected $repository;
25+
26+
/**
27+
* Holds the available table columns.
28+
*
29+
* @var array
30+
*/
31+
protected $tableColumns = [];
32+
33+
/**
34+
* @throws ApiException
35+
*/
36+
protected function makeModel(): void
37+
{
38+
$model = resolve($this->model());
39+
40+
if (! $model instanceof Model) {
41+
throw new ApiException("Class {$this->model()} must be an instance of " . Model::class);
42+
}
43+
44+
self::$model = $model;
45+
}
46+
47+
/**
48+
* Creates our repository linkage.
49+
*/
50+
protected function makeRepository()
51+
{
52+
$this->repository = BaseRepository::withModel($this->model());
53+
}
54+
55+
/**
56+
* Set which columns area available in the model.
57+
*
58+
* @param Model $model
59+
*/
60+
protected function setTableColumns(?Model $model = null): void
61+
{
62+
if (is_null($model)) {
63+
$model = self::$model;
64+
}
65+
$table = $model->getTable();
66+
$this->tableColumns[$table] = Schema::getColumnListing($table);
67+
}
68+
69+
/**
70+
* gets avaialble columns for the table.
71+
*
72+
* @param Model $model
73+
*
74+
* @return array
75+
*/
76+
protected function getTableColumns(?Model $model = null): array
77+
{
78+
if (is_null($model)) {
79+
$model = self::$model;
80+
}
81+
82+
$table = $model->getTable();
83+
84+
if (! isset($this->tableColumns[$table])) {
85+
$this->setTableColumns($model);
86+
}
87+
88+
return $this->tableColumns[$table];
89+
}
90+
91+
/**
92+
* Eloquent model.
93+
*
94+
* @return string (model classname)
95+
*/
96+
abstract protected function model();
97+
98+
/**
99+
* Unguard eloquent model if needed.
100+
*/
101+
protected function unguardIfNeeded()
102+
{
103+
if ($this->unguard) {
104+
self::$model->unguard();
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)