-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathDumpCommand.php
168 lines (145 loc) · 5.48 KB
/
DumpCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
declare(strict_types=1);
/*
* This file is part of the FOSJsRoutingBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\JsRoutingBundle\Command;
use FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractorInterface;
use FOS\JsRoutingBundle\Response\RoutesResponse;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Serializer\SerializerInterface;
/**
* Dumps routes to the filesystem.
*
* @author Benjamin Dulau <[email protected]>
*/
#[AsCommand('fos:js-routing:dump', 'Dumps exposed routes to the filesystem')]
class DumpCommand extends Command
{
public function __construct(
private RoutesResponse $routesResponse,
private ExposedRoutesExtractorInterface $extractor,
private SerializerInterface $serializer,
private string $projectDir,
private ?string $requestContextBaseUrl = null,
) {
parent::__construct();
}
protected function configure(): void
{
$this
->setName('fos:js-routing:dump')
->setDescription('Dumps exposed routes to the filesystem')
->addOption(
'callback',
null,
InputOption::VALUE_REQUIRED,
'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,
InputOption::VALUE_OPTIONAL,
'Override the target file to dump routes in.'
)
->addOption(
'locale',
null,
InputOption::VALUE_OPTIONAL,
'Set locale to be used with JMSI18nRoutingBundle.',
''
)
->addOption(
'pretty-print',
'p',
InputOption::VALUE_NONE,
'Pretty print the JSON.'
)
->addOption(
'domain',
null,
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'Specify expose domain',
[]
)
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (!in_array($input->getOption('format'), ['js', 'json'])) {
$output->writeln('<error>Invalid format specified. Use js or json.</error>');
return 1;
}
$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;
}
$output->writeln('Dumping exposed routes.');
$output->writeln('');
$this->doDump($input, $output);
return 0;
}
/**
* Performs the routes dump.
*/
private function doDump(InputInterface $input, OutputInterface $output): void
{
$domain = $input->getOption('domain');
$extractor = $this->extractor;
$serializer = $this->serializer;
$targetPath = $input->getOption('target') ?:
sprintf(
'%s/public/js/fos_js_routes%s.%s',
$this->projectDir,
empty($domain) ? '' : ('_'.implode('_', $domain)),
$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> '.$targetPath);
$baseUrl = $this->requestContextBaseUrl ?? $this->extractor->getBaseUrl()
;
if ($input->getOption('pretty-print')) {
$params = ['json_encode_options' => JSON_PRETTY_PRINT];
} else {
$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);
}
if (false === @file_put_contents($targetPath, $content)) {
throw new \RuntimeException('Unable to write file '.$targetPath);
}
}
}