|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Gii\Validator; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Aliases\Aliases; |
|
8
|
|
|
use Yiisoft\Validator\Exception\UnexpectedRuleException; |
|
9
|
|
|
use Yiisoft\Validator\Result; |
|
10
|
|
|
use Yiisoft\Validator\RuleHandlerInterface; |
|
11
|
|
|
use Yiisoft\Validator\ValidationContext; |
|
12
|
|
|
use Yiisoft\Yii\Gii\GiiParametersProvider; |
|
13
|
|
|
|
|
14
|
|
|
final class TemplateRuleHandler implements RuleHandlerInterface |
|
15
|
|
|
{ |
|
16
|
1 |
|
public function __construct( |
|
17
|
|
|
private Aliases $aliases, |
|
18
|
|
|
private GiiParametersProvider $parametersProvider, |
|
19
|
|
|
) { |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Validates the template selection. |
|
24
|
|
|
* This method validates whether the user selects an existing template |
|
25
|
|
|
* and the template contains all required template files as specified in {@see requiredTemplates()}. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $value |
|
28
|
|
|
*/ |
|
29
|
1 |
|
public function validate(mixed $value, object $rule, ValidationContext $context): Result |
|
30
|
|
|
{ |
|
31
|
1 |
|
if (!$rule instanceof TemplateRule) { |
|
32
|
|
|
throw new UnexpectedRuleException(TemplateRule::class, $rule); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
1 |
|
$result = new Result(); |
|
36
|
1 |
|
$templates = $this->parametersProvider->getTemplates(); |
|
37
|
1 |
|
if ($templates === []) { |
|
38
|
1 |
|
return $result; |
|
39
|
|
|
} |
|
40
|
|
|
if (!isset($templates[$value])) { |
|
41
|
|
|
$result->addError('Invalid template selection.'); |
|
42
|
|
|
} else { |
|
43
|
|
|
$templatePath = $templates[$value]; |
|
44
|
|
|
foreach ($this->parametersProvider->getRequiredTemplates() as $template) { |
|
45
|
|
|
if (!is_file($this->aliases->get($templatePath . '/' . $template))) { |
|
46
|
|
|
$result->addError("Unable to find the required code template file '$template'."); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $result; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|