diff --git a/composer.json b/composer.json index c78c201b..d86d2af0 100644 --- a/composer.json +++ b/composer.json @@ -31,6 +31,7 @@ "prefer-stable": true, "require": { "php": "^8.1", + "google-gemini-php/client": "^1.0", "httpsoft/http-basis": "^1.1", "phpspec/php-diff": "^1.1.3", "psr/http-message": "^1.0|^2.0", diff --git a/config/params.php b/config/params.php index 818fb455..f385628e 100644 --- a/config/params.php +++ b/config/params.php @@ -33,6 +33,10 @@ 'class' => Generators\ActiveRecord\Generator::class, 'parameters' => [], ], + [ + 'class' => Generators\Gemini\Generator::class, + 'parameters' => [], + ], ], 'parameters' => [ 'templates' => [], diff --git a/src/Generator/Gemini/Command.php b/src/Generator/Gemini/Command.php new file mode 100644 index 00000000..7bda8441 --- /dev/null +++ b/src/Generator/Gemini/Command.php @@ -0,0 +1,60 @@ +resultFilePath; + } + + public function getPrompt(): string + { + return $this->prompt; + } + + public static function getAttributeLabels(): array + { + return [ + 'resultFilePath' => 'Model namespace', + 'prompt' => 'Text prompt', + 'template' => 'Template', + ]; + } + + public static function getHints(): array + { + return [ + 'resultFilePath' => 'Namespace for the model class to store it in the related directory.', + 'prompt' => 'Text to ask.', + ]; + } + + public static function getAttributes(): array + { + return [ + 'prompt', + 'resultFilePath', + 'template', + ]; + } +} diff --git a/src/Generator/Gemini/Generator.php b/src/Generator/Gemini/Generator.php new file mode 100644 index 00000000..77e2aa11 --- /dev/null +++ b/src/Generator/Gemini/Generator.php @@ -0,0 +1,110 @@ +getPrompt(); + $prompt = [ + 'Generate a test with PHPUnit for the following class', + file_get_contents(__DIR__ . '/../../Gii.php'), + ]; + $result = $client->geminiPro()->generateContent($prompt); + + $rootPath = $this->aliases->get('@root'); + + $path = $this->getOutputFile($command); + $fileContent = preg_replace( + '/^(```[a-z]+)\r?\n?|\r?\n?```$/i', + '', + $result->text(), + ); + $codeFile = (new CodeFile( + $path, + $fileContent, + ))->withBasePath($rootPath); + + $files = []; + $files[$codeFile->getId()] = $codeFile; + + return $files; + } + + /** + * @return string the controller class file path + */ + private function getOutputFile(Command $command): string + { + $directory = '@root/'; + + return $this->aliases->get( + str_replace( + ['\\', '//'], + '/', + sprintf( + '%s/%s', + $directory, + $command->getResultFilePath(), + ), + ), + ); + } + + public static function getCommandClass(): string + { + return Command::class; + } +}