1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\DataProvider; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Api\IriConverterInterface; |
17
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
18
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
19
|
|
|
use Doctrine\Common\Proxy\Proxy; |
20
|
|
|
use Doctrine\ORM\EntityManager; |
21
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
22
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\AbstractPageData; |
23
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\ComponentInterface; |
24
|
|
|
use Silverback\ApiComponentsBundle\Metadata\PageDataComponentMetadata; |
25
|
|
|
use Silverback\ApiComponentsBundle\Metadata\PageDataPropertyMetadata; |
26
|
|
|
use Silverback\ApiComponentsBundle\Metadata\Provider\PageDataMetadataProvider; |
27
|
|
|
use Silverback\ApiComponentsBundle\Repository\Core\RouteRepository; |
28
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
29
|
|
|
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @author Daniel West <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
class PageDataProvider |
35
|
|
|
{ |
36
|
|
|
private RequestStack $requestStack; |
37
|
|
|
private RouteRepository $routeRepository; |
38
|
|
|
private IriConverterInterface $iriConverter; |
39
|
|
|
private ResourceMetadataFactoryInterface $resourceMetadataFactory; |
40
|
|
|
private PageDataMetadataProvider $pageDataMetadataProvider; |
41
|
|
|
private ManagerRegistry $managerRegistry; |
42
|
|
|
|
43
|
|
|
public function __construct( |
44
|
|
|
RequestStack $requestStack, |
45
|
|
|
RouteRepository $routeRepository, |
46
|
|
|
IriConverterInterface $iriConverter, |
47
|
|
|
ResourceMetadataFactoryInterface $resourceMetadataFactory, |
48
|
|
|
PageDataMetadataProvider $pageDataMetadataProvider, |
49
|
|
|
ManagerRegistry $managerRegistry |
50
|
|
|
) { |
51
|
|
|
$this->requestStack = $requestStack; |
52
|
|
|
$this->routeRepository = $routeRepository; |
53
|
|
|
$this->iriConverter = $iriConverter; |
54
|
|
|
$this->resourceMetadataFactory = $resourceMetadataFactory; |
55
|
|
|
$this->pageDataMetadataProvider = $pageDataMetadataProvider; |
56
|
|
|
$this->managerRegistry = $managerRegistry; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function getOriginalRequestPath(): ?string |
60
|
|
|
{ |
61
|
|
|
$request = $this->requestStack->getCurrentRequest(); |
62
|
|
|
if (!$request) { |
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
$path = $request->headers->get('path'); |
66
|
|
|
if (!$path) { |
67
|
|
|
throw new UnprocessableEntityHttpException('Could not find path header to retrieve page data'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return parse_url($path, \PHP_URL_PATH); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getPageData(): ?AbstractPageData |
74
|
|
|
{ |
75
|
|
|
$path = $this->getOriginalRequestPath(); |
76
|
|
|
if (!$path) { |
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$route = $this->routeRepository->findOneByIdOrPath($path); |
81
|
|
|
if (!$route) { |
82
|
|
|
$object = $this->iriConverter->getItemFromIri($path); |
83
|
|
|
if ($object instanceof AbstractPageData) { |
84
|
|
|
return $object; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $route->getPageData(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function findPageDataComponentMetadata(ComponentInterface $component): iterable |
94
|
|
|
{ |
95
|
|
|
$resourceShortName = $this->getComponentShortName($component); |
96
|
|
|
if (!$resourceShortName) { |
97
|
|
|
return; |
98
|
|
|
} |
99
|
|
|
$pageDataLocations = $this->getPageDataLocations($resourceShortName); |
100
|
|
|
foreach ($pageDataLocations as $pageDataClassName => $properties) { |
101
|
|
|
if ($metadata = $this->findPageDataResourcesByPropertiesAndComponent($pageDataClassName, $properties, $component)) { |
102
|
|
|
yield $metadata; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function findPageDataResourcesByPages(iterable $pages): array |
108
|
|
|
{ |
109
|
|
|
$em = $this->managerRegistry->getManagerForClass(AbstractPageData::class); |
110
|
|
|
if (!$em instanceof EntityManager) { |
111
|
|
|
return []; |
112
|
|
|
} |
113
|
|
|
$qb = $em->createQueryBuilder(); |
114
|
|
|
$expr = $qb->expr(); |
115
|
|
|
$qb |
116
|
|
|
->select('pd') |
117
|
|
|
->from(AbstractPageData::class, 'pd'); |
118
|
|
|
foreach ($pages as $x => $page) { |
119
|
|
|
$paramName = 'page_' . $x; |
120
|
|
|
$qb->setParameter($paramName, $page); |
121
|
|
|
$qb->orWhere($expr->eq('pd.page', ":$paramName")); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $qb->getQuery()->getResult() ?: []; |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
private function findPageDataResourcesByPropertiesAndComponent(string $pageDataClassName, ArrayCollection $properties, ComponentInterface $component): ?PageDataComponentMetadata |
128
|
|
|
{ |
129
|
|
|
$em = $this->managerRegistry->getManagerForClass($pageDataClassName); |
130
|
|
|
if (!$em instanceof EntityManager) { |
131
|
|
|
return null; |
132
|
|
|
} |
133
|
|
|
$qb = $em->createQueryBuilder(); |
134
|
|
|
$expr = $qb->expr(); |
135
|
|
|
$qb |
136
|
|
|
->select('pd') |
137
|
|
|
->from($pageDataClassName, 'pd') |
138
|
|
|
->setParameter('component', $component); |
139
|
|
|
foreach ($properties as $property) { |
140
|
|
|
$qb->orWhere($expr->eq('pd.' . $property, ':component')); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return new PageDataComponentMetadata($qb->getQuery()->getResult() ?: [], $properties); |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
private function getPageDataLocations(string $resourceShortName): array |
147
|
|
|
{ |
148
|
|
|
$pageDataMetadatas = $this->pageDataMetadataProvider->createAll(); |
149
|
|
|
$pageDataLocations = []; |
150
|
|
|
foreach ($pageDataMetadatas as $pageDataMetadata) { |
151
|
|
|
$resourceProperties = $pageDataMetadata->findPropertiesByComponentShortName($resourceShortName); |
152
|
|
|
if ($resourceProperties->count() > 0) { |
153
|
|
|
$pageDataLocations[$pageDataMetadata->getResourceClass()] = $resourceProperties->map(static function (PageDataPropertyMetadata $metadata) { |
154
|
|
|
return $metadata->getProperty(); |
155
|
|
|
}); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $pageDataLocations; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
private function getComponentShortName(ComponentInterface $component): ?string |
163
|
|
|
{ |
164
|
|
|
$resourceClass = \get_class($component); |
165
|
|
|
if ($component instanceof Proxy) { |
166
|
|
|
$em = $this->managerRegistry->getManagerForClass($resourceClass); |
167
|
|
|
if (!$em) { |
168
|
|
|
return null; |
169
|
|
|
} |
170
|
|
|
$resourceClass = $em->getClassMetadata($resourceClass)->getName(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $this->resourceMetadataFactory->create($resourceClass)->getShortName(); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|