From 53c21a0c19d2c2592abecaf6af1b5c9c86b35f3d Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Thu, 9 Jan 2020 15:33:14 +0200 Subject: [PATCH 1/2] Allow dependency injection --- src/Http/Requests/InteractWithRepositories.php | 8 ++++++-- src/Repositories/Crudable.php | 6 ++++-- src/Repositories/Repository.php | 12 ++++++++++-- src/Repositories/RepositoryCollection.php | 6 +++++- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/Http/Requests/InteractWithRepositories.php b/src/Http/Requests/InteractWithRepositories.php index adff405c0..bc8f20c2e 100644 --- a/src/Http/Requests/InteractWithRepositories.php +++ b/src/Http/Requests/InteractWithRepositories.php @@ -84,7 +84,9 @@ public function newRepository() { $repository = $this->repository(); - return new $repository($repository::newModel()); + return resolve($repository, [ + 'model' => $repository::newModel(), + ])->withResource($repository::newModel()); } /** @@ -115,7 +117,9 @@ public function newRepositoryWith($model) { $repository = $this->repository(); - return new $repository($model); + return resolve($repository, [ + 'model' => $model, + ])->withResource($model); } /** diff --git a/src/Repositories/Crudable.php b/src/Repositories/Crudable.php index fcfe98282..9cf58522a 100644 --- a/src/Repositories/Crudable.php +++ b/src/Repositories/Crudable.php @@ -22,7 +22,9 @@ trait Crudable */ public function index(RestifyRequest $request, Paginator $paginated) { - return (new static($paginated))->response(); + return resolve(static::class, [ + 'model' => $paginated + ])->withResource($paginated)->response(); } /** @@ -61,7 +63,7 @@ public function store(RestifyRequest $request) return (new static ($model)) ->response() ->setStatusCode(RestResponse::REST_RESPONSE_CREATED_CODE) - ->header('Location', Restify::path().'/'.static::uriKey().'/'.$model->id); + ->header('Location', Restify::path() . '/' . static::uriKey() . '/' . $model->id); } /** diff --git a/src/Repositories/Repository.php b/src/Repositories/Repository.php index b239cc0db..1ce946efe 100644 --- a/src/Repositories/Repository.php +++ b/src/Repositories/Repository.php @@ -41,10 +41,9 @@ abstract class Repository extends RepositoryCollection implements RestifySearcha * * @param \Illuminate\Database\Eloquent\Model $model */ - public function __construct($model) + public function __construct($model = null) { parent::__construct($model); - $this->resource = $model; } /** @@ -126,4 +125,13 @@ public function collectFields(RestifyRequest $request) { return collect($this->fields($request)); } + + /** + * @param $resource + * @return Repository + */ + public function withResource($resource) { + $this->resource = $resource; + return $this; + } } diff --git a/src/Repositories/RepositoryCollection.php b/src/Repositories/RepositoryCollection.php index c0505a4ff..1debf3c42 100644 --- a/src/Repositories/RepositoryCollection.php +++ b/src/Repositories/RepositoryCollection.php @@ -38,7 +38,11 @@ public function toArrayForCollection($request) $iterator->next(); } - $response = $data->mapInto($currentRepository)->toArray($request); + $response = $data->map(function ($value) use ($currentRepository) { + return resolve($currentRepository, [ + 'model' => $value, + ])->withResource($value); + })->toArray($request); return [ 'meta' => $this->when($this->isRenderingPaginated(), $this->meta($paginated)), From 5d4bf2bd039c58c9f52bb59caee0c4cacdf966fb Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Thu, 9 Jan 2020 15:33:34 +0200 Subject: [PATCH 2/2] Apply fixes from StyleCI (#76) --- src/Repositories/Crudable.php | 4 ++-- src/Repositories/Repository.php | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Repositories/Crudable.php b/src/Repositories/Crudable.php index 9cf58522a..14f505f61 100644 --- a/src/Repositories/Crudable.php +++ b/src/Repositories/Crudable.php @@ -23,7 +23,7 @@ trait Crudable public function index(RestifyRequest $request, Paginator $paginated) { return resolve(static::class, [ - 'model' => $paginated + 'model' => $paginated, ])->withResource($paginated)->response(); } @@ -63,7 +63,7 @@ public function store(RestifyRequest $request) return (new static ($model)) ->response() ->setStatusCode(RestResponse::REST_RESPONSE_CREATED_CODE) - ->header('Location', Restify::path() . '/' . static::uriKey() . '/' . $model->id); + ->header('Location', Restify::path().'/'.static::uriKey().'/'.$model->id); } /** diff --git a/src/Repositories/Repository.php b/src/Repositories/Repository.php index 1ce946efe..7c0cd9533 100644 --- a/src/Repositories/Repository.php +++ b/src/Repositories/Repository.php @@ -130,8 +130,10 @@ public function collectFields(RestifyRequest $request) * @param $resource * @return Repository */ - public function withResource($resource) { + public function withResource($resource) + { $this->resource = $resource; + return $this; } }