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); + } + } + } +}