1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Gii\Validator; |
6
|
|
|
|
7
|
|
|
use RuntimeException; |
8
|
|
|
use Yiisoft\Aliases\Aliases; |
9
|
|
|
use Yiisoft\Validator\DataSet\ObjectDataSet; |
10
|
|
|
use Yiisoft\Validator\Exception\UnexpectedRuleException; |
11
|
|
|
use Yiisoft\Validator\Result; |
12
|
|
|
use Yiisoft\Validator\RuleHandlerInterface; |
13
|
|
|
use Yiisoft\Validator\ValidationContext; |
14
|
|
|
use Yiisoft\Yii\Gii\GeneratorCommandInterface; |
15
|
|
|
use Yiisoft\Yii\Gii\GiiInterface; |
16
|
|
|
use Yiisoft\Yii\Gii\GiiParametersProvider; |
17
|
|
|
|
18
|
|
|
final class TemplateRuleHandler implements RuleHandlerInterface |
19
|
|
|
{ |
20
|
3 |
|
public function __construct( |
21
|
|
|
private Aliases $aliases, |
22
|
|
|
private GiiInterface $gii, |
23
|
|
|
private GiiParametersProvider $parametersProvider, |
24
|
|
|
) { |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Validates the template selection. |
29
|
|
|
* This method validates whether the user selects an existing template |
30
|
|
|
* and the template contains all required template files as specified in {@see requiredTemplates()}. |
31
|
|
|
* |
32
|
|
|
* @param string $value |
33
|
|
|
*/ |
34
|
3 |
|
public function validate(mixed $value, object $rule, ValidationContext $context): Result |
35
|
|
|
{ |
36
|
3 |
|
if (!$rule instanceof TemplateRule) { |
37
|
|
|
throw new UnexpectedRuleException(TemplateRule::class, $rule); |
38
|
|
|
} |
39
|
3 |
|
$result = new Result(); |
40
|
|
|
|
41
|
3 |
|
if ($value === 'default') { |
42
|
1 |
|
return $result; |
43
|
|
|
} |
44
|
2 |
|
$command = $context->getDataSet(); |
45
|
2 |
|
if (!$command instanceof ObjectDataSet || !$command->getObject() instanceof GeneratorCommandInterface) { |
46
|
|
|
// TODO |
47
|
|
|
throw new RuntimeException('Unsupported dataset class'); |
48
|
|
|
} |
49
|
2 |
|
$selectedGenerator = null; |
50
|
2 |
|
foreach ($this->gii->getGenerators() as $generator) { |
51
|
2 |
|
if ($generator::getCommandClass() === $command->getObject()::class) { |
52
|
2 |
|
$selectedGenerator = $generator; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
// TODO |
56
|
2 |
|
if ($selectedGenerator === null) { |
57
|
|
|
throw new RuntimeException('Unknown generator'); |
58
|
|
|
} |
59
|
2 |
|
$templates = $this->parametersProvider->getTemplates($selectedGenerator::getId()); |
60
|
|
|
|
61
|
2 |
|
if ($templates === []) { |
62
|
|
|
return $result; |
63
|
|
|
} |
64
|
2 |
|
if (!isset($templates[$value])) { |
65
|
2 |
|
$result->addError( |
66
|
|
|
message: 'Template "{template}" does not exist. Known templates: {templates}', |
67
|
|
|
parameters: [ |
68
|
2 |
|
'template' => $value, |
69
|
2 |
|
'templates' => implode(', ', array_keys($templates)), |
70
|
|
|
], |
71
|
|
|
); |
72
|
2 |
|
return $result; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$templatePath = $templates[$value]; |
76
|
|
|
foreach ($selectedGenerator->getRequiredTemplates() as $template) { |
77
|
|
|
if (!is_file($this->aliases->get($templatePath . '/' . $template))) { |
78
|
|
|
$result->addError( |
79
|
|
|
message: 'Unable to find the required code template file "{template}".', |
80
|
|
|
parameters: ['template' => $template], |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $result; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|