NelmioApiDocBundle/Command/DumpCommand.php

55 lines
1.8 KiB
PHP
Raw Normal View History

<?php
2012-04-12 18:37:42 +02:00
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
*/
2012-04-12 17:48:21 +02:00
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.formatter.simple_formatter');
} else {
2012-04-12 17:48:21 +02:00
if (!in_array($format, $this->availableFormats)) {
throw new \RuntimeException(sprintf('Format "%s" not supported.', $format));
}
$formatter = $this->getContainer()->get(sprintf('nelmio.api.formatter.%s_formatter', $format));
}
$extractedDoc = $this->getContainer()->get('nelmio.api.extractor.api_doc_extractor')->all();
2012-04-12 17:48:21 +02:00
$formattedDoc = $formatter->format($extractedDoc);
if ('json' === $format) {
2012-04-12 17:48:21 +02:00
$output->writeln(json_encode($formattedDoc));
} else {
2012-04-12 17:48:21 +02:00
$output->writeln($formattedDoc);
}
}
}