|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\Serializer; |
|
4
|
|
|
|
|
5
|
|
|
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface; |
|
6
|
|
|
use Silverback\ApiComponentBundle\Entity\Component\AbstractComponent; |
|
7
|
|
|
use Silverback\ApiComponentBundle\Entity\Content\AbstractContent; |
|
8
|
|
|
use Silverback\ApiComponentBundle\Entity\Content\ComponentLocation; |
|
9
|
|
|
use Silverback\ApiComponentBundle\Entity\Layout\Layout; |
|
10
|
|
|
use Silverback\ApiComponentBundle\Entity\Navigation\AbstractNavigation; |
|
11
|
|
|
use Silverback\ApiComponentBundle\Entity\Navigation\AbstractNavigationItem; |
|
12
|
|
|
use Silverback\ApiComponentBundle\Entity\Navigation\Route\Route; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
14
|
|
|
|
|
15
|
|
|
class ApiContextBuilder implements SerializerContextBuilderInterface |
|
16
|
|
|
{ |
|
17
|
|
|
private $decorated; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(SerializerContextBuilderInterface $decorated) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->decorated = $decorated; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
private function getGroups(string $group, bool $normalization, ?string $operation) |
|
25
|
|
|
{ |
|
26
|
|
|
$groups = [$group, $group . ($normalization ? '_read' : '_write')]; |
|
27
|
|
|
if ($operation) { |
|
28
|
|
|
$groups[] = "${group}_${operation}"; |
|
29
|
|
|
} |
|
30
|
|
|
return $groups; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
private function matchClass($className, $matchClassName) |
|
34
|
|
|
{ |
|
35
|
|
|
return $className === $matchClassName || is_subclass_of($className, $matchClassName); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param Request $request |
|
40
|
|
|
* @param bool $normalization |
|
41
|
|
|
* @param array|null $extractedAttributes |
|
42
|
|
|
* @return array |
|
43
|
|
|
* @throws \ApiPlatform\Core\Exception\RuntimeException |
|
44
|
|
|
*/ |
|
45
|
|
|
public function createFromRequest(Request $request, bool $normalization, array $extractedAttributes = null) : array |
|
46
|
|
|
{ |
|
47
|
|
|
$context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes); |
|
48
|
|
|
if (isset($context['groups']) && in_array('none', $context['groups'])) { |
|
49
|
|
|
return $context; |
|
50
|
|
|
} |
|
51
|
|
|
$subject = $request->attributes->get('_api_resource_class'); |
|
52
|
|
|
$operation = $context['item_operation_name'] ?? null; |
|
53
|
|
|
$groups = []; |
|
54
|
|
|
if ( |
|
55
|
|
|
$this->matchClass($subject, AbstractComponent::class) || |
|
56
|
|
|
$this->matchClass($subject, AbstractNavigation::class) || |
|
57
|
|
|
$this->matchClass($subject, ComponentLocation::class) |
|
58
|
|
|
) { |
|
59
|
|
|
$groups[] = $this->getGroups('component', $normalization, $operation); |
|
60
|
|
|
} |
|
61
|
|
|
if ( |
|
62
|
|
|
$this->matchClass($subject, AbstractNavigationItem::class) |
|
63
|
|
|
) { |
|
64
|
|
|
$groups[] = $this->getGroups('component_item', $normalization, $operation); |
|
65
|
|
|
} |
|
66
|
|
|
if ( |
|
67
|
|
|
$this->matchClass($subject, AbstractContent::class) |
|
68
|
|
|
) { |
|
69
|
|
|
$groups[] = $this->getGroups('content', $normalization, $operation); |
|
70
|
|
|
} |
|
71
|
|
|
if ( |
|
72
|
|
|
$this->matchClass($subject, Route::class) |
|
73
|
|
|
) { |
|
74
|
|
|
$groups[] = $this->getGroups('route', $normalization, $operation); |
|
75
|
|
|
} |
|
76
|
|
|
if ($this->matchClass($subject, Layout::class)) { |
|
77
|
|
|
$groups[] = $this->getGroups('layout', $normalization, $operation); |
|
78
|
|
|
} |
|
79
|
|
|
if (\count($groups)) { |
|
80
|
|
|
if (!isset($context['groups'])) { |
|
81
|
|
|
$context['groups'] = ['default']; |
|
82
|
|
|
} else { |
|
83
|
|
|
$context['groups'][] = ['default']; |
|
84
|
|
|
} |
|
85
|
|
|
$context['groups'] = array_merge($context['groups'], ...$groups); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
return $context; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|