1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Gii\Controller; |
6
|
|
|
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use Yiisoft\DataResponse\DataResponse; |
9
|
|
|
use Yiisoft\DataResponse\DataResponseFactoryInterface; |
10
|
|
|
use Yiisoft\Http\Status; |
11
|
|
|
use Yiisoft\RequestModel\Attribute\Query; |
12
|
|
|
use Yiisoft\Yii\Gii\CodeFile; |
13
|
|
|
use Yiisoft\Yii\Gii\CodeFileSaver; |
14
|
|
|
use Yiisoft\Yii\Gii\Exception\InvalidGeneratorCommandException; |
15
|
|
|
use Yiisoft\Yii\Gii\GeneratorInterface; |
16
|
|
|
use Yiisoft\Yii\Gii\Request\GeneratorRequest; |
17
|
|
|
|
18
|
|
|
final class DefaultController |
19
|
|
|
{ |
20
|
|
|
public function __construct(private DataResponseFactoryInterface $responseFactory) |
21
|
|
|
{ |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function get(GeneratorRequest $request): ResponseInterface |
25
|
|
|
{ |
26
|
|
|
$generator = $request->getGenerator(); |
27
|
|
|
$params = [ |
28
|
|
|
'name' => $generator::getName(), |
29
|
|
|
'description' => $generator::getDescription(), |
30
|
|
|
'commandClass' => $generator::getCommandClass(), |
31
|
|
|
// 'templatePath' => $generator->getTemplatePath(), |
32
|
|
|
// 'templates' => $generator->getTemplates(), |
33
|
|
|
'directory' => $generator->getDirectory(), |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
return $this->responseFactory->createResponse($params); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function generate(GeneratorRequest $request, CodeFileSaver $codeFileSaver): ResponseInterface |
40
|
|
|
{ |
41
|
|
|
/** @var GeneratorInterface $generator */ |
42
|
|
|
$generator = $request->getGenerator(); |
43
|
|
|
$command = new ($generator::getCommandClass())(); |
|
|
|
|
44
|
|
|
$answers = $request->getAnswers(); |
45
|
|
|
try { |
46
|
|
|
$files = $generator->generate($command); |
47
|
|
|
} catch (InvalidGeneratorCommandException $e) { |
48
|
|
|
return $this->createErrorResponse($e); |
49
|
|
|
} |
50
|
|
|
$params = []; |
51
|
|
|
$results = []; |
52
|
|
|
$params['hasError'] = !$codeFileSaver->save($command, $files, (array) $answers, $results); |
53
|
|
|
$params['results'] = $results; |
54
|
|
|
return $this->responseFactory->createResponse($params); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function preview(GeneratorRequest $request, #[Query('file')] ?string $file = null): ResponseInterface |
58
|
|
|
{ |
59
|
|
|
/** @var GeneratorInterface $generator */ |
60
|
|
|
$generator = $request->getGenerator(); |
61
|
|
|
$command = new ($generator::getCommandClass())(); |
62
|
|
|
try { |
63
|
|
|
$files = $generator->generate($command); |
64
|
|
|
} catch (InvalidGeneratorCommandException $e) { |
65
|
|
|
return $this->createErrorResponse($e); |
66
|
|
|
} |
67
|
|
|
if ($file !== null) { |
68
|
|
|
foreach ($files as $generatedFile) { |
69
|
|
|
if ($generatedFile->getId() === $file) { |
70
|
|
|
$content = $generatedFile->preview(); |
71
|
|
|
return $this->responseFactory->createResponse( |
72
|
|
|
['content' => $content ?: 'Preview is not available for this file type.'] |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
return $this->responseFactory->createResponse( |
77
|
|
|
['message' => "Code file not found: $file"], |
78
|
|
|
Status::UNPROCESSABLE_ENTITY |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
return $this->responseFactory->createResponse(['files' => $files, 'operations' => CodeFile::OPERATIONS_MAP]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function diff(GeneratorRequest $request, #[Query('file')] string $file): ResponseInterface |
85
|
|
|
{ |
86
|
|
|
/** @var GeneratorInterface $generator */ |
87
|
|
|
$generator = $request->getGenerator(); |
88
|
|
|
$command = new ($generator::getCommandClass())(); |
89
|
|
|
try { |
90
|
|
|
$files = $generator->generate($command); |
91
|
|
|
} catch (InvalidGeneratorCommandException $e) { |
92
|
|
|
return $this->createErrorResponse($e); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
foreach ($files as $generatedFile) { |
96
|
|
|
if ($generatedFile->getId() === $file) { |
97
|
|
|
return $this->responseFactory->createResponse(['diff' => $generatedFile->diff()]); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
return $this->responseFactory->createResponse( |
101
|
|
|
['message' => "Code file not found: $file"], |
102
|
|
|
Status::UNPROCESSABLE_ENTITY |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private function createErrorResponse(InvalidGeneratorCommandException $e): DataResponse |
107
|
|
|
{ |
108
|
|
|
return $this->responseFactory->createResponse( |
109
|
|
|
// TODO: fix |
110
|
|
|
['errors' => $e->getResult()->getErrorMessagesIndexedByAttribute()], |
111
|
|
|
Status::UNPROCESSABLE_ENTITY |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|