Skip to content
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

JSON dump #302

Merged
merged 6 commits into from
Nov 18, 2017
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
76 changes: 40 additions & 36 deletions Command/DumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,6 @@
*/
class DumpCommand extends ContainerAwareCommand
{
/**
* @var string
*/
private $targetPath;

/**
* @var \FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractorInterface
*/
private $extractor;

/**
* @var \Symfony\Component\Serializer\SerializerInterface
*/
private $serializer;

protected function configure()
{
$this
Expand All @@ -51,6 +36,13 @@ protected function configure()
'Callback function to pass the routes as an argument.',
'fos.Router.setData'
)
->addOption(
'format',
null,
InputOption::VALUE_REQUIRED,
'Format to output routes in. js to wrap the response in a callback, json for raw json output. Callback is ignored when format is json',
'js'
)
->addOption(
'target',
null,
Expand All @@ -73,23 +65,24 @@ protected function configure()
;
}

protected function initialize(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output)
{
parent::initialize($input, $output);

$this->targetPath = $input->getOption('target') ?:
sprintf('%s/../web/js/fos_js_routes.js', $this->getContainer()->getParameter('kernel.root_dir'));
if(!in_array($input->getOption('format'), array('js', 'json'))) {
$output->writeln('<error>Invalid format specified. Use js or json.</error>');
return 1;
}

$this->extractor = $this->getContainer()->get('fos_js_routing.extractor');
$this->serializer = $this->getContainer()->get('fos_js_routing.serializer');
}
$callback = $input->getOption('callback');
if(empty($callback)) {
$output->writeln('<error>If you include --callback it must not be empty. Do you perhaps want --format=json</error>');
return 1;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Dumping exposed routes.');
$output->writeln('');

$this->doDump($input, $output);
return 0;
}

/**
Expand All @@ -100,18 +93,27 @@ protected function execute(InputInterface $input, OutputInterface $output)
*/
private function doDump(InputInterface $input, OutputInterface $output)
{
if (!is_dir($dir = dirname($this->targetPath))) {
$extractor = $this->getContainer()->get('fos_js_routing.extractor');
$serializer = $this->getContainer()->get('fos_js_routing.serializer');
$targetPath = $input->getOption('target') ?:
sprintf(
'%s/../web/js/fos_js_routes.%s',
$this->getContainer()->getParameter('kernel.root_dir'),
$input->getOption('format')
);

if (!is_dir($dir = dirname($targetPath))) {
$output->writeln('<info>[dir+]</info> ' . $dir);
if (false === @mkdir($dir, 0777, true)) {
throw new \RuntimeException('Unable to create directory ' . $dir);
}
}

$output->writeln('<info>[file+]</info> ' . $this->targetPath);
$output->writeln('<info>[file+]</info> ' . $targetPath);

$baseUrl = $this->getContainer()->hasParameter('fos_js_routing.request_context_base_url') ?
$this->getContainer()->getParameter('fos_js_routing.request_context_base_url') :
$this->extractor->getBaseUrl()
$extractor->getBaseUrl()
;

if ($input->getOption('pretty-print')) {
Expand All @@ -120,22 +122,24 @@ private function doDump(InputInterface $input, OutputInterface $output)
$params = array();
}

$content = $this->serializer->serialize(
$content = $serializer->serialize(
new RoutesResponse(
$baseUrl,
$this->extractor->getRoutes(),
$this->extractor->getPrefix($input->getOption('locale')),
$this->extractor->getHost(),
$this->extractor->getScheme()
$extractor->getRoutes(),
$extractor->getPrefix($input->getOption('locale')),
$extractor->getHost(),
$extractor->getScheme()
),
'json',
$params
);

$content = sprintf("%s(%s);", $input->getOption('callback'), $content);
if('js' == $input->getOption('format')) {
$content = sprintf("%s(%s);", $input->getOption('callback'), $content);
}

if (false === @file_put_contents($this->targetPath, $content)) {
throw new \RuntimeException('Unable to write file ' . $this->targetPath);
if (false === @file_put_contents($targetPath, $content)) {
throw new \RuntimeException('Unable to write file ' . $targetPath);
}
}
}
33 changes: 33 additions & 0 deletions Tests/Command/DumpCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,39 @@ public function testExecuteCallbackOption()
$this->assertEquals('test({"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":""});', file_get_contents('/tmp/dump-command-test'));
}

public function testExecuteFormatOption()
{
$json = '{"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":""}';

$this->container->expects($this->at(0))
->method('get')
->with('fos_js_routing.extractor')
->will($this->returnValue($this->extractor));

$this->serializer->expects($this->once())
->method('serialize')
->will($this->returnValue($json));

$this->container->expects($this->at(1))
->method('get')
->with('fos_js_routing.serializer')
->will($this->returnValue($this->serializer));

$command = new DumpCommand();
$command->setContainer($this->container);

$tester = new CommandTester($command);
$tester->execute(array(
'--target' => '/tmp/dump-command-test',
'--format' => 'json',
));

$this->assertContains('Dumping exposed routes.', $tester->getDisplay());
$this->assertContains('[file+] /tmp/dump-command-test', $tester->getDisplay());

$this->assertEquals($json, file_get_contents('/tmp/dump-command-test'));
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Unable to create directory /../web/js
Expand Down