Skip to content

Commit

Permalink
#112 add Attribut OpenApi
Browse files Browse the repository at this point in the history
  • Loading branch information
bfoujols committed Aug 6, 2024
1 parent 3d8b346 commit 0531264
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 10 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"ext-pdo": "*",
"symfony/yaml": "6.3.*",
"symfony/console": "6.3.*",
"nette/php-generator": "^4.1@dev"
"nette/php-generator": "^4.1@dev",
"zircote/swagger-php": "^4.0@dev"
}
,
"require-dev": {
Expand Down
199 changes: 198 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 32 additions & 8 deletions src/EduFramework/Commands/CreateApiCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Studoo\EduFramework\Commands;

use Nette\PhpGenerator\PhpFile;
use PhpParser\Node\Expr\Array_;
use Studoo\EduFramework\Commands\Exception\ControllerAlreadyExistsException;
use Studoo\EduFramework\Commands\Exception\RouteAlreadyExistsException;
use Studoo\EduFramework\Core\Controller\ControllerInterface;
Expand Down Expand Up @@ -69,9 +70,11 @@ public function execute(InputInterface $input, OutputInterface $output): int
//Format controller-name arg
$namesCollection = $this->getNamesCollection($input->getArgument('controller-name'));
//Generate route params in app/Config/routes.yaml
$this->generateRoute($namesCollection["uri"], $namesCollection["uri"], $namesCollection["className"]);
$router = $this->generateRoute($namesCollection["uri"], $namesCollection["uri"], $namesCollection["className"]);
// Generate openapi.php
$this->generateOpenApi();
//Generate Controller Class
$this->generateController($namesCollection["className"]);
$this->generateController($namesCollection["className"], $router);
//Close command message
$io->success("Controller successfully generated");
return Command::SUCCESS;
Expand Down Expand Up @@ -106,10 +109,11 @@ private function getNamesCollection(string $arg): array
/**
* Fonction permettant de générer la classe PHP
* @param string $className Nom de la classe
* @param array $router Route générée
* @return void
* @throws ControllerAlreadyExistsException
*/
private function generateController(string $className): void
private function generateController(string $className, array $router): void
{
$filename = self::CONTROLLER_DIR . self::API_DIR . "$className.php";

Expand All @@ -127,12 +131,15 @@ private function generateController(string $className): void
$namespace->addUse('Twig\Error\LoaderError');
$namespace->addUse('Twig\Error\RuntimeError');
$namespace->addUse('Twig\Error\SyntaxError');
$namespace->addUse('OpenApi\Attributes');
//Generate ClassName
$class = $namespace->addClass($className);
//Add interface implementation
$class->addImplement(ControllerInterface::class);
//Create and design execute method
$method = $class->addMethod('execute');
$method->addAttribute('OpenApi\\Attributes\\Get', ['path' => $router['uri']]);
$method->addAttribute('OpenApi\\Attributes\\Response', ['response' => '200', 'description' => 'Mettre une description']);
$method->setReturnType('string|null');
$method->addParameter('request')->setType('Studoo\EduFramework\Core\Controller\Request');
$method->setBody(<<<'CODE'
Expand All @@ -153,13 +160,13 @@ private function generateController(string $className): void
/**
* Fonction permettant d'ajouter la route au fichier de configuration
* Par défaut la méthode lors de la génération est GET
* @param string $name
* @param string $uri
* @param string $className
* @return void
* @param string $name Nom de la route
* @param string $uri URI de la route
* @param string $className Nom de la classe
* @return array $router Route générée
* @throws RouteAlreadyExistsException
*/
private function generateRoute(string $name, string $uri, string $className): void
private function generateRoute(string $name, string $uri, string $className): array
{
if (is_file(self::ROUTES_FILE_PATH) === false) {
file_put_contents(self::ROUTES_FILE_PATH, '');
Expand All @@ -183,5 +190,22 @@ private function generateRoute(string $name, string $uri, string $className): vo
];

file_put_contents(self::ROUTES_FILE_PATH, Yaml::dump($router));

return $router[$indexName . $name];
}

private function generateOpenApi(): void
{
$filename = self::CONTROLLER_DIR . self::API_DIR . 'openapi.php';
if (file_exists($filename) === true) {
return;
}

$file = new PhpFile();
$namespace = $file->addNamespace("Controller\api");
$namespace->addUse('OpenApi\Attributes');
$class = $namespace->addClass('openapi');
$class->addAttribute('OpenApi\\Attributes\\Info', ['title' => 'My First API', 'version' => '0.1']);
file_put_contents($filename, $file);
}
}

0 comments on commit 0531264

Please sign in to comment.