2014-06-17 17:05:00 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the NelmioApiDocBundle.
|
|
|
|
*
|
|
|
|
* (c) Nelmio <hello@nelm.io>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Nelmio\ApiDocBundle\Command;
|
|
|
|
|
2014-07-31 12:27:40 -07:00
|
|
|
use Nelmio\ApiDocBundle\Formatter\SwaggerFormatter;
|
2014-06-17 17:05:00 -07:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
|
2014-07-31 12:27:40 -07:00
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
2014-06-17 17:05:00 -07:00
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use Symfony\Component\Filesystem\Exception\IOException;
|
|
|
|
use Symfony\Component\Filesystem\Filesystem;
|
|
|
|
|
|
|
|
/**
|
2014-07-31 12:27:40 -07:00
|
|
|
* Console command to dump Swagger-compliant API definitions.
|
2014-06-17 17:05:00 -07:00
|
|
|
*
|
|
|
|
* @author Bez Hermoso <bez@activelamp.com>
|
|
|
|
*/
|
|
|
|
class SwaggerDumpCommand extends ContainerAwareCommand
|
|
|
|
{
|
2014-07-31 12:00:56 -07:00
|
|
|
/**
|
|
|
|
* @var Filesystem
|
|
|
|
*/
|
|
|
|
protected $filesystem;
|
|
|
|
|
2014-07-31 12:27:40 -07:00
|
|
|
/**
|
|
|
|
* @var SwaggerFormatter
|
|
|
|
*/
|
|
|
|
protected $formatter;
|
2014-07-31 12:00:56 -07:00
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
protected function configure()
|
|
|
|
{
|
2014-07-31 12:00:56 -07:00
|
|
|
$this->filesystem = new Filesystem();
|
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
$this
|
2014-07-31 12:27:40 -07:00
|
|
|
->setDescription('Dumps Swagger-compliant API definitions.')
|
|
|
|
->addOption('resource', 'r', InputOption::VALUE_OPTIONAL, 'A specific resource API declaration to dump.')
|
|
|
|
->addOption('list-only', 'l', InputOption::VALUE_NONE, 'Dump resource list only.')
|
|
|
|
->addOption('pretty', 'p', InputOption::VALUE_NONE, 'Dump as prettified JSON.')
|
|
|
|
->addArgument('destination', InputArgument::OPTIONAL, 'Directory to dump JSON files in.', null)
|
2014-06-17 17:05:00 -07:00
|
|
|
->setName('api:swagger:dump');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
|
|
|
$container = $this->getContainer();
|
|
|
|
|
2014-07-31 12:00:56 -07:00
|
|
|
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
|
2014-07-31 12:27:40 -07:00
|
|
|
$this->formatter = $container->get('nelmio_api_doc.formatter.swagger_formatter');
|
2014-06-17 17:05:00 -07:00
|
|
|
|
|
|
|
if ($input->getOption('list-only') && $input->getOption('resource')) {
|
|
|
|
throw new \RuntimeException('Cannot selectively dump a resource with the --list-only flag.');
|
|
|
|
}
|
|
|
|
|
2014-07-31 12:00:56 -07:00
|
|
|
$apiDocs = $extractor->all();
|
2014-06-17 17:05:00 -07:00
|
|
|
|
|
|
|
if ($input->getOption('list-only')) {
|
2014-07-31 12:00:56 -07:00
|
|
|
$data = $this->getResourceList($apiDocs, $output);
|
|
|
|
$this->dump($data, null, $input, $output);
|
2015-03-06 11:19:08 +01:00
|
|
|
|
2014-07-31 12:00:56 -07:00
|
|
|
return;
|
2014-06-17 17:05:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (false != ($resource = $input->getOption('resource'))) {
|
2014-07-31 12:00:56 -07:00
|
|
|
$data = $this->getApiDeclaration($apiDocs, $resource, $output);
|
|
|
|
if (count($data['apis']) === 0) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('Resource "%s" does not exist.', $resource));
|
|
|
|
}
|
|
|
|
$this->dump($data, $resource, $input, $output);
|
2015-03-06 11:19:08 +01:00
|
|
|
|
2014-07-31 12:00:56 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-31 12:27:40 -07:00
|
|
|
/*
|
|
|
|
* If --list-only and --resource is not specified, dump everything.
|
|
|
|
*/
|
2014-07-31 12:00:56 -07:00
|
|
|
$data = $this->getResourceList($apiDocs);
|
|
|
|
|
2014-07-31 12:27:40 -07:00
|
|
|
if (!$input->getArguments('destination')) {
|
2014-07-31 12:00:56 -07:00
|
|
|
$output->writeln('');
|
|
|
|
$output->writeln('<comment>Resource list: </comment>');
|
2014-06-17 17:05:00 -07:00
|
|
|
}
|
|
|
|
|
2014-07-31 12:00:56 -07:00
|
|
|
$this->dump($data, null, $input, $output, false);
|
|
|
|
|
|
|
|
foreach ($data['apis'] as $api) {
|
|
|
|
|
|
|
|
$resource = substr($api['path'], 1);
|
2014-07-31 12:27:40 -07:00
|
|
|
if (!$input->getArgument('destination')) {
|
2014-07-31 12:00:56 -07:00
|
|
|
$output->writeln('');
|
|
|
|
$output->writeln(sprintf('<comment>API declaration for <info>"%s"</info> resource: </comment>', $resource));
|
2014-06-17 17:05:00 -07:00
|
|
|
}
|
2014-07-31 12:00:56 -07:00
|
|
|
$data = $this->getApiDeclaration($apiDocs, $resource, $output);
|
|
|
|
$this->dump($data, $resource, $input, $output, false);
|
2014-06-17 17:05:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-31 12:00:56 -07:00
|
|
|
protected function dump(array $data, $resource, InputInterface $input, OutputInterface $output, $treatAsFile = true)
|
2014-06-17 17:05:00 -07:00
|
|
|
{
|
2014-07-31 12:27:40 -07:00
|
|
|
$destination = $input->getArgument('destination');
|
2014-06-17 17:05:00 -07:00
|
|
|
|
2014-07-31 12:27:40 -07:00
|
|
|
$content = json_encode($data, $input->getOption('pretty') ? JSON_PRETTY_PRINT : 0);
|
2014-06-17 17:05:00 -07:00
|
|
|
|
2014-07-31 12:27:40 -07:00
|
|
|
if (!$destination) {
|
2014-07-31 12:00:56 -07:00
|
|
|
$output->writeln($content);
|
2015-03-06 11:19:08 +01:00
|
|
|
|
2014-07-31 12:00:56 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-31 12:27:40 -07:00
|
|
|
if ($treatAsFile === false) {
|
|
|
|
if (!$this->filesystem->exists($destination)) {
|
|
|
|
$this->filesystem->mkdir($destination);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$resource) {
|
|
|
|
|
|
|
|
if (!$treatAsFile) {
|
|
|
|
$destination = sprintf('%s/api-docs.json', rtrim($destination, '\\/'));
|
2014-07-31 12:00:56 -07:00
|
|
|
}
|
2014-07-31 12:27:40 -07:00
|
|
|
$message = sprintf('<comment>Dumping resource list to %s: </comment>', $destination);
|
|
|
|
$this->writeToFile($content, $destination, $output, $message);
|
|
|
|
|
2014-07-31 12:00:56 -07:00
|
|
|
return;
|
|
|
|
}
|
2014-06-17 17:05:00 -07:00
|
|
|
|
2014-07-31 12:00:56 -07:00
|
|
|
if ($treatAsFile === false) {
|
2014-07-31 12:27:40 -07:00
|
|
|
$destination = sprintf('%s/%s.json', rtrim($destination, '\\/'), $resource);
|
2014-07-31 12:00:56 -07:00
|
|
|
}
|
2014-07-31 12:27:40 -07:00
|
|
|
|
|
|
|
$message = sprintf('<comment>Dump API declaration to %s: </comment>', $destination);
|
|
|
|
$this->writeToFile($content, $destination, $output, $message);
|
2014-07-31 12:00:56 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-31 12:27:40 -07:00
|
|
|
protected function writeToFile($content, $file, OutputInterface $output, $message)
|
2014-07-31 12:00:56 -07:00
|
|
|
{
|
2014-06-17 17:05:00 -07:00
|
|
|
try {
|
2014-07-31 12:00:56 -07:00
|
|
|
$this->filesystem->dumpFile($file, $content);
|
2014-07-31 12:27:40 -07:00
|
|
|
$message .= ' <info>OK</info>';
|
2014-06-17 17:05:00 -07:00
|
|
|
} catch (IOException $e) {
|
2014-07-31 12:27:40 -07:00
|
|
|
$message .= sprintf(' <error>NOT OK - %s</error>', $e->getMessage());
|
2014-06-17 17:05:00 -07:00
|
|
|
}
|
2014-07-31 12:27:40 -07:00
|
|
|
|
|
|
|
$output->writeln($message);
|
2014-06-17 17:05:00 -07:00
|
|
|
}
|
|
|
|
|
2014-07-31 12:00:56 -07:00
|
|
|
protected function getResourceList(array $data)
|
2014-06-17 17:05:00 -07:00
|
|
|
{
|
2014-07-31 12:27:40 -07:00
|
|
|
return $this->formatter->format($data);
|
2014-07-31 12:00:56 -07:00
|
|
|
}
|
2014-06-17 17:05:00 -07:00
|
|
|
|
2014-07-31 12:00:56 -07:00
|
|
|
protected function getApiDeclaration(array $data, $resource)
|
|
|
|
{
|
2014-07-31 12:27:40 -07:00
|
|
|
return $this->formatter->format($data, '/' . $resource);
|
2014-06-17 17:05:00 -07:00
|
|
|
}
|
2014-07-31 12:27:40 -07:00
|
|
|
}
|