mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 23:59:26 +03:00
6f85aed33c
Unified data types [actualType and subType] Updated tests. JMS parsing fixes; updated {Validator,FormType}Parser, FOSRestHandler, and AbstractFormatter, and updated DataTypes enum. Modified dataType checking. Updated tests. Updated DataTypes enum. Quick fix and added doc comments. CS fixes. Refactored FormTypeParser to produce nested parameters. Updated tests accordingly. Logical and CS fixes. Sub-forms and more tests. Logical and CS fixes. Swagger support: created formatter. Configuration and resourcePath logic update. ApiDoc annotation update. Updated formatter and added tests. Parameter formatting. Added tests for SwaggerFormatter. Added option in annotation, and the corresponding logic for parsing the supplied values and processing them in the formatter. Routing update. Updated tests. Removed unused dependency and updated doc comments. Renamed 'responseModels' to 'responseMap' Update the resource filtering and formatting of response messages. Updated check for 200 response model. Ignore data_class and always use form-type to avoid conflicts. Fix: add 'type' even if '' is specified. Refactored responseMap; added parsedResponseMap. Added tests and updated some. Fix: add 'type' even if '' is specified. Initial commit of command. Finished logic for dumping files. Updated doc comment; added license and added more meaningful class comment. Array of models support.
132 lines
4.7 KiB
PHP
132 lines
4.7 KiB
PHP
<?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;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
|
|
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;
|
|
|
|
|
|
/**
|
|
* Symfony2 command to dump Swagger-compliant JSON files.
|
|
*
|
|
* @author Bez Hermoso <bez@activelamp.com>
|
|
*/
|
|
class SwaggerDumpCommand extends ContainerAwareCommand
|
|
{
|
|
protected function configure()
|
|
{
|
|
$this
|
|
->setDescription('Dump Swagger-compliant JSON files.')
|
|
->addOption('resource', '', InputOption::VALUE_OPTIONAL, 'A specific resource API declaration to dump.')
|
|
->addOption('all', '', InputOption::VALUE_NONE, 'Dump resource list and all API declarations.')
|
|
->addOption('list-only', '', InputOption::VALUE_NONE, 'Dump resource list only.')
|
|
->addArgument('destination', InputOption::VALUE_REQUIRED, 'Directory to dump JSON files in.', null)
|
|
->setName('api:swagger:dump');
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
{
|
|
$container = $this->getContainer();
|
|
$destination = $input->getArgument('destination');
|
|
|
|
$rootDir = $container->get('kernel')->getRootDir();
|
|
$rootDir = $rootDir . '/..';
|
|
|
|
if (null === $destination) {
|
|
$destination = realpath($rootDir . '/' . $destination);
|
|
}
|
|
|
|
$fs = new Filesystem();
|
|
|
|
if (!$fs->exists($destination)) {
|
|
$fs->mkdir($destination);
|
|
}
|
|
|
|
$destination = realpath($destination);
|
|
|
|
if ($input->getOption('all') && $input->getOption('resource')) {
|
|
throw new \RuntimeException('Cannot selectively dump a resource with the --all flag.');
|
|
}
|
|
|
|
if ($input->getOption('list-only') && $input->getOption('resource')) {
|
|
throw new \RuntimeException('Cannot selectively dump a resource with the --list-only flag.');
|
|
}
|
|
|
|
if ($input->getOption('all') && $input->getOption('list-only')) {
|
|
throw new \RuntimeException('Cannot selectively dump resource list with the --all flag.');
|
|
}
|
|
|
|
$output->writeln('');
|
|
$output->writeln('Reading annotations...');
|
|
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
|
|
$data = $extractor->all();
|
|
|
|
if ($input->getOption('list-only')) {
|
|
$this->dumpResourceList($destination, $data, $output);
|
|
}
|
|
|
|
if (false != ($resource = $input->getOption('resource'))) {
|
|
$this->dumpApiDeclaration($destination, $data, $resource, $output);
|
|
}
|
|
|
|
if ($input->getOption('all')) {
|
|
$formatter = $container->get('nelmio_api_doc.formatter.swagger_formatter');
|
|
$this->dumpResourceList($destination, $data, $output);
|
|
$list = $formatter->format($data);
|
|
foreach ($list['apis'] as $api) {
|
|
$this->dumpApiDeclaration($destination, $data, substr($api['path'], 1), $output);
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function dumpResourceList($destination, array $data, OutputInterface $output)
|
|
{
|
|
$container = $this->getContainer();
|
|
$formatter = $container->get('nelmio_api_doc.formatter.swagger_formatter');
|
|
|
|
$list = $formatter->format($data);
|
|
|
|
$fs = new Filesystem();
|
|
$path = $destination . '/api-docs.json';
|
|
|
|
$string = sprintf('<comment>Dump resource list to %s: </comment>', $path);
|
|
try {
|
|
$fs->dumpFile($path, json_encode($list));
|
|
} catch (IOException $e) {
|
|
$output->writeln($string . ' <error>NOT OK</error>');
|
|
}
|
|
$output->writeln($string . '<info>OK</info>');
|
|
}
|
|
|
|
protected function dumpApiDeclaration($destination, array $data, $resource, OutputInterface $output)
|
|
{
|
|
$container = $this->getContainer();
|
|
$formatter = $container->get('nelmio_api_doc.formatter.swagger_formatter');
|
|
|
|
$list = $formatter->format($data, '/' . $resource);
|
|
|
|
$fs = new Filesystem();
|
|
$path = sprintf($destination . '/%s.json', $resource);
|
|
|
|
$string = sprintf('<comment>Dump API declaration to %s: </comment>', $path);
|
|
try {
|
|
$fs->dumpFile($path, json_encode($list));
|
|
} catch (IOException $e) {
|
|
$output->writeln($string . ' <error>NOT OK</error>');
|
|
}
|
|
$output->writeln($string . '<info>OK</info>');
|
|
}
|
|
} |