|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[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 | +namespace Symfony\UX\LiveComponent\EventListener; |
| 13 | + |
| 14 | +use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 15 | +use Symfony\Component\HttpFoundation\Request; |
| 16 | +use Symfony\Component\HttpKernel\Event\ResponseEvent; |
| 17 | +use Symfony\Component\HttpKernel\KernelEvents; |
| 18 | +use Symfony\Component\Routing\RouterInterface; |
| 19 | +use Symfony\UX\LiveComponent\Metadata\LiveComponentMetadataFactory; |
| 20 | + |
| 21 | +class LiveUrlSubscriber implements EventSubscriberInterface |
| 22 | +{ |
| 23 | + private const string URL_HEADER = 'X-Live-Url'; |
| 24 | + |
| 25 | + public function __construct( |
| 26 | + private readonly RouterInterface $router, |
| 27 | + private readonly LiveComponentMetadataFactory $metadataFactory, |
| 28 | + ) { |
| 29 | + } |
| 30 | + |
| 31 | + public function onKernelResponse(ResponseEvent $event): void |
| 32 | + { |
| 33 | + if (!$this->isLiveComponentRequest($request = $event->getRequest())) { |
| 34 | + return; |
| 35 | + } |
| 36 | + if (!$event->isMainRequest()) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + if ($previousLocation = $request->headers->get(self::URL_HEADER)) { |
| 41 | + $newUrl = $this->computeNewUrl( |
| 42 | + $previousLocation, |
| 43 | + $this->getUrlLiveProps($request) |
| 44 | + ); |
| 45 | + if ($newUrl) { |
| 46 | + $event->getResponse()->headers->set( |
| 47 | + self::URL_HEADER, |
| 48 | + $this->computeNewUrl( |
| 49 | + $previousLocation, |
| 50 | + $this->getUrlLiveProps($request) |
| 51 | + ) |
| 52 | + ); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public static function getSubscribedEvents(): array |
| 58 | + { |
| 59 | + return [ |
| 60 | + KernelEvents::RESPONSE => 'onKernelResponse', |
| 61 | + ]; |
| 62 | + } |
| 63 | + |
| 64 | + private function getUrlLiveProps(Request $request): array |
| 65 | + { |
| 66 | + $componentName = $request->attributes->get('_live_component'); |
| 67 | + $component = $request->attributes->get('_mounted_component'); |
| 68 | + $metadata = $this->metadataFactory->getMetadata($componentName); |
| 69 | + |
| 70 | + $liveData = $request->attributes->get('_live_request_data') ?? []; |
| 71 | + $values = array_merge($liveData['props'] ?? [], $liveData['updated'] ?? []); |
| 72 | + |
| 73 | + $urlLiveProps = []; |
| 74 | + foreach ($metadata->getAllLivePropsMetadata($component) as $liveProp) { |
| 75 | + $name = $liveProp->getName(); |
| 76 | + $urlMapping = $liveProp->urlMapping(); |
| 77 | + if (isset($values[$name]) && $urlMapping) { |
| 78 | + $urlLiveProps[$urlMapping->as ?? $name] = $values[$name]; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return $urlLiveProps; |
| 83 | + } |
| 84 | + |
| 85 | + // @todo use requestStack ? |
| 86 | + private function computeNewUrl(string $previousUrl, array $newProps): string |
| 87 | + { |
| 88 | + $parsed = parse_url($previousUrl); |
| 89 | + $baseUrl = $parsed['scheme'].'://'; |
| 90 | + if (isset($parsed['user'])) { |
| 91 | + $baseUrl .= $parsed['user']; |
| 92 | + if (isset($parsed['pass'])) { |
| 93 | + $baseUrl .= ':'.$parsed['pass']; |
| 94 | + } |
| 95 | + $baseUrl .= '@'; |
| 96 | + } |
| 97 | + $baseUrl .= $parsed['host']; |
| 98 | + if (isset($parsed['port'])) { |
| 99 | + $baseUrl .= ':'.$parsed['port']; |
| 100 | + } |
| 101 | + |
| 102 | + $path = $parsed['path'] ?? ''; |
| 103 | + if (isset($parsed['query'])) { |
| 104 | + $path .= '?'.$parsed['query']; |
| 105 | + } |
| 106 | + $match = $this->router->match($path); |
| 107 | + $newUrl = $this->router->generate( |
| 108 | + $match['_route'], |
| 109 | + $newProps |
| 110 | + ); |
| 111 | + |
| 112 | + $fragment = $parsed['fragment'] ?? ''; |
| 113 | + |
| 114 | + return $baseUrl.$newUrl.$fragment; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * copied from LiveComponentSubscriber. |
| 119 | + */ |
| 120 | + private function isLiveComponentRequest(Request $request): bool |
| 121 | + { |
| 122 | + if (!$request->attributes->has('_live_component')) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + // if ($this->testMode) { |
| 127 | + // return true; |
| 128 | + // } |
| 129 | + |
| 130 | + // Except when testing, require the correct content-type in the Accept header. |
| 131 | + // This also acts as a CSRF protection since this can only be set in accordance with same-origin/CORS policies. |
| 132 | + return \in_array('application/vnd.live-component+html', $request->getAcceptableContentTypes(), true); |
| 133 | + } |
| 134 | +} |
0 commit comments