77 * For the full copyright and license information, please view the LICENSE
88 * file that is distributed with this source code.
99 */
10+
1011namespace Lunetics \LocaleBundle \Switcher ;
1112
12- use Symfony \Component \HttpFoundation \Request ;
13+ use Lunetics \LocaleBundle \LocaleInformation \AllowedLocalesProvider ;
14+ use Symfony \Component \HttpFoundation \RequestStack ;
1315use Symfony \Component \Intl \Intl ;
1416use Symfony \Component \Routing \Exception \InvalidParameterException ;
1517use Symfony \Component \Routing \Exception \RouteNotFoundException ;
2426 */
2527class TargetInformationBuilder
2628{
27- private $ request ;
29+ /**
30+ * @var RequestStack
31+ */
32+ private $ requestStack ;
33+ /**
34+ * @var RouterInterface
35+ */
2836 private $ router ;
37+ /**
38+ * @var bool
39+ */
2940 private $ showCurrentLocale ;
41+ /**
42+ * @var bool
43+ */
3044 private $ useController ;
31- private $ allowedLocales ;
45+ /**
46+ * @var AllowedLocalesProvider
47+ */
48+ private $ allowedLocalesProvider ;
3249
3350 /**
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
3956 */
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 ;
4365 $ this ->router = $ router ;
44- $ this ->allowedLocales = $ allowedLocales ;
66+ $ this ->allowedLocalesProvider = $ allowedLocalesProvider ;
4567 $ this ->showCurrentLocale = $ showCurrentLocale ;
4668 $ this ->useController = $ useController ;
4769 }
@@ -62,13 +84,13 @@ public function __construct(Request $request, RouterInterface $router, $allowedL
6284 * locale_current_language: Anglais
6385 *
6486 * @param string|null $targetRoute The target route
65- * @param array $parameters Parameters
87+ * @param array $parameters Parameters
6688 *
6789 * @return array Informations for the switcher template
6890 */
69- public function getTargetInformations ($ targetRoute = null , $ parameters = array () )
91+ public function getTargetInformations ($ targetRoute = null , $ parameters = [] )
7092 {
71- $ request = $ this ->request ;
93+ $ request = $ this ->requestStack -> getCurrentRequest () ;
7294 $ router = $ this ->router ;
7395 $ route = $ request ->attributes ->get ('_route ' );
7496
@@ -83,12 +105,11 @@ public function getTargetInformations($targetRoute = null, $parameters = array()
83105
84106 $ infos ['current_locale ' ] = $ request ->getLocale ();
85107 $ infos ['current_route ' ] = $ route ;
86- $ infos ['locales ' ] = array () ;
108+ $ infos ['locales ' ] = [] ;
87109
88110 $ parameters = array_merge ((array ) $ request ->attributes ->get ('_route_params ' ), $ request ->query ->all (), (array ) $ parameters );
89111
90- $ targetLocales = $ this ->allowedLocales ;
91- foreach ($ targetLocales as $ locale ) {
112+ foreach ($ this ->allowedLocalesProvider ->getAllowedLocales () as $ locale ) {
92113 $ strpos = 0 === strpos ($ request ->getLocale (), $ locale );
93114 if ($ this ->showCurrentLocale && $ strpos || !$ strpos ) {
94115 $ targetLocaleTargetLang = Intl::getLanguageBundle ()->getLanguageName ($ locale , null , $ locale );
@@ -98,7 +119,7 @@ public function getTargetInformations($targetRoute = null, $parameters = array()
98119 if (null !== $ targetRoute && "" !== $ targetRoute ) {
99120 $ switchRoute = $ router ->generate ($ targetRoute , $ parameters );
100121 } elseif ($ this ->useController ) {
101- $ switchRoute = $ router ->generate ('lunetics_locale_switcher ' , array ( '_locale ' => $ locale) );
122+ $ switchRoute = $ router ->generate ('lunetics_locale_switcher ' , [ '_locale ' => $ locale] );
102123 } elseif ($ route ) {
103124 $ switchRoute = $ router ->generate ($ route , $ parameters );
104125 } else {
@@ -118,12 +139,12 @@ public function getTargetInformations($targetRoute = null, $parameters = array()
118139 throw $ e ;
119140 }
120141
121- $ infos ['locales ' ][$ locale ] = array (
142+ $ infos ['locales ' ][$ locale ] = [
122143 'locale_current_language ' => $ targetLocaleCurrentLang ,
123144 'locale_target_language ' => $ targetLocaleTargetLang ,
124145 'link ' => $ switchRoute ,
125146 'locale ' => $ locale ,
126- ) ;
147+ ] ;
127148 }
128149 }
129150
0 commit comments