NelmioApiDocBundle/Command/DumpCommand.php
William DURAND 5bd05379ec Fixed naming
2012-04-12 19:10:16 +02:00

55 lines
1.8 KiB
PHP

<?php
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;
class DumpCommand extends ContainerAwareCommand
{
/**
* @var array
*/
protected $availableFormats = array('markdown', 'json', 'html');
protected function configure()
{
$this
->setDescription('')
->addOption(
'format', '', InputOption::VALUE_REQUIRED,
'Output format like: ' . implode(', ', $this->availableFormats),
$this->availableFormats[0]
)
->setName('api:doc:dump')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$format = $input->getOption('format');
$routeCollection = $this->getContainer()->get('router')->getRouteCollection();
if (!$input->hasOption('format') || in_array($format, array('json'))) {
$formatter = $this->getContainer()->get('nelmio_api_doc.formatter.simple_formatter');
} else {
if (!in_array($format, $this->availableFormats)) {
throw new \RuntimeException(sprintf('Format "%s" not supported.', $format));
}
$formatter = $this->getContainer()->get(sprintf('nelmio_api_doc.formatter.%s_formatter', $format));
}
$extractedDoc = $this->getContainer()->get('nelmio_api_doc.extractor.api_doc_extractor')->all();
$formattedDoc = $formatter->format($extractedDoc);
if ('json' === $format) {
$output->writeln(json_encode($formattedDoc));
} else {
$output->writeln($formattedDoc);
}
}
}