8000 Define RoutesResponse as a Service by pyksid · Pull Request #474 · FriendsOfSymfony/FOSJsRoutingBundle · GitHub
[go: up one dir, main page]

Skip to content

Define RoutesResponse as a Service #474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
42 changes: 37 additions & 5 deletions Tests/Command/DumpCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace FOS\JsRoutingBundle\Tests\Command;

use FOS\JsRoutingBundle\Command\DumpCommand;
use FOS\JsRoutingBundle\Response\RoutesResponse;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Serializer\SerializerInterface;
Expand All @@ -22,12 +23,17 @@

class DumpCommandTest extends TestCase
{
protected RoutesResponse $routesResponse;
protected $extractor;
protected $router;
private $serializer;

public function setUp(): void
{
$this->routesResponse = $this->getMockBuilder(RoutesResponse::class)
->disableOriginalConstructor()
->getMock();

$this->extractor = $this->getMockBuilder(ExposedRoutesExtractor::class)
->disableOriginalConstructor()
->getMock();
Expand All @@ -47,7 +53,12 @@ public function testExecute(): void
->method('serialize')
->will($this->returnValue('{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[]},"blog":{"tokens":[["variable","\/","[^\/]++","slug"],["text","\/blog-post"]],"defaults":[],"requirements":[],"hosttokens":[["text","localhost"]]}},"prefix":"","host":"","scheme":""}'));

$command = new DumpCommand($this->extractor, $this->serializer, '/root/dir');
$command = new DumpCommand(
$this->routesResponse,
$this->extractor,
$this->serializer,
'/root/dir',
);

$tester = new CommandTester($command);
$tester->execute(['--target' => '/tmp/dump-command-test']);
Expand All @@ -64,7 +75,12 @@ public function testExecuteCallbackOption(): void
->method('serialize')
->will($this->returnValue('{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[]},"blog":{"tokens":[["variable","\/","[^\/]++","slug"],["text","\/blog-post"]],"defaults":[],"requirements":[],"hosttokens":[["text","localhost"]]}},"prefix":"","host":"","scheme":""}'));

$command = new DumpCommand($this->extractor, $this->serializer, '/root/dir');
$command = new DumpCommand(
$this->routesResponse,
$this->extractor,
$this->serializer,
'/root/dir',
);

$tester = new CommandTester($command);
$tester->execute([
Expand All @@ -86,7 +102,12 @@ public function testExecuteFormatOption(): void
->method('serialize')
->will($this->returnValue($json));

$command = new DumpCommand($this->extractor, $this->serializer, '/root/dir');
$command = new DumpCommand(
$this->routesResponse,
$this->extractor,
$this->serializer,
'/root/dir',
);

$tester = new CommandTester($command);
$tester->execute([
Expand All @@ -104,7 +125,13 @@ public function testExecuteUnableToCreateDirectory(): void
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Unable to create directory /root/dir/public/js');
$command = new DumpCommand($this->extractor, $this->serializer, '/root/dir');

$command = new DumpCommand(
$this->routesResponse,
$this->extractor,
$this->serializer,
'/root/dir',
);

$tester = new CommandTester($command);
$tester->execute([]);
Expand All @@ -118,7 +145,12 @@ public function testExecuteUnableToWriteFile(): void
->method('serialize')
->will($this->returnValue('{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[]},"blog":{"tokens":[["variable","\/","[^\/]++","slug"],["text","\/blog-post"]],"defaults":[],"requirements":[],"hosttokens":[["text","localhost"]]}},"prefix":"","host":"","scheme":""}'));

$command = new DumpCommand($this->extractor, $this->serializer, '/root/dir');
$command = new DumpCommand(
$this->routesResponse,
$this->extractor,
$this->serializer,
'/root/dir',
);

$tester = new CommandTester($command);
$tester->execute(['--target' => '/tmp']);
Expand Down
Loading
0