2424 */
2525class 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}
0 commit comments