|
17 | 17 | use Symfony\Component\HttpKernel\KernelEvents;
|
18 | 18 | use Symfony\Component\Routing\RouterInterface;
|
19 | 19 | use Symfony\UX\LiveComponent\Metadata\LiveComponentMetadataFactory;
|
| 20 | +use Symfony\UX\LiveComponent\Util\UrlFactory; |
20 | 21 |
|
21 | 22 | class LiveUrlSubscriber implements EventSubscriberInterface
|
22 | 23 | {
|
23 | 24 | private const URL_HEADER = 'X-Live-Url';
|
24 | 25 |
|
25 | 26 | public function __construct(
|
26 |
| - private readonly RouterInterface $router, |
27 |
| - private readonly LiveComponentMetadataFactory $metadataFactory, |
| 27 | + private LiveComponentMetadataFactory $metadataFactory, |
| 28 | + private UrlFactory $urlFactory |
28 | 29 | ) {
|
29 | 30 | }
|
30 | 31 |
|
31 | 32 | public function onKernelResponse(ResponseEvent $event): void
|
32 | 33 | {
|
33 |
| - if (!$this->isLiveComponentRequest($request = $event->getRequest())) { |
| 34 | + $request = $event->getRequest(); |
| 35 | + if (!$request->attributes->has('_live_component')) { |
34 | 36 | return;
|
35 | 37 | }
|
36 | 38 | if (!$event->isMainRequest()) {
|
37 | 39 | return;
|
38 | 40 | }
|
39 | 41 |
|
40 | 42 | if ($previousLocation = $request->headers->get(self::URL_HEADER)) {
|
41 |
| - $newUrl = $this->computeNewUrl( |
| 43 | + $liveProps = $this->getLivePropsToMap($request); |
| 44 | + $newUrl = $this->urlFactory->createFromPreviousAndProps( |
42 | 45 | $previousLocation,
|
43 |
| - $this->getLivePropsToMap($request) |
| 46 | + $liveProps['path'], |
| 47 | + $liveProps['query'] |
44 | 48 | );
|
45 | 49 | if ($newUrl) {
|
46 | 50 | $event->getResponse()->headers->set(
|
@@ -82,44 +86,4 @@ private function getLivePropsToMap(Request $request): array
|
82 | 86 |
|
83 | 87 | return $urlLiveProps;
|
84 | 88 | }
|
85 |
| - |
86 |
| - private function computeNewUrl(string $previousUrl, array $livePropsToMap): string |
87 |
| - { |
88 |
| - $parsed = parse_url($previousUrl); |
89 |
| - |
90 |
| - $url = $parsed['path'] ?? ''; |
91 |
| - if (isset($parsed['query'])) { |
92 |
| - $url .= '?'.$parsed['query']; |
93 |
| - } |
94 |
| - parse_str($parsed['query'] ?? '', $previousQueryParams); |
95 |
| - |
96 |
| - $newUrl = $this->router->generate( |
97 |
| - $this->router->match($url)['_route'], |
98 |
| - array_merge($previousQueryParams, $livePropsToMap['path']) |
99 |
| - ); |
100 |
| - parse_str(parse_url($newUrl)['query'] ?? '', $queryParams); |
101 |
| - $queryString = http_build_query(array_merge($queryParams, $livePropsToMap['query'])); |
102 |
| - |
103 |
| - return preg_replace('/[?#].*/', '', $newUrl). |
104 |
| - ('' !== $queryString ? '?' : ''). |
105 |
| - $queryString; |
106 |
| - } |
107 |
| - |
108 |
| - /** |
109 |
| - * copied from LiveComponentSubscriber. |
110 |
| - */ |
111 |
| - private function isLiveComponentRequest(Request $request): bool |
112 |
| - { |
113 |
| - if (!$request->attributes->has('_live_component')) { |
114 |
| - return false; |
115 |
| - } |
116 |
| - |
117 |
| - // if ($this->testMode) { |
118 |
| - // return true; |
119 |
| - // } |
120 |
| - |
121 |
| - // Except when testing, require the correct content-type in the Accept header. |
122 |
| - // This also acts as a CSRF protection since this can only be set in accordance with same-origin/CORS policies. |
123 |
| - return \in_array('application/vnd.live-component+html', $request->getAcceptableContentTypes(), true); |
124 |
| - } |
125 | 89 | }
|
0 commit comments