fix: bug (#7552)
[openemr.git] / src / Common / Command / CreateAPIDocumentationCommand.php
blob1099028308f6da4689c2a9ed907e129e5c0cbbdf
1 <?php
3 /**
4 * CreateAPIDocumentation.php
5 * @package openemr
6 * @link http://www.open-emr.org
7 * @author Stephen Nielson <stephen@nielson.org>
8 * @copyright Copyright (c) 2021 Stephen Nielson <stephen@nielson.org>
9 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
12 namespace OpenEMR\Common\Command;
14 use Symfony\Component\Console\Command\Command;
15 use Symfony\Component\Console\Input\InputDefinition;
16 use Symfony\Component\Console\Input\InputInterface;
17 use Symfony\Component\Console\Input\InputOption;
18 use Symfony\Component\Console\Output\OutputInterface;
20 class CreateAPIDocumentationCommand extends Command
22 protected function configure()
24 $this
25 ->setName('openemr:create-api-documentation')
26 ->setDescription("Generates an OpenAPI swagger file that documents the OpenEMR API")
27 ->addUsage('--site=default')
28 ->setDefinition(
29 new InputDefinition([
30 new InputOption('site', null, InputOption::VALUE_REQUIRED, 'Name of site', 'default'),
34 protected function execute(InputInterface $input, OutputInterface $output): int
36 $routesLocation = $GLOBALS['fileroot'] . DIRECTORY_SEPARATOR . "_rest_routes.inc.php";
37 $fileDestinationFolder = $GLOBALS['fileroot'] . DIRECTORY_SEPARATOR . "swagger" . DIRECTORY_SEPARATOR;
38 $fileDestinationYaml = $fileDestinationFolder . "openemr-api.yaml";
39 $site = $input->getOption('site') ?? 'default';
41 $openapi = \OpenApi\Generator::scan([$routesLocation]);
43 $resultYaml = file_put_contents($fileDestinationYaml, $openapi->toYaml());
45 if ($resultYaml === false) {
46 $output->writeln("No write access to " . $fileDestinationYaml);
47 return Command::FAILURE;
48 } else {
49 $output->writeln("API file generated at " . $fileDestinationYaml);
50 $output->writeln("Your API documentation can now be viewed by going to <webroot>/swagger/");
51 $output->writeln("For example on the easy docker installation this would be https://localhost:9300/swagger/");
52 return Command::SUCCESS;