NelmioApiDocBundle/Command/DumpCommand.php

69 lines
2.2 KiB
PHP
Raw Normal View History

<?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;
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]
)
->addOption('no-sandbox', '', InputOption::VALUE_NONE)
->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'))) {
2012-04-12 18:44:38 +02:00
$formatter = $this->getContainer()->get('nelmio_api_doc.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));
}
2012-04-12 18:44:38 +02:00
$formatter = $this->getContainer()->get(sprintf('nelmio_api_doc.formatter.%s_formatter', $format));
}
if ($input->hasOption('no-sandbox') && 'html' === $format) {
$formatter->setEnableSandbox(false);
}
2012-04-12 18:44:38 +02:00
$extractedDoc = $this->getContainer()->get('nelmio_api_doc.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);
}
}
}