Commit 9ae407fc authored by Fabien Potencier's avatar Fabien Potencier

Merge branch '1.3'

* 1.3:
  fixed translation override
  removed obsolete cookbook entry
  fixed CS
  updated the docs to show how to restrict a firewall by more than just the URL
parents 6679a4ea 79dec6aa
Translating Validation Messages
===============================
When working with Symfony validator, a common task would be to show localized
validation messages.
In order to do that, you will need to register translator and point to
translated resources::
$app->register(new Silex\Provider\TranslationServiceProvider(), array(
'locale' => 'sr_Latn',
'translator.domains' => array(),
));
$app->before(function () use ($app) {
$app['translator']->addLoader('xlf', new Symfony\Component\Translation\Loader\XliffFileLoader());
$app['translator']->addResource('xlf', __DIR__.'/vendor/symfony/validator/Symfony/Component/Validator/Resources/translations/validators/validators.sr_Latn.xlf', 'sr_Latn', 'validators');
});
And that's all you need to load translations from Symfony ``xlf`` files.
...@@ -129,10 +129,24 @@ under ``/admin/``:: ...@@ -129,10 +129,24 @@ under ``/admin/``::
), ),
); );
The ``pattern`` is a regular expression (it can also be a `RequestMatcher The ``pattern`` is a regular expression on the URL path; the ``http`` setting
tells the security layer to use HTTP basic authentication and the ``users``
entry defines valid users.
If you want to restrict the firewall by more than the URL pattern (like the
HTTP method, the client IP, the hostname, or any Request attributes), use an
instance of a `RequestMatcher
<http://api.symfony.com/master/Symfony/Component/HttpFoundation/RequestMatcher.html>`_ <http://api.symfony.com/master/Symfony/Component/HttpFoundation/RequestMatcher.html>`_
instance); the ``http`` setting tells the security layer to use HTTP basic for the ``pattern`` option::
authentication and the ``users`` entry defines valid users.
use Symfony/Component/HttpFoundation/RequestMatcher;
$app['security.firewalls'] = array(
'admin' => array(
'pattern' => new RequestMatcher('^/admin', 'example.com', 'POST'),
// ...
),
);
Each user is defined with the following information: Each user is defined with the following information:
......
...@@ -94,6 +94,22 @@ The above example will result in following routes: ...@@ -94,6 +94,22 @@ The above example will result in following routes:
* ``/it/hello/igor`` will return ``Hello igor`` (because of the fallback). * ``/it/hello/igor`` will return ``Hello igor`` (because of the fallback).
Using Resources
---------------
When translations are stored in a file, you can load them as follows::
$app = new Application();
$app->register(new TranslationServiceProvider());
$app->extend('translator.resources', function ($resources, $app) {
$resources = array_merge($resources, array(
array('array', array('This value should be a valid number.' => 'Cette valeur doit être un nombre.'), 'fr', 'validators'),
));
return $resources;
});
Traits Traits
------ ------
......
...@@ -40,6 +40,11 @@ class TranslationServiceProvider implements ServiceProviderInterface, EventListe ...@@ -40,6 +40,11 @@ class TranslationServiceProvider implements ServiceProviderInterface, EventListe
$translator->addLoader('array', new ArrayLoader()); $translator->addLoader('array', new ArrayLoader());
$translator->addLoader('xliff', new XliffFileLoader()); $translator->addLoader('xliff', new XliffFileLoader());
// Register default resources
foreach ($app['translator.resources'] as $resource) {
$translator->addResource($resource[0], $resource[1], $resource[2], $resource[3]);
}
foreach ($app['translator.domains'] as $domain => $data) { foreach ($app['translator.domains'] as $domain => $data) {
foreach ($data as $locale => $messages) { foreach ($data as $locale => $messages) {
$translator->addResource('array', $messages, $locale, $domain); $translator->addResource('array', $messages, $locale, $domain);
...@@ -57,6 +62,10 @@ class TranslationServiceProvider implements ServiceProviderInterface, EventListe ...@@ -57,6 +62,10 @@ class TranslationServiceProvider implements ServiceProviderInterface, EventListe
return new MessageSelector(); return new MessageSelector();
}; };
$app['translator.resources'] = function ($app) {
return array();
};
$app['translator.domains'] = array(); $app['translator.domains'] = array();
$app['locale_fallbacks'] = array('en'); $app['locale_fallbacks'] = array('en');
$app['translator.cache_dir'] = null; $app['translator.cache_dir'] = null;
......
...@@ -36,7 +36,13 @@ class ValidatorServiceProvider implements ServiceProviderInterface ...@@ -36,7 +36,13 @@ class ValidatorServiceProvider implements ServiceProviderInterface
$r = new \ReflectionClass('Symfony\Component\Validator\Validation'); $r = new \ReflectionClass('Symfony\Component\Validator\Validation');
$file = dirname($r->getFilename()).'/Resources/translations/validators.'.$app['locale'].'.xlf'; $file = dirname($r->getFilename()).'/Resources/translations/validators.'.$app['locale'].'.xlf';
if (file_exists($file)) { if (file_exists($file)) {
$app['translator']->addResource('xliff', $file, $app['locale'], 'validators'); $app->extend('translator.resources', function ($resources, $app) use ($file) {
$resources = array_merge(array(
array('xliff', $file, $app['locale'], 'validators'),
), $resources);
return $resources;
});
} }
} }
......
...@@ -48,7 +48,7 @@ class SecurityTraitTest extends \PHPUnit_Framework_TestCase ...@@ -48,7 +48,7 @@ class SecurityTraitTest extends \PHPUnit_Framework_TestCase
$request = Request::create('/'); $request = Request::create('/');
$app = $this->createApplication(array( $app = $this->createApplication(array(
'fabien' => array('ROLE_ADMIN', '5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg=='), 'fabien' => array('ROLE_ADMIN', '5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg=='),
'monique' => array('ROLE_USER', '5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg=='), 'monique' => array('ROLE_USER', '5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg=='),
)); ));
$app->get('/', function () { return 'foo'; }); $app->get('/', function () { return 'foo'; });
......
...@@ -162,22 +162,22 @@ class ExceptionHandlerTest extends \PHPUnit_Framework_TestCase ...@@ -162,22 +162,22 @@ class ExceptionHandlerTest extends \PHPUnit_Framework_TestCase
$errors = 0; $errors = 0;
$app->error(function ($e) use (&$errors) { $app->error(function ($e) use (&$errors) {
$errors++; ++$errors;
}); });
$app->error(function ($e) use (&$errors) { $app->error(function ($e) use (&$errors) {
$errors++; ++$errors;
}); });
$app->error(function ($e) use (&$errors) { $app->error(function ($e) use (&$errors) {
$errors++; ++$errors;
return new Response('foo exception handler'); return new Response('foo exception handler');
}); });
$app->error(function ($e) use (&$errors) { $app->error(function ($e) use (&$errors) {
// should not execute // should not execute
$errors++; ++$errors;
}); });
$request = Request::create('/foo'); $request = Request::create('/foo');
...@@ -198,7 +198,7 @@ class ExceptionHandlerTest extends \PHPUnit_Framework_TestCase ...@@ -198,7 +198,7 @@ class ExceptionHandlerTest extends \PHPUnit_Framework_TestCase
$errors = 0; $errors = 0;
$app->error(function ($e) use (&$errors) { $app->error(function ($e) use (&$errors) {
$errors++; ++$errors;
}); });
try { try {
......
...@@ -31,17 +31,17 @@ class MiddlewareTest extends \PHPUnit_Framework_TestCase ...@@ -31,17 +31,17 @@ class MiddlewareTest extends \PHPUnit_Framework_TestCase
$app->before(function () use (&$i, $test) { $app->before(function () use (&$i, $test) {
$test->assertEquals(0, $i); $test->assertEquals(0, $i);
$i++; ++$i;
}); });
$app->match('/foo', function () use (&$i, $test) { $app->match('/foo', function () use (&$i, $test) {
$test->assertEquals(1, $i); $test->assertEquals(1, $i);
$i++; ++$i;
}); });
$app->after(function () use (&$i, $test) { $app->after(function () use (&$i, $test) {
$test->assertEquals(2, $i); $test->assertEquals(2, $i);
$i++; ++$i;
}); });
$request = Request::create('/foo'); $request = Request::create('/foo');
...@@ -57,13 +57,13 @@ class MiddlewareTest extends \PHPUnit_Framework_TestCase ...@@ -57,13 +57,13 @@ class MiddlewareTest extends \PHPUnit_Framework_TestCase
$app = new Application(); $app = new Application();
$app->match('/foo', function () use (&$i) { $app->match('/foo', function () use (&$i) {
$i++; ++$i;
return new Response('foo'); return new Response('foo');
}); });
$app->after(function () use (&$i) { $app->after(function () use (&$i) {
$i++; ++$i;
}); });
$request = Request::create('/foo'); $request = Request::create('/foo');
...@@ -81,27 +81,27 @@ class MiddlewareTest extends \PHPUnit_Framework_TestCase ...@@ -81,27 +81,27 @@ class MiddlewareTest extends \PHPUnit_Framework_TestCase
$app->before(function () use (&$i, $test) { $app->before(function () use (&$i, $test) {
$test->assertEquals(0, $i); $test->assertEquals(0, $i);
$i++; ++$i;
}); });
$app->before(function () use (&$i, $test) { $app->before(function () use (&$i, $test) {
$test->assertEquals(1, $i); $test->assertEquals(1, $i);
$i++; ++$i;
}); });
$app->match('/foo', function () use (&$i, $test) { $app->match('/foo', function () use (&$i, $test) {
$test->assertEquals(2, $i); $test->assertEquals(2, $i);
$i++; ++$i;
}); });
$app->after(function () use (&$i, $test) { $app->after(function () use (&$i, $test) {
$test->assertEquals(3, $i); $test->assertEquals(3, $i);
$i++; ++$i;
}); });
$app->after(function () use (&$i, $test) { $app->after(function () use (&$i, $test) {
$test->assertEquals(4, $i); $test->assertEquals(4, $i);
$i++; ++$i;
}); });
$request = Request::create('/foo'); $request = Request::create('/foo');
...@@ -117,7 +117,7 @@ class MiddlewareTest extends \PHPUnit_Framework_TestCase ...@@ -117,7 +117,7 @@ class MiddlewareTest extends \PHPUnit_Framework_TestCase
$app = new Application(); $app = new Application();
$app->before(function () use (&$i) { $app->before(function () use (&$i) {
$i++; ++$i;
}); });
$app->match('/foo', function () { $app->match('/foo', function () {
...@@ -125,7 +125,7 @@ class MiddlewareTest extends \PHPUnit_Framework_TestCase ...@@ -125,7 +125,7 @@ class MiddlewareTest extends \PHPUnit_Framework_TestCase
}); });
$app->after(function () use (&$i) { $app->after(function () use (&$i) {
$i++; ++$i;
}); });
$app->error(function () { $app->error(function () {
...@@ -145,11 +145,11 @@ class MiddlewareTest extends \PHPUnit_Framework_TestCase ...@@ -145,11 +145,11 @@ class MiddlewareTest extends \PHPUnit_Framework_TestCase
$app = new Application(); $app = new Application();
$app->before(function () use (&$i) { $app->before(function () use (&$i) {
$i++; ++$i;
}, Application::EARLY_EVENT); }, Application::EARLY_EVENT);
$app->after(function () use (&$i) { $app->after(function () use (&$i) {
$i++; ++$i;
}); });
$app->error(function () { $app->error(function () {
......
...@@ -28,7 +28,7 @@ class HttpFragmentServiceProviderTest extends \PHPUnit_Framework_TestCase ...@@ -28,7 +28,7 @@ class HttpFragmentServiceProviderTest extends \PHPUnit_Framework_TestCase
$app->register(new TwigServiceProvider(), array( $app->register(new TwigServiceProvider(), array(
'twig.templates' => array( 'twig.templates' => array(
'hello' => '{{ render("/foo") }}{{ render_esi("/foo") }}{{ render_hinclude("/foo") }}', 'hello' => '{{ render("/foo") }}{{ render_esi("/foo") }}{{ render_hinclude("/foo") }}',
'foo' => 'foo', 'foo' => 'foo',
), ),
)); ));
......
...@@ -272,7 +272,7 @@ class SecurityServiceProviderTest extends WebTestCase ...@@ -272,7 +272,7 @@ class SecurityServiceProviderTest extends WebTestCase
'users' => array( 'users' => array(
// password is foo // password is foo
'fabien' => array('ROLE_USER', '5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg=='), 'fabien' => array('ROLE_USER', '5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg=='),
'admin' => array('ROLE_ADMIN', '5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg=='), 'admin' => array('ROLE_ADMIN', '5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg=='),
), ),
), ),
), ),
...@@ -323,7 +323,7 @@ class SecurityServiceProviderTest extends WebTestCase ...@@ -323,7 +323,7 @@ class SecurityServiceProviderTest extends WebTestCase
'users' => array( 'users' => array(
// password is foo // password is foo
'dennis' => array('ROLE_USER', '5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg=='), 'dennis' => array('ROLE_USER', '5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg=='),
'admin' => array('ROLE_ADMIN', '5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg=='), 'admin' => array('ROLE_ADMIN', '5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg=='),
), ),
), ),
), ),
......
...@@ -43,7 +43,7 @@ class TwigServiceProviderTest extends \PHPUnit_Framework_TestCase ...@@ -43,7 +43,7 @@ class TwigServiceProviderTest extends \PHPUnit_Framework_TestCase
{ {
$app = new Application(); $app = new Application();
$app->register(new TwigServiceProvider(), array( $app->register(new TwigServiceProvider(), array(
'twig.templates' => array('foo' => 'foo'), 'twig.templates' => array('foo' => 'foo'),
)); ));
$loader = $this->getMock('\Twig_LoaderInterface'); $loader = $this->getMock('\Twig_LoaderInterface');
$loader->expects($this->never())->method('getSource'); $loader->expects($this->never())->method('getSource');
......
...@@ -86,7 +86,7 @@ class ValidatorServiceProviderTest extends \PHPUnit_Framework_TestCase ...@@ -86,7 +86,7 @@ class ValidatorServiceProviderTest extends \PHPUnit_Framework_TestCase
)); ));
$builder = $app['form.factory']->createBuilder('form', array(), array( $builder = $app['form.factory']->createBuilder('form', array(), array(
'constraints' => $constraints, 'constraints' => $constraints,
'csrf_protection' => false, 'csrf_protection' => false,
)); ));
...@@ -132,4 +132,42 @@ class ValidatorServiceProviderTest extends \PHPUnit_Framework_TestCase ...@@ -132,4 +132,42 @@ class ValidatorServiceProviderTest extends \PHPUnit_Framework_TestCase
array('email@sample.com', true, 0, 0), array('email@sample.com', true, 0, 0),
); );
} }
public function testAddResource()
{
$app = new Application();
$app['locale'] = 'fr';
$app->register(new ValidatorServiceProvider());
$app->register(new TranslationServiceProvider());
$app['translator'] = $app->extend('translator', function ($translator, $app) {
$translator->addResource('array', array('This value should not be blank.' => 'Pas vide'), 'fr', 'validators');
return $translator;
});
$app['validator'];
$this->assertEquals('Pas vide', $app['translator']->trans('This value should not be blank.', array(), 'validators', 'fr'));
}
public function testAddResourceAlternate()
{
$app = new Application();
$app['locale'] = 'fr';
$app->register(new ValidatorServiceProvider());
$app->register(new TranslationServiceProvider());
$app->factory($app->extend('translator.resources', function ($resources, $app) {
$resources = array_merge($resources, array(
array('array', array('This value should not be blank.' => 'Pas vide'), 'fr', 'validators'),
));
return $resources;
}));
$app['validator'];
$this->assertEquals('Pas vide', $app['translator']->trans('This value should not be blank.', array(), 'validators', 'fr'));
}
} }
...@@ -35,7 +35,7 @@ class StreamTest extends \PHPUnit_Framework_TestCase ...@@ -35,7 +35,7 @@ class StreamTest extends \PHPUnit_Framework_TestCase
$i = 0; $i = 0;
$stream = function () use (&$i) { $stream = function () use (&$i) {
$i++; ++$i;
}; };
$app = new Application(); $app = new Application();
......
...@@ -69,7 +69,7 @@ class WebTestCaseTest extends WebTestCase ...@@ -69,7 +69,7 @@ class WebTestCaseTest extends WebTestCase
$client = $this->createClient(array( $client = $this->createClient(array(
'PHP_AUTH_USER' => $user, 'PHP_AUTH_USER' => $user,
'PHP_AUTH_PW' => $pass, 'PHP_AUTH_PW' => $pass,
)); ));
$crawler = $client->request('GET', '/server'); $crawler = $client->request('GET', '/server');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment