Skip to content

Commit 34b3b70

Browse files
authored
Merge pull request #104 from tailflow/next
v2.1 Release
2 parents c1d36ce + acab3c2 commit 34b3b70

File tree

9 files changed

+133
-35
lines changed

9 files changed

+133
-35
lines changed

src/Concerns/HandlesRelationStandardOperations.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,7 @@ protected function performStore(
352352
array $attributes,
353353
array $pivot
354354
): void {
355-
$entity->fill(
356-
Arr::except($attributes, array_keys($entity->getDirty()))
357-
);
355+
$this->performFill($request, $parentEntity, $entity, $attributes, $pivot);
358356

359357
if (!$parentEntity->{$this->getRelation()}() instanceof BelongsTo) {
360358
$parentEntity->{$this->getRelation()}()->save($entity, $this->preparePivotFields($pivot));
@@ -696,9 +694,7 @@ protected function performUpdate(
696694
array $attributes,
697695
array $pivot
698696
): void {
699-
$entity->fill(
700-
Arr::except($attributes, array_keys($entity->getDirty()))
701-
);
697+
$this->performFill($request, $parentEntity, $entity, $attributes, $pivot);
702698
$entity->save();
703699

704700
$relation = $parentEntity->{$this->getRelation()}();
@@ -1028,4 +1024,25 @@ protected function afterRestore(Request $request, Model $parentEntity, Model $en
10281024
{
10291025
return null;
10301026
}
1027+
1028+
/**
1029+
* Fills attributes on the given relation entity.
1030+
*
1031+
* @param Request $request
1032+
* @param Model $parentEntity
1033+
* @param Model $entity
1034+
* @param array $attributes
1035+
* @param array $pivot
1036+
*/
1037+
protected function performFill(
1038+
Request $request,
1039+
Model $parentEntity,
1040+
Model $entity,
1041+
array $attributes,
1042+
array $pivot
1043+
): void {
1044+
$entity->fill(
1045+
Arr::except($attributes, array_keys($entity->getDirty()))
1046+
);
1047+
}
10311048
}

src/Concerns/HandlesStandardOperations.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,7 @@ protected function beforeSave(Request $request, Model $entity)
207207
*/
208208
protected function performStore(Request $request, Model $entity, array $attributes): void
209209
{
210-
$entity->fill(
211-
Arr::except($attributes, array_keys($entity->getDirty()))
212-
);
210+
$this->performFill($request, $entity, $attributes);
213211
$entity->save();
214212
}
215213

@@ -426,9 +424,7 @@ protected function beforeUpdate(Request $request, Model $entity)
426424
*/
427425
protected function performUpdate(Request $request, Model $entity, array $attributes): void
428426
{
429-
$entity->fill(
430-
Arr::except($attributes, array_keys($entity->getDirty()))
431-
);
427+
$this->performFill($request, $entity, $attributes);
432428
$entity->save();
433429
}
434430

@@ -664,4 +660,18 @@ protected function afterRestore(Request $request, Model $entity)
664660
{
665661
return null;
666662
}
663+
664+
/**
665+
* Fills attributes on the given entity.
666+
*
667+
* @param Request $request
668+
* @param Model $entity
669+
* @param array $attributes
670+
*/
671+
protected function performFill(Request $request, Model $entity, array $attributes): void
672+
{
673+
$entity->fill(
674+
Arr::except($attributes, array_keys($entity->getDirty()))
675+
);
676+
}
667677
}

src/Contracts/RelationsResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function relationForeignKeyFromRelationInstance(Relation $relationInstanc
2323

2424
public function relationLocalKeyFromRelationInstance(Relation $relationInstance): string;
2525

26-
public function guardRelationsForCollection(Collection $entities, array $requestedRelations, bool $normalized = false): Collection;
26+
public function guardRelationsForCollection(Collection $entities, array $requestedRelations, ?string $parentRelation = null, bool $normalized = false): Collection;
2727

28-
public function guardRelations(Model $entity, array $requestedRelations, bool $normalized = false);
28+
public function guardRelations(Model $entity, array $requestedRelations, ?string $parentRelation = null, bool $normalized = false);
2929
}

src/Drivers/Standard/RelationsResolver.php

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ public function relationForeignKeyFromRelationInstance(Relation $relationInstanc
115115
{
116116
$laravelVersion = (float)app()->version();
117117

118-
return $laravelVersion > 5.7 || get_class($relationInstance) === HasOne::class ? $relationInstance->getQualifiedForeignKeyName() : $relationInstance->getQualifiedForeignKey();
118+
return $laravelVersion > 5.7 || get_class(
119+
$relationInstance
120+
) === HasOne::class ? $relationInstance->getQualifiedForeignKeyName(
121+
) : $relationInstance->getQualifiedForeignKey();
119122
}
120123

121124
/**
@@ -129,15 +132,12 @@ public function relationLocalKeyFromRelationInstance(Relation $relationInstance)
129132
switch (get_class($relationInstance)) {
130133
case HasOne::class:
131134
case MorphOne::class:
132-
return $relationInstance->getParent()->getTable().'.'.$relationInstance->getLocalKeyName();
133-
break;
135+
return $relationInstance->getParent()->getTable() . '.' . $relationInstance->getLocalKeyName();
134136
case BelongsTo::class:
135137
case MorphTo::class:
136138
return $relationInstance->getQualifiedOwnerKeyName();
137-
break;
138139
default:
139140
return $relationInstance->getQualifiedLocalKeyName();
140-
break;
141141
}
142142
}
143143

@@ -146,14 +146,19 @@ public function relationLocalKeyFromRelationInstance(Relation $relationInstance)
146146
*
147147
* @param Collection $entities
148148
* @param array $requestedRelations
149+
* @param string|null $parentRelation
149150
* @param bool $normalized
150151
* @return Collection
151152
*/
152-
public function guardRelationsForCollection(Collection $entities, array $requestedRelations, bool $normalized = false): Collection
153-
{
153+
public function guardRelationsForCollection(
154+
Collection $entities,
155+
array $requestedRelations,
156+
?string $parentRelation = null,
157+
bool $normalized = false
158+
): Collection {
154159
return $entities->transform(
155-
function ($entity) use ($requestedRelations, $normalized) {
156-
return $this->guardRelations($entity, $requestedRelations, $normalized);
160+
function ($entity) use ($requestedRelations, $parentRelation, $normalized) {
161+
return $this->guardRelations($entity, $requestedRelations, $parentRelation, $normalized);
157162
}
158163
);
159164
}
@@ -163,11 +168,16 @@ function ($entity) use ($requestedRelations, $normalized) {
163168
*
164169
* @param Model $entity
165170
* @param array $requestedRelations
171+
* @param string|null $parentRelation
166172
* @param bool $normalized
167173
* @return Model
168174
*/
169-
public function guardRelations(Model $entity, array $requestedRelations, bool $normalized = false): Model
170-
{
175+
public function guardRelations(
176+
Model $entity,
177+
array $requestedRelations,
178+
?string $parentRelation = null,
179+
bool $normalized = false
180+
): Model {
171181
if (!$normalized) {
172182
$requestedRelations = $this->normalizeRequestedRelations($requestedRelations);
173183
}
@@ -176,17 +186,27 @@ public function guardRelations(Model $entity, array $requestedRelations, bool $n
176186
ksort($relations);
177187

178188
foreach ($relations as $relationName => $relation) {
179-
if ($relationName === 'pivot') {
189+
if ($relationName === 'pivot' || $relationName === $parentRelation) {
180190
continue;
181191
}
182192

183193
if (!array_key_exists($relationName, $requestedRelations)) {
184194
unset($relations[$relationName]);
185195
} elseif ($relation !== null) {
186196
if ($relation instanceof Model) {
187-
$relation = $this->guardRelations($relation, $requestedRelations[$relationName], true);
197+
$relation = $this->guardRelations(
198+
$relation,
199+
$requestedRelations[$relationName],
200+
$relationName,
201+
true
202+
);
188203
} else {
189-
$relation = $this->guardRelationsForCollection($relation, $requestedRelations[$relationName], true);
204+
$relation = $this->guardRelationsForCollection(
205+
$relation,
206+
$requestedRelations[$relationName],
207+
$relationName,
208+
true
209+
);
190210
}
191211
}
192212
}

src/Http/Middleware/EnforceExpectsJson.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Orion\Http\Middleware;
44

55
use Illuminate\Http\Request;
6+
use Illuminate\Support\Str;
67

78
class EnforceExpectsJson
89
{
@@ -13,7 +14,10 @@ class EnforceExpectsJson
1314
*/
1415
public function handle(Request $request, $next)
1516
{
16-
$request->headers->add(['Accept' => 'application/json']);
17+
if (!Str::contains($request->header('Accept'), 'application/json')) {
18+
$request->headers->set('Accept', 'application/json, ' . $request->header('Accept'));
19+
}
20+
1721
return $next($request);
1822
}
1923
}

src/Specs/Builders/Components/Model/ModelResourceComponentBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function build(Model $resourceModel): ModelResourceComponent
2626
$component->type = 'object';
2727
$component->properties = [
2828
'allOf' => [
29-
['$ref' => "#/components/schemas/{$resourceComponentBaseName}Resource"],
29+
['$ref' => "#/components/schemas/{$resourceComponentBaseName}"],
3030
[
3131
'type' => 'object',
3232
'properties' => $this->getPropertiesFromSchema($resourceModel)

src/ValueObjects/RegisteredResource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function getKeyType(): string
5050
*/
5151
protected function qualifyControllerClass(string $controller): string
5252
{
53-
if (Str::startsWith($controller, config('orion.namespaces.controllers'))) {
53+
if (class_exists($controller) || Str::startsWith($controller, config('orion.namespaces.controllers'))) {
5454
return $controller;
5555
}
5656

tests/Unit/Drivers/Standard/RelationsResolverTest.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Orion\Drivers\Standard\RelationsResolver;
66
use Orion\Http\Requests\Request;
7+
use Orion\Tests\Fixtures\App\Models\Category;
78
use Orion\Tests\Fixtures\App\Models\Company;
89
use Orion\Tests\Fixtures\App\Models\Post;
910
use Orion\Tests\Fixtures\App\Models\Team;
@@ -85,6 +86,35 @@ public function guarding_entity_nested_relations()
8586
{
8687
$post = new Post(['title' => 'test post']);
8788

89+
$parentCategory = new Category(['name' => 'parent category']); // categories
90+
$childCategory = new Category(['name' => 'child category']); // categories.categories
91+
$nestedChildCategory = new Category(['name' => 'nested child category']); // categories.categories.categories
92+
93+
$childCategory->setRelation('categories', collect([$nestedChildCategory]));
94+
$parentCategory->setRelation('categories', collect([$childCategory]));
95+
96+
$post->setRelations(
97+
[
98+
'categories' => collect([$parentCategory]),
99+
]
100+
);
101+
102+
$relationsResolver = new RelationsResolver(['categories'], []);
103+
$guardedPost = $relationsResolver->guardRelations($post, ['categories']);
104+
105+
self::assertArrayHasKey('categories', $guardedPost->getRelations());
106+
self::assertArrayHasKey('categories', $guardedPost->getRelation('categories')->first()->getRelations());
107+
self::assertArrayHasKey(
108+
'categories',
109+
$guardedPost->getRelation('categories')->first()->getRelation('categories')->first()->getRelations()
110+
);
111+
}
112+
113+
/** @test */
114+
public function guarding_entity_recursive_nested_relations()
115+
{
116+
$post = new Post(['title' => 'test post']);
117+
88118
$manager = new User(['name' => 'manager user']);
89119

90120
$team = new Team(['name' => 'test team']);
@@ -102,7 +132,10 @@ public function guarding_entity_nested_relations()
102132
);
103133

104134
$relationsResolver = new RelationsResolver(['user', 'editors.team.users', 'editors.team.company'], []);
105-
$guardedPost = $relationsResolver->guardRelations($post, ['user', 'editors.team.users', 'editors.team.company']);
135+
$guardedPost = $relationsResolver->guardRelations(
136+
$post,
137+
['user', 'editors.team.users', 'editors.team.company']
138+
);
106139

107140
self::assertArrayHasKey('user', $guardedPost->getRelations());
108141
self::assertArrayHasKey('editors', $guardedPost->getRelations());
@@ -159,4 +192,4 @@ public function resolving_deep_relation_from_param_constraint(): void
159192

160193
self::assertSame('user.posts', $relation);
161194
}
162-
}
195+
}

tests/Unit/Http/Middleware/EnforceExpectsJsonTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,28 @@
99
class EnforceExpectsJsonTest extends TestCase
1010
{
1111
/** @test */
12-
public function adding_accept_header()
12+
public function adding_application_json_to_accept_header(): void
1313
{
1414
$request = Request::create('/api/posts');
1515

1616
(new EnforceExpectsJson())->handle(
1717
$request,
1818
function ($processedRequest) {
19-
$this->assertEquals('application/json', $processedRequest->header('Accept'));
19+
$this->assertTrue($processedRequest->expectsJson());
20+
}
21+
);
22+
}
23+
24+
/** @test */
25+
public function preserving_existing_accept_header_content_types(): void
26+
{
27+
$request = Request::create('/api/posts');
28+
$request->headers->set('Accept', 'application/xml');
29+
30+
(new EnforceExpectsJson())->handle(
31+
$request,
32+
function ($processedRequest) {
33+
$this->assertSame('application/json, application/xml', $processedRequest->header('Accept'));
2034
}
2135
);
2236
}

0 commit comments

Comments
 (0)