forked from silexphp/Silex
-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
ValidatorServiceProvider.php
60 lines (49 loc) · 1.94 KB
/
ValidatorServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex\Provider;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Silex\ConstraintValidatorFactory;
use Symfony\Component\Validator\Validator;
use Symfony\Component\Validator\DefaultTranslator;
use Symfony\Component\Validator\Mapping\ClassMetadataFactory;
use Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader;
/**
* Symfony Validator component Provider.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ValidatorServiceProvider implements ServiceProviderInterface
{
public function register(Application $app)
{
$app['validator'] = $app->share(function ($app) {
$r = new \ReflectionClass('Symfony\Component\Validator\Validator');
if (isset($app['translator'])) {
$app['translator']->addResource('xliff', dirname($r->getFilename()).'/Resources/translations/validators.'.$app['locale'].'.xlf', $app['locale'], 'validators');
}
return new Validator(
$app['validator.mapping.class_metadata_factory'],
$app['validator.validator_factory'],
isset($app['translator']) ? $app['translator'] : new DefaultTranslator()
);
});
$app['validator.mapping.class_metadata_factory'] = $app->share(function ($app) {
return new ClassMetadataFactory(new StaticMethodLoader());
});
$app['validator.validator_factory'] = $app->share(function () use ($app) {
$validators = isset($app['validator.validator_service_ids']) ? $app['validator.validator_service_ids'] : array();
return new ConstraintValidatorFactory($app, $validators);
});
}
public function boot(Application $app)
{
}
}