From aeb9ea53dd8d3baace02f49144355d21a66771f9 Mon Sep 17 00:00:00 2001 From: Pierre Kerfurus Date: Tue, 12 Dec 2023 09:53:04 +0100 Subject: [PATCH] Define RoutesResponse as a Service --- Command/DumpCommand.php | 33 +++++++------- Controller/Controller.php | 31 +++++++------ Resources/config/controllers.xml | 1 + Resources/config/services.xml | 4 +- Response/RoutesResponse.php | 75 +++++++++++++++++++++++++++----- 5 files changed, 101 insertions(+), 43 deletions(-) diff --git a/Command/DumpCommand.php b/Command/DumpCommand.php index 5f3f2a90..52188514 100644 --- a/Command/DumpCommand.php +++ b/Command/DumpCommand.php @@ -30,8 +30,13 @@ #[AsCommand('fos:js-routing:dump', 'Dumps exposed routes to the filesystem')] class DumpCommand extends Command { - public function __construct(private ExposedRoutesExtractorInterface $extractor, private SerializerInterface $serializer, private string $projectDir, private ?string $requestContextBaseUrl = null) - { + public function __construct( + private RoutesResponse $routesResponse, + private ExposedRoutesExtractorInterface $extractor, + private SerializerInterface $serializer, + private string $projectDir, + private ?string $requestContextBaseUrl = null, + ) { parent::__construct(); } @@ -141,20 +146,16 @@ private function doDump(InputInterface $input, OutputInterface $output): void $params = []; } - $content = $serializer->serialize( - new RoutesResponse( - $baseUrl, - $extractor->getRoutes(), - $extractor->getPrefix($input->getOption('locale')), - $extractor->getHost(), - $extractor->getPort(), - $extractor->getScheme(), - $input->getOption('locale'), - $domain - ), - 'json', - $params - ); + $this->routesResponse->setBaseUrl($baseUrl); + $this->routesResponse->setRoutes($extractor->getRoutes()); + $this->routesResponse->setPrefix($extractor->getPrefix($input->getOption('locale'))); + $this->routesResponse->setHost($extractor->getHost()); + $this->routesResponse->setPort($extractor->getPort()); + $this->routesResponse->setScheme($extractor->getScheme()); + $this->routesResponse->setLocale($input->getOption('locale')); + $this->routesResponse->setDomains($domain); + + $content = $serializer->serialize($this->routesResponse, 'json', $params); if ('js' == $input->getOption('format')) { $content = sprintf('%s(%s);', $input->getOption('callback'), $content); diff --git a/Controller/Controller.php b/Controller/Controller.php index b3203471..8480eb59 100644 --- a/Controller/Controller.php +++ b/Controller/Controller.php @@ -38,8 +38,13 @@ class Controller * @param ExposedRoutesExtractorInterface $exposedRoutesExtractor the extractor service * @param bool $debug */ - public function __construct(private mixed $serializer, private ExposedRoutesExtractorInterface $exposedRoutesExtractor, array $cacheControl = [], private bool $debug = false) - { + public function __construct( + private RoutesResponse $routesResponse, + private mixed $serializer, + private ExposedRoutesExtractorInterface $exposedRoutesExtractor, + array $cacheControl = [], + private bool $debug = false, + ) { $this->cacheControlConfig = new CacheControlConfig($cacheControl); } @@ -68,18 +73,16 @@ public function indexAction(Request $request, $_format): Response ); } - $routesResponse = new RoutesResponse( - $this->exposedRoutesExtractor->getBaseUrl(), - $exposedRoutes, - $this->exposedRoutesExtractor->getPrefix($request->getLocale()), - $this->exposedRoutesExtractor->getHost(), - $this->exposedRoutesExtractor->getPort(), - $this->exposedRoutesExtractor->getScheme(), - $request->getLocale(), - $request->query->has('domain') ? explode(',', $request->query->get('domain')) : [] - ); - - $content = $this->serializer->serialize($routesResponse, 'json'); + $this->routesResponse->setBaseUrl($this->exposedRoutesExtractor->getBaseUrl()); + $this->routesResponse->setRoutes($exposedRoutes); + $this->routesResponse->setPrefix($this->exposedRoutesExtractor->getPrefix($request->getLocale())); + $this->routesResponse->setHost($this->exposedRoutesExtractor->getHost()); + $this->routesResponse->setPort($this->exposedRoutesExtractor->getPort()); + $this->routesResponse->setScheme($this->exposedRoutesExtractor->getScheme()); + $this->routesResponse->setLocale($request->getLocale()); + $this->routesResponse->setDomains($request->query->has('domain') ? explode(',', $request->query->get('domain')) : []); + + $content = $this->serializer->serialize($this->routesResponse, 'json'); if (null !== $callback = $request->query->get('callback')) { if (!\JsonpCallbackValidator::validate($callback)) { diff --git a/Resources/config/controllers.xml b/Resources/config/controllers.xml index ecc7e546..5413804a 100644 --- a/Resources/config/controllers.xml +++ b/Resources/config/controllers.xml @@ -7,6 +7,7 @@ + %fos_js_routing.cache_control% diff --git a/Resources/config/services.xml b/Resources/config/services.xml index efdd6715..cd71b95c 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -5,6 +5,7 @@ FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractor + FOS\JsRoutingBundle\Response\RoutesResponse @@ -14,8 +15,9 @@ %kernel.cache_dir% %kernel.bundles% - + + %kernel.project_dir% diff --git a/Response/RoutesResponse.php b/Response/RoutesResponse.php index e4b9aa97..9d18aacc 100644 --- a/Response/RoutesResponse.php +++ b/Response/RoutesResponse.php @@ -17,24 +17,25 @@ class RoutesResponse { - private $routes; - - public function __construct(private string $baseUrl, RouteCollection $routes = null, - private ?string $prefix = null, private ?string $host = null, - private ?string $port = null, private ?string $scheme = null, - private ?string $locale = null, private array $domains = []) - { + protected $routes; + + public function __construct( + protected ?string $baseUrl = null, + RouteCollection $routes = null, + protected ?string $prefix = null, + protected ?string $host = null, + protected ?string $port = null, + protected ?string $scheme = null, + protected ?string $locale = null, + protected array $domains = [], + ) { $this->routes = $routes ?: new RouteCollection(); } - public function getBaseUrl(): string - { - return $this->baseUrl; - } - public function getRoutes(): array { $exposedRoutes = []; + foreach ($this->routes->all() as $name => $route) { if (!$route->hasOption('expose')) { $domain = 'default'; @@ -74,28 +75,78 @@ public function getRoutes(): array return $exposedRoutes; } + public function setRoutes(RouteCollection $routes): void + { + $this->routes = $routes; + } + + public function getBaseUrl(): string + { + return $this->baseUrl; + } + + public function setBaseUrl(string $baseUrl): void + { + $this->baseUrl = $baseUrl; + } + public function getPrefix(): ?string { return $this->prefix; } + public function setPrefix(?string $prefix): void + { + $this->prefix = $prefix; + } + public function getHost(): ?string { return $this->host; } + public function setHost(?string $host): void + { + $this->host = $host; + } + public function getPort(): ?string { return $this->port; } + public function setPort(?string $port): void + { + $this->port = $port; + } + public function getScheme(): ?string { return $this->scheme; } + public function setScheme(?string $scheme): void + { + $this->scheme = $scheme; + } + public function getLocale(): ?string { return $this->locale; } + + public function setLocale(?string $locale): void + { + $this->locale = $locale; + } + + public function getDomains(): array + { + return $this->domains; + } + + public function setDomains(array $domains): void + { + $this->domains = $domains; + } }