Skip to content

Commit 5c011e6

Browse files
Matatiro Solutionstobias-93
authored andcommitted
JSON dump (#302), add format option
* #219 dump JSON as a step to WebPack support * #219 dump JSON as a step to WebPack support * Switch to PHP 5.3 array declaration * #219 dump JSON as a step to WebPack support * #219 Adding test (based on existing) for use of --format=json
1 parent 4068ac9 commit 5c011e6

File tree

2 files changed

+73
-36
lines changed

2 files changed

+73
-36
lines changed

Command/DumpCommand.php

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,6 @@
2424
*/
2525
class DumpCommand extends ContainerAwareCommand
2626
{
27-
/**
28-
* @var string
29-
*/
30-
private $targetPath;
31-
32-
/**
33-
* @var \FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractorInterface
34-
*/
35-
private $extractor;
36-
37-
/**
38-
* @var \Symfony\Component\Serializer\SerializerInterface
39-
*/
40-
private $serializer;
41-
4227
protected function configure()
4328
{
4429
$this
@@ -51,6 +36,13 @@ protected function configure()
5136
'Callback function to pass the routes as an argument.',
5237
'fos.Router.setData'
5338
)
39+
->addOption(
40+
'format',
41+
null,
42+
InputOption::VALUE_REQUIRED,
43+
'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',
44+
'js'
45+
)
5446
->addOption(
5547
'target',
5648
null,
@@ -73,23 +65,24 @@ protected function configure()
7365
;
7466
}
7567

76-
protected function initialize(InputInterface $input, OutputInterface $output)
68+
protected function execute(InputInterface $input, OutputInterface $output)
7769
{
78-
parent::initialize($input, $output);
79-
80-
$this->targetPath = $input->getOption('target') ?:
81-
sprintf('%s/../web/js/fos_js_routes.js', $this->getContainer()->getParameter('kernel.root_dir'));
70+
if(!in_array($input->getOption('format'), array('js', 'json'))) {
71+
$output->writeln('<error>Invalid format specified. Use js or json.</error>');
72+
return 1;
73+
}
8274

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

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

9284
$this->doDump($input, $output);
85+
return 0;
9386
}
9487

9588
/**
@@ -100,18 +93,27 @@ protected function execute(InputInterface $input, OutputInterface $output)
10093
*/
10194
private function doDump(InputInterface $input, OutputInterface $output)
10295
{
103-
if (!is_dir($dir = dirname($this->targetPath))) {
96+
$extractor = $this->getContainer()->get('fos_js_routing.extractor');
97+
$serializer = $this->getContainer()->get('fos_js_routing.serializer');
98+
$targetPath = $input->getOption('target') ?:
99+
sprintf(
100+
'%s/../web/js/fos_js_routes.%s',
101+
$this->getContainer()->getParameter('kernel.root_dir'),
102+
$input->getOption('format')
103+
);
104+
105+
if (!is_dir($dir = dirname($targetPath))) {
104106
$output->writeln('<info>[dir+]</info> ' . $dir);
105107
if (false === @mkdir($dir, 0777, true)) {
106108
throw new \RuntimeException('Unable to create directory ' . $dir);
107109
}
108110
}
109111

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

112114
$baseUrl = $this->getContainer()->hasParameter('fos_js_routing.request_context_base_url') ?
113115
$this->getContainer()->getParameter('fos_js_routing.request_context_base_url') :
114-
$this->extractor->getBaseUrl()
116+
$extractor->getBaseUrl()
115117
;
116118

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

123-
$content = $this->serializer->serialize(
125+
$content = $serializer->serialize(
124126
new RoutesResponse(
125127
$baseUrl,
126-
$this->extractor->getRoutes(),
127-
$this->extractor->getPrefix($input->getOption('locale')),
128-
$this->extractor->getHost(),
129-
$this->extractor->getScheme()
128+
$extractor->getRoutes(),
129+
$extractor->getPrefix($input->getOption('locale')),
130+
$extractor->getHost(),
131+
$extractor->getScheme()
130132
),
131133
'json',
132134
$params
133135
);
134136

135-
$content = sprintf("%s(%s);", $input->getOption('callback'), $content);
137+
if('js' == $input->getOption('format')) {
138+
$content = sprintf("%s(%s);", $input->getOption('callback'), $content);
139+
}
136140

137-
if (false === @file_put_contents($this->targetPath, $content)) {
138-
throw new \RuntimeException('Unable to write file ' . $this->targetPath);
141+
if (false === @file_put_contents($targetPath, $content)) {
142+
throw new \RuntimeException('Unable to write file ' . $targetPath);
139143
}
140144
}
141145
}

Tests/Command/DumpCommandTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,39 @@ public function testExecuteCallbackOption()
9797
$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'));
9898
}
9999

100+
public function testExecuteFormatOption()
101+
{
102+
$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":""}';
103+
104+
$this->container->expects($this->at(0))
105+
->method('get')
106+
->with('fos_js_routing.extractor')
107+
->will($this->returnValue($this->extractor));
108+
109+
$this->serializer->expects($this->once())
110+
->method('serialize')
111+
->will($this->returnValue($json));
112+
113+
$this->container->expects($this->at(1))
114+
->method('get')
115+
->with('fos_js_routing.serializer')
116+
->will($this->returnValue($this->serializer));
117+
118+
$command = new DumpCommand();
119+
$command->setContainer($this->container);
120+
121+
$tester = new CommandTester($command);
122+
$tester->execute(array(
123+
'--target' => '/tmp/dump-command-test',
124+
'--format' => 'json',
125+
));
126+
127+
$this->assertContains('Dumping exposed routes.', $tester->getDisplay());
128+
$this->assertContains('[file+] /tmp/dump-command-test', $tester->getDisplay());
129+
130+
$this->assertEquals($json, file_get_contents('/tmp/dump-command-test'));
131+
}
132+
100133
/**
101134
* @expectedException \RuntimeException
102135
* @expectedExceptionMessage Unable to create directory /../web/js

0 commit comments

Comments
 (0)