|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the ModularBundle project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Anthonius Munthi <https://itstoni.com> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Doyo\Bundle\Modular\Compiler; |
|
15
|
|
|
|
|
16
|
|
|
use Doyo\Bundle\Modular\Application\ModuleInterface; |
|
17
|
|
|
use Doyo\Bundle\Modular\Modules; |
|
18
|
|
|
use Symfony\Component\Config\Resource\DirectoryResource; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
21
|
|
|
use Symfony\Component\Finder\Finder; |
|
22
|
|
|
|
|
23
|
|
|
class ValidationConfiguratorPass implements CompilerPassInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @return void |
|
27
|
|
|
*/ |
|
28
|
|
|
public function process(ContainerBuilder $container) |
|
29
|
|
|
{ |
|
30
|
|
|
$container->getDefinition('validator.builder'); |
|
31
|
|
|
/** @var Modules $modules */ |
|
32
|
|
|
$modules = $container->get('doyo.modules'); |
|
33
|
|
|
|
|
34
|
|
|
foreach ($modules->getModules() as $module) { |
|
35
|
|
|
$this->configureValidation($container, $module); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @psalm-suppress MixedArrayAccess |
|
41
|
|
|
* @psalm-suppress MixedArrayAssignment |
|
42
|
|
|
* @psalm-suppress MixedAssignment |
|
43
|
|
|
* @psalm-suppress UnusedClosureParam |
|
44
|
|
|
* @psalm-suppress MixedClosureParamType |
|
45
|
|
|
* @psalm-suppress MissingClosureParamType |
|
46
|
|
|
* @psalm-suppress MixedArgument |
|
47
|
|
|
* @noinspection PhpArrayUsedOnlyForWriteInspection |
|
48
|
|
|
*/ |
|
49
|
|
|
private function configureValidation(ContainerBuilder $container, ModuleInterface $module): void |
|
50
|
|
|
{ |
|
51
|
|
|
$files = []; |
|
52
|
|
|
$fileRecorder = function ($extension, $path) use (&$files): void { |
|
53
|
|
|
$files['xml'][] = $path; |
|
54
|
|
|
}; |
|
55
|
|
|
|
|
56
|
|
|
$path = $module->getBasePath().'/Resources/validation'; |
|
57
|
|
|
|
|
58
|
|
|
if (is_dir($path)) { |
|
59
|
|
|
$container->addResource(new DirectoryResource($path)); |
|
60
|
|
|
$this->registerMappingFilesFromDir($path, $fileRecorder); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$validatorBuilder = $container->getDefinition('validator.builder'); |
|
64
|
|
|
if (\array_key_exists('xml', $files)) { |
|
65
|
|
|
$validatorBuilder->addMethodCall('addXmlMappings', [$files['xml']]); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @psalm-suppress MixedMethodCall |
|
71
|
|
|
* @psalm-suppress MixedAssignment |
|
72
|
|
|
*/ |
|
73
|
|
|
private function registerMappingFilesFromDir(string $dir, \Closure $fileRecorder): void |
|
74
|
|
|
{ |
|
75
|
|
|
foreach (Finder::create()->followLinks()->files()->in($dir)->name('/\.(xml|ya?ml)$/')->sortByName() as $file) { |
|
76
|
|
|
$fileRecorder($file->getExtension(), $file->getRealPath()); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|