Skip to content

Commit b05b320

Browse files
author
Lupacescu Eduard
authored
Dependency injection (#77)
* Allow dependency injection * Apply fixes from StyleCI (#76)
1 parent e27d7f6 commit b05b320

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

src/Http/Requests/InteractWithRepositories.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ public function newRepository()
8484
{
8585
$repository = $this->repository();
8686

87-
return new $repository($repository::newModel());
87+
return resolve($repository, [
88+
'model' => $repository::newModel(),
89+
])->withResource($repository::newModel());
8890
}
8991

9092
/**
@@ -115,7 +117,9 @@ public function newRepositoryWith($model)
115117
{
116118
$repository = $this->repository();
117119

118-
return new $repository($model);
120+
return resolve($repository, [
121+
'model' => $model,
122+
])->withResource($model);
119123
}
120124

121125
/**

src/Repositories/Crudable.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ trait Crudable
2222
*/
2323
public function index(RestifyRequest $request, Paginator $paginated)
2424
{
25-
return (new static($paginated))->response();
25+
return resolve(static::class, [
26+
'model' => $paginated,
27+
])->withResource($paginated)->response();
2628
}
2729

2830
/**

src/Repositories/Repository.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ abstract class Repository extends RepositoryCollection implements RestifySearcha
4141
*
4242
* @param \Illuminate\Database\Eloquent\Model $model
4343
*/
44-
public function __construct($model)
44+
public function __construct($model = null)
4545
{
4646
parent::__construct($model);
47-
$this->resource = $model;
4847
}
4948

5049
/**
@@ -126,4 +125,15 @@ public function collectFields(RestifyRequest $request)
126125
{
127126
return collect($this->fields($request));
128127
}
128+
129+
/**
130+
* @param $resource
131+
* @return Repository
132+
*/
133+
public function withResource($resource)
134+
{
135+
$this->resource = $resource;
136+
137+
return $this;
138+
}
129139
}

src/Repositories/RepositoryCollection.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ public function toArrayForCollection($request)
3838
$iterator->next();
3939
}
4040

41-
$response = $data->mapInto($currentRepository)->toArray($request);
41+
$response = $data->map(function ($value) use ($currentRepository) {
42+
return resolve($currentRepository, [
43+
'model' => $value,
44+
])->withResource($value);
45+
})->toArray($request);
4246

4347
return [
4448
'meta' => $this->when($this->isRenderingPaginated(), $this->meta($paginated)),

0 commit comments

Comments
 (0)