mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 23:59:26 +03:00
* Add dump command to print out swagger JSON. Command outputs to stdout, so can be redirected to a file. Pretty prints output by default. --no-pretty can be passed to remove whitespace from json output if it does not need to be human readable * Update for styleci corrections * Fix CS and add type hint * REbase + Updates to documentation of dump command. * Remove defaultName - config file specifies this Add return value to dump command * Update for OA3 * Add a test case * update command name * Fix the tests Co-authored-by: Guilhem Niot <egetick@gmail.com> Co-authored-by: Guilhem Niot <guilhem.niot@gmail.com>
This commit is contained in:
parent
cd4aa3705e
commit
b3c53beead
78
Command/DumpCommand.php
Normal file
78
Command/DumpCommand.php
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the NelmioApiDocBundle package.
|
||||||
|
*
|
||||||
|
* (c) Nelmio
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Nelmio\ApiDocBundle\Command;
|
||||||
|
|
||||||
|
use Psr\Container\ContainerInterface;
|
||||||
|
use Symfony\Component\Console\Command\Command;
|
||||||
|
use Symfony\Component\Console\Exception\InvalidArgumentException;
|
||||||
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
|
||||||
|
class DumpCommand extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var ContainerInterface
|
||||||
|
*/
|
||||||
|
private $generatorLocator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DumpCommand constructor.
|
||||||
|
*
|
||||||
|
* @param ContainerInterface $generatorLocator
|
||||||
|
*/
|
||||||
|
public function __construct(ContainerInterface $generatorLocator)
|
||||||
|
{
|
||||||
|
$this->generatorLocator = $generatorLocator;
|
||||||
|
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configures the dump command.
|
||||||
|
*/
|
||||||
|
protected function configure()
|
||||||
|
{
|
||||||
|
$this
|
||||||
|
->setDescription('Dumps documentation in OpenAPI JSON format')
|
||||||
|
->addOption('area', '', InputOption::VALUE_OPTIONAL, '', 'default')
|
||||||
|
->addOption('no-pretty', '', InputOption::VALUE_NONE, 'Do not pretty format output')
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param InputInterface $input
|
||||||
|
* @param OutputInterface $output
|
||||||
|
*
|
||||||
|
* @throws InvalidArgumentException If the area to dump is not valid
|
||||||
|
*
|
||||||
|
* @return int|void
|
||||||
|
*/
|
||||||
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||||||
|
{
|
||||||
|
$area = $input->getOption('area');
|
||||||
|
|
||||||
|
if (!$this->generatorLocator->has($area)) {
|
||||||
|
throw new InvalidArgumentException(sprintf('Area "%s" is not supported.', $area));
|
||||||
|
}
|
||||||
|
|
||||||
|
$spec = $this->generatorLocator->get($area)->generate();
|
||||||
|
|
||||||
|
if ($input->hasParameterOption(['--no-pretty'])) {
|
||||||
|
$output->writeln(json_encode($spec));
|
||||||
|
} else {
|
||||||
|
$output->writeln(json_encode($spec, JSON_PRETTY_PRINT));
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
@ -4,6 +4,12 @@
|
|||||||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
|
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
|
||||||
|
|
||||||
<services>
|
<services>
|
||||||
|
<!-- Commands -->
|
||||||
|
<service id="nelmio_api_doc.command.dump" class="Nelmio\ApiDocBundle\Command\DumpCommand" public="true">
|
||||||
|
<argument type="service" id="nelmio_api_doc.generator_locator" />
|
||||||
|
<tag name="console.command" command="nelmio:apidoc:dump" />
|
||||||
|
</service>
|
||||||
|
|
||||||
<!-- Controllers -->
|
<!-- Controllers -->
|
||||||
<service id="nelmio_api_doc.controller.swagger_ui" class="Nelmio\ApiDocBundle\Controller\SwaggerUiController" public="true">
|
<service id="nelmio_api_doc.controller.swagger_ui" class="Nelmio\ApiDocBundle\Controller\SwaggerUiController" public="true">
|
||||||
<argument type="service" id="nelmio_api_doc.generator_locator" />
|
<argument type="service" id="nelmio_api_doc.generator_locator" />
|
||||||
|
36
Tests/Command/DumpCommandTest.php
Normal file
36
Tests/Command/DumpCommandTest.php
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the NelmioApiDocBundle package.
|
||||||
|
*
|
||||||
|
* (c) Nelmio
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Nelmio\ApiDocBundle\Tests\Command;
|
||||||
|
|
||||||
|
use Nelmio\ApiDocBundle\Tests\Functional\WebTestCase; // for the creation of the kernel
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Console\Application;
|
||||||
|
use Symfony\Component\Console\Tester\CommandTester;
|
||||||
|
|
||||||
|
class DumpCommandTest extends WebTestCase
|
||||||
|
{
|
||||||
|
public function testExecute()
|
||||||
|
{
|
||||||
|
$kernel = static::bootKernel();
|
||||||
|
$application = new Application($kernel);
|
||||||
|
|
||||||
|
$command = $application->find('nelmio:apidoc:dump');
|
||||||
|
$commandTester = new CommandTester($command);
|
||||||
|
$commandTester->execute([
|
||||||
|
'--area' => 'test',
|
||||||
|
'--no-pretty' => '',
|
||||||
|
]);
|
||||||
|
|
||||||
|
// the output of the command in the console
|
||||||
|
$output = $commandTester->getDisplay();
|
||||||
|
$this->assertEquals(json_encode($this->getOpenApiDefinition('test'))."\n", $output);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user