7
7
* For the full copyright and license information, please view the LICENSE
8
8
* file that is distributed with this source code.
9
9
*/
10
+
10
11
namespace Lunetics \LocaleBundle \Switcher ;
11
12
12
- use Symfony \Component \HttpFoundation \Request ;
13
+ use Lunetics \LocaleBundle \LocaleInformation \AllowedLocalesProvider ;
14
+ use Symfony \Component \HttpFoundation \RequestStack ;
13
15
use Symfony \Component \Intl \Intl ;
14
16
use Symfony \Component \Routing \Exception \InvalidParameterException ;
15
17
use Symfony \Component \Routing \Exception \RouteNotFoundException ;
24
26
*/
25
27
class TargetInformationBuilder
26
28
{
27
- private $ request ;
29
+ /**
30
+ * @var RequestStack
31
+ */
32
+ private $ requestStack ;
33
+ /**
34
+ * @var RouterInterface
35
+ */
28
36
private $ router ;
37
+ /**
38
+ * @var bool
39
+ */
29
40
private $ showCurrentLocale ;
41
+ /**
42
+ * @var bool
43
+ */
30
44
private $ useController ;
31
- private $ allowedLocales ;
45
+ /**
46
+ * @var AllowedLocalesProvider
47
+ */
48
+ private $ allowedLocalesProvider ;
32
49
33
50
/**
34
- * @param Request $request Request
35
- * @param RouterInterface $router Router
36
- * @param array $allowedLocales Config Var
37
- * @param bool $showCurrentLocale Config Var
38
- * @param bool $useController Config Var
51
+ * @param RequestStack $requestStack Request
52
+ * @param RouterInterface $router Router
53
+ * @param AllowedLocalesProvider $allowedLocalesProvider
54
+ * @param bool $showCurrentLocale Config Var
55
+ * @param bool $useController Config Var
39
56
*/
40
- public function __construct (Request $ request , RouterInterface $ router , $ allowedLocales = array (), $ showCurrentLocale = false , $ useController = false )
41
- {
42
- $ this ->request = $ request ;
57
+ public function __construct (
58
+ RequestStack $ requestStack ,
59
+ RouterInterface $ router ,
60
+ AllowedLocalesProvider $ allowedLocalesProvider ,
61
+ $ showCurrentLocale = false ,
62
+ $ useController = false
63
+ ) {
64
+ $ this ->requestStack = $ requestStack ;
43
65
$ this ->router = $ router ;
44
- $ this ->allowedLocales = $ allowedLocales ;
66
+ $ this ->allowedLocalesProvider = $ allowedLocalesProvider ;
45
67
$ this ->showCurrentLocale = $ showCurrentLocale ;
46
68
$ this ->useController = $ useController ;
47
69
}
@@ -62,13 +84,13 @@ public function __construct(Request $request, RouterInterface $router, $allowedL
62
84
* locale_current_language: Anglais
63
85
*
64
86
* @param string|null $targetRoute The target route
65
- * @param array $parameters Parameters
87
+ * @param array $parameters Parameters
66
88
*
67
89
* @return array Informations for the switcher template
68
90
*/
69
- public function getTargetInformations ($ targetRoute = null , $ parameters = array () )
91
+ public function getTargetInformations ($ targetRoute = null , $ parameters = [] )
70
92
{
71
- $ request = $ this ->request ;
93
+ $ request = $ this ->requestStack -> getCurrentRequest () ;
72
94
$ router = $ this ->router ;
73
95
$ route = $ request ->attributes ->get ('_route ' );
74
96
@@ -83,12 +105,11 @@ public function getTargetInformations($targetRoute = null, $parameters = array()
83
105
84
106
$ infos ['current_locale ' ] = $ request ->getLocale ();
85
107
$ infos ['current_route ' ] = $ route ;
86
- $ infos ['locales ' ] = array () ;
108
+ $ infos ['locales ' ] = [] ;
87
109
88
110
$ parameters = array_merge ((array ) $ request ->attributes ->get ('_route_params ' ), $ request ->query ->all (), (array ) $ parameters );
89
111
90
- $ targetLocales = $ this ->allowedLocales ;
91
- foreach ($ targetLocales as $ locale ) {
112
+ foreach ($ this ->allowedLocalesProvider ->getAllowedLocales () as $ locale ) {
92
113
$ strpos = 0 === strpos ($ request ->getLocale (), $ locale );
93
114
if ($ this ->showCurrentLocale && $ strpos || !$ strpos ) {
94
115
$ targetLocaleTargetLang = Intl::getLanguageBundle ()->getLanguageName ($ locale , null , $ locale );
@@ -98,7 +119,7 @@ public function getTargetInformations($targetRoute = null, $parameters = array()
98
119
if (null !== $ targetRoute && "" !== $ targetRoute ) {
99
120
$ switchRoute = $ router ->generate ($ targetRoute , $ parameters );
100
121
} elseif ($ this ->useController ) {
101
- $ switchRoute = $ router ->generate ('lunetics_locale_switcher ' , array ( '_locale ' => $ locale) );
122
+ $ switchRoute = $ router ->generate ('lunetics_locale_switcher ' , [ '_locale ' => $ locale] );
102
123
} elseif ($ route ) {
103
124
$ switchRoute = $ router ->generate ($ route , $ parameters );
104
125
} else {
@@ -118,12 +139,12 @@ public function getTargetInformations($targetRoute = null, $parameters = array()
118
139
throw $ e ;
119
140
}
120
141
121
- $ infos ['locales ' ][$ locale ] = array (
142
+ $ infos ['locales ' ][$ locale ] = [
122
143
'locale_current_language ' => $ targetLocaleCurrentLang ,
123
144
'locale_target_language ' => $ targetLocaleTargetLang ,
124
145
'link ' => $ switchRoute ,
125
146
'locale ' => $ locale ,
126
- ) ;
147
+ ] ;
127
148
}
128
149
}
129
150
0 commit comments