Skip to content

Commit

Permalink
Define RoutesResponse as a Service
Browse files Browse the repository at this point in the history
  • Loading branch information
pyksid committed Dec 14, 2023
1 parent 80835fe commit aeb9ea5
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 43 deletions.
33 changes: 17 additions & 16 deletions Command/DumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);
Expand Down
31 changes: 17 additions & 14 deletions Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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)) {
Expand Down
1 change: 1 addition & 0 deletions Resources/config/controllers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</parameters>
<services>
<service id="fos_js_routing.controller" class="%fos_js_routing.controller.class%" public="true">
<argument type="service" id="fos_js_routing.routes_response" />
<argument type="service" id="fos_js_routing.serializer" />
<argument type="service" id="fos_js_routing.extractor" />
<argument>%fos_js_routing.cache_control%</argument>
Expand Down
4 changes: 3 additions & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

<parameters>
<parameter key="fos_js_routing.extractor.class">FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractor</parameter>
<parameter key="fos_js_routing.routes_response.class">FOS\JsRoutingBundle\Response\RoutesResponse</parameter>
</parameters>

<services>
Expand All @@ -14,8 +15,9 @@
<argument>%kernel.cache_dir%</argument>
<argument>%kernel.bundles%</argument>
</service>

<service id="fos_js_routing.routes_response" class="%fos_js_routing.routes_response.class%" public="true" />
<service id="fos_js_routing.dump_command" class="FOS\JsRoutingBundle\Command\DumpCommand">
<argument type="service" id="fos_js_routing.routes_response" />
<argument type="service" id="fos_js_routing.extractor" />
<argument type="service" id="fos_js_routing.serializer" />
<argument>%kernel.project_dir%</argument>
Expand Down
75 changes: 63 additions & 12 deletions Response/RoutesResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
}
}

0 comments on commit aeb9ea5

Please # to comment.