Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
74 lines (62 sloc) 2.665 kB
<?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 Pimple\Container;
use Pimple\ServiceProviderInterface;
use Silex\Provider\Validator\ConstraintValidatorFactory;
use Silex\Application;
use Symfony\Component\Validator\Validator;
use Symfony\Component\Validator\DefaultTranslator;
use Symfony\Component\Validator\Mapping\ClassMetadataFactory;
use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
use Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader;
use Symfony\Component\Validator\Validation;
/**
* Symfony Validator component Provider.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ValidatorServiceProvider implements ServiceProviderInterface
{
public function register(Container $app)
{
$app['validator'] = function ($app) {
if (isset($app['translator'])) {
$r = new \ReflectionClass('Symfony\Component\Validator\Validation');
$file = dirname($r->getFilename()).'/Resources/translations/validators.'.$app['locale'].'.xlf';
if (file_exists($file)) {
$app['translator']->addResource('xliff', $file, $app['locale'], 'validators');
}
}
return $app['validator.builder']->getValidator();
};
$app['validator.builder'] = function ($app) {
$builder = Validation::createValidatorBuilder();
$builder->setConstraintValidatorFactory($app['validator.validator_factory']);
$builder->setTranslationDomain('validators');
$builder->addObjectInitializers($app['validator.object_initializers']);
$builder->setMetadataFactory($app['validator.mapping.class_metadata_factory']);
if (isset($app['translator'])) {
$builder->setTranslator($app['translator']);
}
return $builder;
};
$app['validator.mapping.class_metadata_factory'] = function ($app) {
return new LazyLoadingMetadataFactory(new StaticMethodLoader());
};
$app['validator.validator_factory'] = function () use ($app) {
$validators = isset($app['validator.validator_service_ids']) ? $app['validator.validator_service_ids'] : array();
return new ConstraintValidatorFactory($app, $validators);
};
$app['validator.object_initializers'] = function ($app) {
return array();
};
}
}
Jump to Line
Something went wrong with that request. Please try again.