2012-04-12 01:42:52 +02:00
|
|
|
<?php
|
|
|
|
|
2012-04-13 11:03:05 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2012-04-12 18:37:42 +02:00
|
|
|
namespace Nelmio\ApiDocBundle\Command;
|
2012-04-12 01:42:52 +02:00
|
|
|
|
2015-05-26 18:09:29 +07:00
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
|
2012-04-12 01:42:52 +02:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2013-12-06 12:13:53 +05:30
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2012-04-12 01:42:52 +02:00
|
|
|
|
|
|
|
class DumpCommand extends ContainerAwareCommand
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2012-04-12 17:48:21 +02:00
|
|
|
protected $availableFormats = array('markdown', 'json', 'html');
|
2012-04-12 01:42:52 +02:00
|
|
|
|
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setDescription('')
|
|
|
|
->addOption(
|
|
|
|
'format', '', InputOption::VALUE_REQUIRED,
|
|
|
|
'Output format like: ' . implode(', ', $this->availableFormats),
|
|
|
|
$this->availableFormats[0]
|
|
|
|
)
|
2015-05-26 18:09:29 +07:00
|
|
|
->addOption('view', '', InputOption::VALUE_OPTIONAL, '', ApiDoc::DEFAULT_VIEW)
|
2012-07-18 16:51:53 +02:00
|
|
|
->addOption('no-sandbox', '', InputOption::VALUE_NONE)
|
2012-04-12 01:42:52 +02:00
|
|
|
->setName('api:doc:dump')
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
|
|
|
$format = $input->getOption('format');
|
2015-05-26 18:09:29 +07:00
|
|
|
$view = $input->getOption('view');
|
|
|
|
|
2012-04-12 01:42:52 +02:00
|
|
|
$routeCollection = $this->getContainer()->get('router')->getRouteCollection();
|
|
|
|
|
|
|
|
if (!$input->hasOption('format') || in_array($format, array('json'))) {
|
2012-04-12 18:44:38 +02:00
|
|
|
$formatter = $this->getContainer()->get('nelmio_api_doc.formatter.simple_formatter');
|
2012-04-12 01:42:52 +02:00
|
|
|
} else {
|
2012-04-12 17:48:21 +02:00
|
|
|
if (!in_array($format, $this->availableFormats)) {
|
|
|
|
throw new \RuntimeException(sprintf('Format "%s" not supported.', $format));
|
|
|
|
}
|
|
|
|
|
2012-04-12 18:44:38 +02:00
|
|
|
$formatter = $this->getContainer()->get(sprintf('nelmio_api_doc.formatter.%s_formatter', $format));
|
2012-04-12 01:42:52 +02:00
|
|
|
}
|
|
|
|
|
2013-12-06 12:13:53 +05:30
|
|
|
if ($input->getOption('no-sandbox') && 'html' === $format) {
|
2012-07-18 16:51:53 +02:00
|
|
|
$formatter->setEnableSandbox(false);
|
|
|
|
}
|
|
|
|
|
2015-12-15 17:04:17 +01:00
|
|
|
if ('html' === $format && method_exists($this->getContainer(), 'enterScope')) {
|
2013-12-06 12:13:53 +05:30
|
|
|
$this->getContainer()->enterScope('request');
|
|
|
|
$this->getContainer()->set('request', new Request(), 'request');
|
|
|
|
}
|
|
|
|
|
2015-05-26 18:09:29 +07:00
|
|
|
$extractedDoc = $this->getContainer()->get('nelmio_api_doc.extractor.api_doc_extractor')->all($view);
|
2012-04-12 17:48:21 +02:00
|
|
|
$formattedDoc = $formatter->format($extractedDoc);
|
2012-04-12 01:42:52 +02:00
|
|
|
|
|
|
|
if ('json' === $format) {
|
2012-04-12 17:48:21 +02:00
|
|
|
$output->writeln(json_encode($formattedDoc));
|
2012-04-12 01:42:52 +02:00
|
|
|
} else {
|
2012-04-12 17:48:21 +02:00
|
|
|
$output->writeln($formattedDoc);
|
2012-04-12 01:42:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|