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\Exception\UnexpectedRuleException; |
10
|
|
|
use Yiisoft\Validator\Result; |
11
|
|
|
use Yiisoft\Validator\RuleHandlerInterface; |
12
|
|
|
use Yiisoft\Validator\ValidationContext; |
13
|
|
|
use Yiisoft\Yii\Gii\GeneratorCommandInterface; |
14
|
|
|
use Yiisoft\Yii\Gii\GeneratorInterface; |
15
|
|
|
use Yiisoft\Yii\Gii\GiiInterface; |
16
|
|
|
use Yiisoft\Yii\Gii\ParametersProvider; |
17
|
|
|
|
18
|
|
|
final class TemplateRuleHandler implements RuleHandlerInterface |
19
|
|
|
{ |
20
|
7 |
|
public function __construct( |
21
|
|
|
private readonly Aliases $aliases, |
22
|
|
|
private readonly GiiInterface $gii, |
23
|
|
|
private readonly ParametersProvider $parametersProvider, |
24
|
|
|
) { |
25
|
7 |
|
} |
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
|
7 |
|
public function validate(mixed $value, object $rule, ValidationContext $context): Result |
33
|
|
|
{ |
34
|
7 |
|
if (!$rule instanceof TemplateRule) { |
35
|
|
|
throw new UnexpectedRuleException(TemplateRule::class, $rule); |
36
|
|
|
} |
37
|
7 |
|
$result = new Result(); |
38
|
|
|
|
39
|
7 |
|
if ($value === 'default') { |
40
|
3 |
|
return $result; |
41
|
|
|
} |
42
|
4 |
|
$command = $context->getRawData(); |
43
|
4 |
|
if (!$command instanceof GeneratorCommandInterface) { |
44
|
|
|
throw new RuntimeException(sprintf( |
45
|
|
|
'Unsupported dataset class "%s".', |
46
|
|
|
get_debug_type($command) |
47
|
|
|
)); |
48
|
|
|
} |
49
|
4 |
|
$generator = $this->getGenerator($command); |
50
|
4 |
|
$templates = $this->parametersProvider->getTemplates($generator::getId()); |
51
|
|
|
|
52
|
4 |
|
if ($templates === []) { |
53
|
|
|
return $result; |
54
|
|
|
} |
55
|
4 |
|
if (!isset($templates[$value])) { |
56
|
2 |
|
$result->addError( |
57
|
2 |
|
message: 'Template "{template}" does not exist. Known templates: {templates}', |
58
|
2 |
|
parameters: [ |
59
|
2 |
|
'template' => $value, |
60
|
2 |
|
'templates' => implode(', ', array_keys($templates)), |
61
|
2 |
|
], |
62
|
2 |
|
); |
63
|
2 |
|
return $result; |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
$templatePath = $templates[$value]; |
67
|
2 |
|
foreach ($generator->getRequiredTemplates() as $template) { |
68
|
2 |
|
if (!is_file($this->aliases->get($templatePath . '/' . $template))) { |
69
|
|
|
$result->addError( |
70
|
|
|
message: 'Unable to find the required code template file "{template}".', |
71
|
|
|
parameters: ['template' => $template], |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
return $result; |
77
|
|
|
} |
78
|
|
|
|
79
|
4 |
|
private function getGenerator(GeneratorCommandInterface $dataSet): GeneratorInterface |
80
|
|
|
{ |
81
|
4 |
|
foreach ($this->gii->getGenerators() as $generator) { |
82
|
4 |
|
if ($generator::getCommandClass() === $dataSet::class) { |
83
|
4 |
|
return $generator; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
throw new RuntimeException(sprintf('Unknown generator "%s".', $dataSet::class)); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|