Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
Silex
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
Silex
Commits
3c07727a
Commit
3c07727a
authored
Apr 11, 2015
by
Fabien Potencier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
replaced custom listener by the Symfony built-in one
parent
8aeea4ba
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
2 additions
and
91 deletions
+2
-91
src/Silex/Provider/Translation/TranslatorListener.php
src/Silex/Provider/Translation/TranslatorListener.php
+0
-89
src/Silex/Provider/TranslationServiceProvider.php
src/Silex/Provider/TranslationServiceProvider.php
+1
-1
tests/Silex/Tests/Provider/TranslationServiceProviderTest.php
...s/Silex/Tests/Provider/TranslationServiceProviderTest.php
+1
-1
No files found.
src/Silex/Provider/Translation/TranslatorListener.php
deleted
100644 → 0
View file @
8aeea4ba
<?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\Translation
;
use
Symfony\Component\HttpKernel\KernelEvents
;
use
Symfony\Component\HttpKernel\Event\GetResponseEvent
;
use
Symfony\Component\HttpKernel\Event\FinishRequestEvent
;
use
Symfony\Component\HttpFoundation\RequestStack
;
use
Symfony\Component\HttpFoundation\Request
;
use
Symfony\Component\EventDispatcher\EventSubscriberInterface
;
use
Symfony\Component\Translation\TranslatorInterface
;
/**
* Initializes the Translator locale based on the Request locale.
*
* This listener works in 2 modes:
*
* * 2.3 compatibility mode where you must call setRequest whenever the Request changes.
* * 2.4+ mode where you must pass a RequestStack instance in the constructor.
*
* @author Mathieu Poisbeau <freepius44@gmail.com>
*/
class
TranslatorListener
implements
EventSubscriberInterface
{
protected
$translator
;
protected
$requestStack
;
/**
* RequestStack will become required in 3.0.
*/
public
function
__construct
(
TranslatorInterface
$translator
,
RequestStack
$requestStack
=
null
)
{
$this
->
translator
=
$translator
;
$this
->
requestStack
=
$requestStack
;
}
/**
* Sets the current Request.
*
* This method was used to synchronize the Request, but as the HttpKernel
* is doing that automatically now, you should never call it directly.
* It is kept public for BC with the 2.3 version.
*
* @param Request|null $request A Request instance
*
* @deprecated Deprecated since version 2.4, to be removed in 3.0.
*/
public
function
setRequest
(
Request
$request
=
null
)
{
if
(
null
===
$request
)
{
return
;
}
$this
->
translator
->
setLocale
(
$request
->
getLocale
());
}
public
function
onKernelRequest
(
GetResponseEvent
$event
)
{
$this
->
translator
->
setLocale
(
$event
->
getRequest
()
->
getLocale
());
}
public
function
onKernelFinishRequest
(
FinishRequestEvent
$event
)
{
if
(
null
===
$this
->
requestStack
)
{
return
;
// removed when requestStack is required
}
if
(
null
!==
$parentRequest
=
$this
->
requestStack
->
getParentRequest
())
{
$this
->
translator
->
setLocale
(
$parentRequest
->
getLocale
());
}
}
public
static
function
getSubscribedEvents
()
{
return
array
(
KernelEvents
::
REQUEST
=>
array
(
array
(
'onKernelRequest'
,
0
)),
KernelEvents
::
FINISH_REQUEST
=>
array
(
array
(
'onKernelFinishRequest'
,
0
)),
);
}
}
src/Silex/Provider/TranslationServiceProvider.php
View file @
3c07727a
...
@@ -18,8 +18,8 @@ use Symfony\Component\Translation\MessageSelector;
...
@@ -18,8 +18,8 @@ use Symfony\Component\Translation\MessageSelector;
use
Symfony\Component\Translation\Loader\ArrayLoader
;
use
Symfony\Component\Translation\Loader\ArrayLoader
;
use
Symfony\Component\Translation\Loader\XliffFileLoader
;
use
Symfony\Component\Translation\Loader\XliffFileLoader
;
use
Symfony\Component\EventDispatcher\EventDispatcherInterface
;
use
Symfony\Component\EventDispatcher\EventDispatcherInterface
;
use
Symfony\Component\HttpKernel\EventListener\TranslatorListener
;
use
Silex\Api\EventListenerProviderInterface
;
use
Silex\Api\EventListenerProviderInterface
;
use
Silex\Provider\Translation\TranslatorListener
;
/**
/**
* Symfony Translation component Provider.
* Symfony Translation component Provider.
...
...
tests/Silex/Tests/Provider/TranslationServiceProviderTest.php
View file @
3c07727a
...
@@ -167,7 +167,7 @@ class TranslationServiceProviderTest extends \PHPUnit_Framework_TestCase
...
@@ -167,7 +167,7 @@ class TranslationServiceProviderTest extends \PHPUnit_Framework_TestCase
public
function
testLocaleWithBefore
()
public
function
testLocaleWithBefore
()
{
{
$app
=
$this
->
getPreparedApp
();
$app
=
$this
->
getPreparedApp
();
$app
->
before
(
function
(
Request
$request
)
{
$request
->
setLocale
(
'fr'
);
});
$app
->
before
(
function
(
Request
$request
)
{
$request
->
setLocale
(
'fr'
);
}
,
Application
::
EARLY_EVENT
);
$app
->
get
(
'/embed'
,
function
()
use
(
$app
)
{
return
$app
[
'translator'
]
->
getLocale
();
});
$app
->
get
(
'/embed'
,
function
()
use
(
$app
)
{
return
$app
[
'translator'
]
->
getLocale
();
});
$app
->
get
(
'/'
,
function
()
use
(
$app
)
{
$app
->
get
(
'/'
,
function
()
use
(
$app
)
{
return
$app
[
'translator'
]
->
getLocale
()
.
return
$app
[
'translator'
]
->
getLocale
()
.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment