|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Gii\Generator\Gemini; |
|
6
|
|
|
|
|
7
|
|
|
use Gemini; |
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
use Yiisoft\Aliases\Aliases; |
|
10
|
|
|
use Yiisoft\Validator\ValidatorInterface; |
|
11
|
|
|
use Yiisoft\Yii\Gii\Component\CodeFile\CodeFile; |
|
12
|
|
|
use Yiisoft\Yii\Gii\Generator\AbstractGenerator; |
|
13
|
|
|
use Yiisoft\Yii\Gii\GeneratorCommandInterface; |
|
14
|
|
|
use Yiisoft\Yii\Gii\ParametersProvider; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* This generator will generate a controller and one or a few action view files. |
|
18
|
|
|
*/ |
|
19
|
|
|
final class Generator extends AbstractGenerator |
|
20
|
|
|
{ |
|
21
|
|
|
public function __construct( |
|
22
|
|
|
Aliases $aliases, |
|
23
|
|
|
ValidatorInterface $validator, |
|
24
|
|
|
ParametersProvider $parametersProvider, |
|
25
|
|
|
) { |
|
26
|
|
|
parent::__construct($aliases, $validator, $parametersProvider); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public static function getId(): string |
|
30
|
|
|
{ |
|
31
|
|
|
return 'ai-gemini'; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public static function getName(): string |
|
35
|
|
|
{ |
|
36
|
|
|
return 'AI / Gemini'; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public static function getDescription(): string |
|
40
|
|
|
{ |
|
41
|
|
|
return ''; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function getRequiredTemplates(): array |
|
45
|
|
|
{ |
|
46
|
|
|
return [ |
|
47
|
|
|
'model.php', |
|
48
|
|
|
]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function doGenerate(GeneratorCommandInterface $command): array |
|
52
|
|
|
{ |
|
53
|
|
|
if (!$command instanceof Command) { |
|
54
|
|
|
throw new InvalidArgumentException(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$apiKey = getenv('GEMINI_API_KEY'); |
|
58
|
|
|
$client = Gemini::client($apiKey); |
|
59
|
|
|
|
|
60
|
|
|
//$prompt = $command->getPrompt(); |
|
61
|
|
|
$prompt = [ |
|
62
|
|
|
'Generate a test with PHPUnit for the following class', |
|
63
|
|
|
file_get_contents(__DIR__ . '/../../Gii.php'), |
|
64
|
|
|
]; |
|
65
|
|
|
$result = $client->geminiPro()->generateContent($prompt); |
|
66
|
|
|
|
|
67
|
|
|
$rootPath = $this->aliases->get('@root'); |
|
68
|
|
|
|
|
69
|
|
|
$path = $this->getOutputFile($command); |
|
70
|
|
|
$fileContent = preg_replace( |
|
71
|
|
|
'/^(```[a-z]+)\r?\n?|\r?\n?```$/i', |
|
72
|
|
|
'', |
|
73
|
|
|
$result->text(), |
|
74
|
|
|
); |
|
75
|
|
|
$codeFile = (new CodeFile( |
|
76
|
|
|
$path, |
|
77
|
|
|
$fileContent, |
|
78
|
|
|
))->withBasePath($rootPath); |
|
79
|
|
|
|
|
80
|
|
|
$files = []; |
|
81
|
|
|
$files[$codeFile->getId()] = $codeFile; |
|
82
|
|
|
|
|
83
|
|
|
return $files; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return string the controller class file path |
|
88
|
|
|
*/ |
|
89
|
|
|
private function getOutputFile(Command $command): string |
|
90
|
|
|
{ |
|
91
|
|
|
$directory = '@root/'; |
|
92
|
|
|
|
|
93
|
|
|
return $this->aliases->get( |
|
94
|
|
|
str_replace( |
|
95
|
|
|
['\\', '//'], |
|
96
|
|
|
'/', |
|
97
|
|
|
sprintf( |
|
98
|
|
|
'%s/%s', |
|
99
|
|
|
$directory, |
|
100
|
|
|
$command->getResultFilePath(), |
|
101
|
|
|
), |
|
102
|
|
|
), |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public static function getCommandClass(): string |
|
107
|
|
|
{ |
|
108
|
|
|
return Command::class; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|