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\DataResponseFactoryInterface; |
9
|
|
|
use Yiisoft\Http\Status; |
10
|
|
|
use Yiisoft\RequestModel\Attribute\Query; |
11
|
|
|
use Yiisoft\Yii\Gii\CodeFile; |
12
|
|
|
use Yiisoft\Yii\Gii\Generator\AbstractGenerator; |
13
|
|
|
use Yiisoft\Yii\Gii\Request\GeneratorRequest; |
14
|
|
|
|
15
|
|
|
final class DefaultController |
16
|
|
|
{ |
17
|
|
|
public function __construct(private DataResponseFactoryInterface $responseFactory) |
18
|
|
|
{ |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function get(GeneratorRequest $request): ResponseInterface |
22
|
|
|
{ |
23
|
|
|
/** @var AbstractGenerator $generator */ |
24
|
|
|
$generator = $request->getGenerator(); |
25
|
|
|
$params = [ |
26
|
|
|
'name' => $generator->getName(), |
27
|
|
|
'description' => $generator->getDescription(), |
28
|
|
|
'template' => $generator->getTemplate(), |
29
|
|
|
'templatePath' => $generator->getTemplatePath(), |
30
|
|
|
'templates' => $generator->getTemplates(), |
31
|
|
|
'directory' => $generator->getDirectory(), |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
return $this->responseFactory->createResponse($params); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function generate(GeneratorRequest $request): ResponseInterface |
38
|
|
|
{ |
39
|
|
|
$generator = $request->getGenerator(); |
40
|
|
|
$answers = $request->getAnswers(); |
41
|
|
|
$validationResult = $generator->validate(); |
42
|
|
|
if ($validationResult->isValid()) { |
43
|
|
|
$generator->saveStickyAttributes(); |
|
|
|
|
44
|
|
|
$files = $generator->generate(); |
45
|
|
|
$params = []; |
46
|
|
|
$results = []; |
47
|
|
|
$params['hasError'] = !$generator->save($files, (array)$answers, $results); |
|
|
|
|
48
|
|
|
$params['results'] = $results; |
49
|
|
|
return $this->responseFactory->createResponse($params); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $this->responseFactory->createResponse( |
53
|
|
|
['errors' => $validationResult->getErrorMessagesIndexedByAttribute()], |
54
|
|
|
Status::UNPROCESSABLE_ENTITY |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function preview(GeneratorRequest $request, #[Query('file')] ?string $file = null): ResponseInterface |
59
|
|
|
{ |
60
|
|
|
$generator = $request->getGenerator(); |
61
|
|
|
$validationResult = $generator->validate(); |
62
|
|
|
if ($validationResult->isValid()) { |
63
|
|
|
$files = $generator->generate(); |
64
|
|
|
if ($file !== null) { |
65
|
|
|
foreach ($files as $generatedFile) { |
66
|
|
|
if ($generatedFile->getId() === $file) { |
67
|
|
|
$content = $generatedFile->preview(); |
68
|
|
|
if ($content !== false) { |
69
|
|
|
return $this->responseFactory->createResponse(['content' => $content]); |
70
|
|
|
} |
71
|
|
|
return $this->responseFactory->createResponse( |
72
|
|
|
['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]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->responseFactory->createResponse( |
85
|
|
|
['errors' => $validationResult->getErrorMessagesIndexedByAttribute()], |
86
|
|
|
Status::UNPROCESSABLE_ENTITY |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function diff(GeneratorRequest $request, #[Query('file')] string $file): ResponseInterface |
91
|
|
|
{ |
92
|
|
|
$generator = $request->getGenerator(); |
93
|
|
|
$validationResult = $generator->validate(); |
94
|
|
|
if ($validationResult->isValid()) { |
95
|
|
|
foreach ($generator->generate() 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
|
|
|
return $this->responseFactory->createResponse( |
107
|
|
|
['errors' => $validationResult->getErrorMessagesIndexedByAttribute()], |
108
|
|
|
Status::UNPROCESSABLE_ENTITY |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|