From 6fc0d8619e31593d512845ff5e77f0006f460627 Mon Sep 17 00:00:00 2001 From: William DURAND Date: Thu, 12 Apr 2012 01:42:52 +0200 Subject: [PATCH] Added a command line to dump the whole documentation in markdown or json --- Command/DumpCommand.php | 64 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Command/DumpCommand.php diff --git a/Command/DumpCommand.php b/Command/DumpCommand.php new file mode 100644 index 0000000..6d2a2d1 --- /dev/null +++ b/Command/DumpCommand.php @@ -0,0 +1,64 @@ +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 { + $formatter = $this->getContainer()->get(sprintf('nelmio.api.formatter.%s_formatter', $format)); + } + + $results = array(); + foreach ($routeCollection->all() as $route) { + preg_match('#(.+)::([\w]+)#', $route->getDefault('_controller'), $matches); + $method = new \ReflectionMethod($matches[1], $matches[2]); + + if ($annot = $this->getContainer()->get('annotation_reader')->getMethodAnnotation($method, $this->annotationClass)) { + $results[] = $formatter->format($annot, $route); + } + } + + if ('json' === $format) { + $output->writeln(json_encode($results)); + } else { + foreach ($results as $result) { + $output->writeln($result); + } + } + } +}