4 * CreateAPIDocumentation.php
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 OpenEMR\Common\Command\Runner\CommandContext
;
16 class CreateAPIDocumentationCommand
implements IOpenEMRCommand
19 * Prints the instructions on how to use this command
20 * @param CommandContext $context All the context about the command environment.
22 public function printUsage(CommandContext
$context)
24 echo "Command Usage: " . $context->getScriptName() . " -c CreateAPIDocumentation" . "\n";
28 * Returns a description of the command
31 public function getDescription(CommandContext
$context): string
33 return "Generates an OpenAPI swagger file that documents the OpenEMR API";
37 * Execute the command and spit any output to STDOUT and errors to STDERR
38 * @param CommandContext $context All the context information needed for the CLI Command to execute
40 public function execute(CommandContext
$context)
42 $routesLocation = $context->getRootPath() . "_rest_routes.inc.php";
43 $fileDestinationFolder = $context->getRootPath() . "swagger" . DIRECTORY_SEPARATOR
;
44 $fileDestinationYaml = $fileDestinationFolder . "openemr-api.yaml";
46 $openapi = \OpenApi\Generator
::scan([$routesLocation]);
48 $resultYaml = file_put_contents($fileDestinationYaml, $openapi->toYaml());
50 if ($resultYaml === false) {
51 echo "No write access to " . $fileDestinationYaml . "\n";
52 $this->printUsage($context);
55 echo "API file generated at " . $fileDestinationYaml . "\n";
56 echo "Your API documentation can now be viewed by going to <SITE_URL>/swagger/\n";
57 echo "For example on the easy docker installation this would be https://localhost:9300/swagger/\n";