|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Knp\Rad\AutoRegistration\DefinitionBuilder; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Inflector\Inflector; |
|
6
|
|
|
use Knp\Rad\AutoRegistration\DefinitionBuilder; |
|
7
|
|
|
use Knp\Rad\AutoRegistration\Finder\BundleFinder; |
|
8
|
|
|
use Knp\Rad\AutoRegistration\Kernel\KernelWrapper; |
|
9
|
|
|
use Knp\Rad\AutoRegistration\Reflection\ClassAnalyzer; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
11
|
|
|
|
|
12
|
|
|
class ConstraintValidatorBuilder implements DefinitionBuilder |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var KernelWrapper |
|
16
|
|
|
*/ |
|
17
|
|
|
private $kernel; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var BundleFinder |
|
21
|
|
|
*/ |
|
22
|
|
|
private $finder; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var ClassAnalyzer |
|
26
|
|
|
*/ |
|
27
|
|
|
private $analyzer; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param KernelWrapper $kernel |
|
31
|
|
|
* @param BundleFinder $finder |
|
32
|
|
|
* @param ClassAnalyzer $analyzer |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(KernelWrapper $kernel, BundleFinder $finder, ClassAnalyzer $analyzer) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->kernel = $kernel; |
|
37
|
|
|
$this->finder = $finder; |
|
38
|
|
|
$this->analyzer = $analyzer; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public function buildDefinitions() |
|
45
|
|
|
{ |
|
46
|
|
|
$definitions = []; |
|
47
|
|
|
|
|
48
|
|
|
$validators = $this->finder->findClasses( |
|
49
|
|
|
$this->kernel->getBundles(), |
|
50
|
|
|
['Validator', 'Validation'], |
|
51
|
|
|
'Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface' |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
foreach ($validators as $validator) { |
|
55
|
|
|
if (false === $this->analyzer->canBeConstructed($validator)) { |
|
56
|
|
|
continue; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$definitions[$validator] = (new Definition()) |
|
60
|
|
|
->setClass($validator) |
|
61
|
|
|
->setPublic(false) |
|
62
|
|
|
->addTag('validator.constraint_validator', ['alias_name' => $this->buildAlias($validator)]) |
|
63
|
|
|
; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $definitions; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getName() |
|
73
|
|
|
{ |
|
74
|
|
|
return 'constraint_validator'; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritdoc} |
|
79
|
|
|
*/ |
|
80
|
|
|
public function isActive() |
|
81
|
|
|
{ |
|
82
|
|
|
return true; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param string $class |
|
87
|
|
|
* |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
|
|
private function buildAlias($class) |
|
91
|
|
|
{ |
|
92
|
|
|
$parts = explode('\\', $class); |
|
93
|
|
|
$shortName = end($parts); |
|
94
|
|
|
|
|
95
|
|
|
if (false === strpos($shortName, 'Validator')) { |
|
96
|
|
|
return Inflector::tableize($shortName); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if ('Validator' !== substr($shortName, -9)) { |
|
100
|
|
|
return Inflector::tableize($shortName); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return Inflector::tableize(substr($shortName, 0, -9)); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|