22
33namespace CodeZero \LocalizedRoutes ;
44
5- use Illuminate \ Support \ Str ;
5+ use CodeZero \ LocalizedRoutes \ Facades \ LocaleConfig ;
66use InvalidArgumentException ;
77use Illuminate \Support \Collection ;
88use Illuminate \Support \Facades \App ;
99use Illuminate \Support \Facades \Route ;
10- use Illuminate \Support \Facades \Config ;
1110use Illuminate \Support \Facades \Request ;
1211use Illuminate \Contracts \Routing \UrlRoutable ;
1312
@@ -20,20 +19,12 @@ class LocalizedUrlGenerator
2019 */
2120 protected $ route ;
2221
23- /**
24- * Supported locales config.
25- *
26- * @var array
27- */
28- protected $ supportedLocales ;
29-
3022 /**
3123 * Create a new LocalizedUrlGenerator instance.
3224 */
3325 public function __construct ()
3426 {
3527 $ this ->route = Route::current ();
36- $ this ->supportedLocales = $ this ->getSupportedLocales ();
3728 }
3829
3930 /**
@@ -48,7 +39,14 @@ public function __construct()
4839 public function generateFromRequest ($ locale = null , $ parameters = null , $ absolute = true , $ keepQuery = true )
4940 {
5041 $ urlBuilder = UrlBuilder::make (Request::fullUrl ());
51- $ locale = $ locale ?: $ this ->detectLocale ($ urlBuilder );
42+
43+ $ domain = $ urlBuilder ->getHost ();
44+ $ localeSlug = $ urlBuilder ->getSlugs ()[0 ] ?? null ;
45+
46+ $ locale = $ locale
47+ ?? LocaleConfig::findLocaleBySlug ($ localeSlug )
48+ ?? LocaleConfig::findLocaleByDomain ($ domain )
49+ ?? App::getLocale ();
5250
5351 if ( ! $ this ->is404 ()) {
5452 // $parameters can be an array, a function or it can contain model instances!
@@ -77,11 +75,11 @@ public function generateFromRequest($locale = null, $parameters = null, $absolut
7775
7876 // If custom domains are not used and it is not a registered,
7977 // non localized route, update the locale slug in the path.
80- if ( ! $ this -> hasCustomDomains () && ($ this ->is404 () || $ this ->isLocalized ())) {
78+ if ( ! LocaleConfig:: hasCustomDomains () && ($ this ->is404 () || $ this ->isLocalized ())) {
8179 $ urlBuilder ->setSlugs ($ this ->updateLocaleInSlugs ($ urlBuilder ->getSlugs (), $ locale ));
8280 }
8381
84- if ($ domain = $ this -> getCustomDomain ($ locale )) {
82+ if ($ domain = LocaleConfig:: findDomainByLocale ($ locale )) {
8583 $ urlBuilder ->setHost ($ domain );
8684 }
8785
@@ -117,13 +115,14 @@ protected function generateFromNamedRoute($locale, $parameters, $absolute)
117115 */
118116 public function isLocalized ()
119117 {
120- $ routeAction = Config:: get ( ' localized-routes.route_action ' );
118+ $ routeAction = LocaleConfig:: getRouteAction ( );
121119
122120 return $ this ->routeExists () && $ this ->route ->getAction ($ routeAction ) !== null ;
123121 }
124122
125123 /**
126124 * Check if the current request is a 404.
125+ * Default 404 requests will not have a Route.
127126 *
128127 * @return bool
129128 */
@@ -144,7 +143,6 @@ protected function isFallback()
144143
145144 /**
146145 * Check if the current Route exists.
147- * Default 404 requests will not have a Route.
148146 *
149147 * @return bool
150148 */
@@ -153,87 +151,6 @@ protected function routeExists()
153151 return $ this ->route !== null ;
154152 }
155153
156- /**
157- * Check if custom domains are configured.
158- *
159- * @return bool
160- */
161- protected function hasCustomDomains ()
162- {
163- if (count ($ this ->supportedLocales ) === 0 ) {
164- return false ;
165- }
166-
167- $ firstValue = array_values ($ this ->supportedLocales )[0 ];
168- $ usingDomains = Str::contains ($ firstValue , '. ' );
169-
170- return $ usingDomains ;
171- }
172-
173- /**
174- * Check if custom slugs are configured.
175- *
176- * @return bool
177- */
178- protected function hasCustomSlugs ()
179- {
180- return ! $ this ->hasCustomDomains () && ! is_numeric (key ($ this ->supportedLocales ));
181- }
182-
183- /**
184- * Get the domain for the given locale if one is configured.
185- *
186- * @param string $locale
187- *
188- * @return string|null
189- */
190- protected function getCustomDomain ($ locale )
191- {
192- if ( ! $ this ->hasCustomDomains ()) {
193- return null ;
194- }
195-
196- return $ this ->supportedLocales [$ locale ] ?? null ;
197- }
198-
199- /**
200- * Find a slug for the given locale.
201- *
202- * @param string $locale
203- *
204- * @return string|null
205- */
206- protected function findSlugByLocale ($ locale )
207- {
208- if ($ this ->hasCustomDomains ()) {
209- return null ;
210- }
211-
212- return $ this ->supportedLocales [$ locale ] ?? $ locale ;
213- }
214-
215- /**
216- * Get the custom domains if configured.
217- *
218- * @return array
219- */
220- protected function getCustomDomains ()
221- {
222- return $ this ->hasCustomDomains () ? $ this ->supportedLocales : [];
223- }
224-
225- /**
226- * Find the locale that belongs to a custom domain.
227- *
228- * @param string $domain
229- *
230- * @return false|string
231- */
232- protected function findLocaleByDomain ($ domain )
233- {
234- return array_search ($ domain , $ this ->getCustomDomains ());
235- }
236-
237154 /**
238155 * Get the locale from the slugs if it exists.
239156 *
@@ -245,77 +162,11 @@ protected function getLocaleFromSlugs(array $slugs)
245162 {
246163 $ locale = $ slugs [0 ] ?? null ;
247164
248- if ($ this -> hasCustomSlugs ()) {
249- $ locale = array_flip ( $ this -> supportedLocales )[ $ locale] ?? null ;
165+ if (LocaleConfig:: hasCustomSlugs ()) {
166+ $ locale = LocaleConfig:: findLocaleBySlug ( $ locale) ;
250167 }
251168
252- return ($ locale && $ this ->localeIsSupported ($ locale )) ? $ locale : null ;
253- }
254-
255- /**
256- * Replace the locale in the slugs or prepend it if no locale exists yet.
257- *
258- * @param array $slugs
259- * @param string $locale
260- *
261- * @return array
262- */
263- protected function setLocaleInSlugs (array $ slugs , $ locale )
264- {
265- $ slugs [0 ] = $ locale ;
266-
267- return $ slugs ;
268- }
269-
270- /**
271- * Get the locale keys from the supported locales configuration.
272- *
273- * @return array
274- */
275- protected function getLocaleKeys ()
276- {
277- return is_numeric (key ($ this ->supportedLocales ))
278- ? $ this ->supportedLocales
279- : array_keys ($ this ->supportedLocales );
280- }
281-
282- /**
283- * Check if the given locale is supported.
284- *
285- * @param string $locale
286- *
287- * @return bool
288- */
289- protected function localeIsSupported ($ locale )
290- {
291- return in_array ($ locale , $ this ->getLocaleKeys ());
292- }
293-
294- /**
295- * Check if the given locale should be omitted from the URL.
296- *
297- * @param string $locale
298- *
299- * @return bool
300- */
301- protected function localeShouldBeOmitted ($ locale )
302- {
303- return $ locale === $ this ->getOmitLocale ();
304- }
305-
306- /**
307- * Detect the locale.
308- *
309- * @param \CodeZero\LocalizedRoutes\UrlBuilder $url
310- *
311- * @return string
312- */
313- protected function detectLocale (UrlBuilder $ url )
314- {
315- $ locale = $ this ->findLocaleByDomain ($ url ->getHost ())
316- ?: $ this ->getLocaleFromSlugs ($ url ->getSlugs ());
317-
318- return $ locale ?: App::getLocale ();
169+ return ($ locale && LocaleConfig::isSupportedLocale ($ locale )) ? $ locale : null ;
319170 }
320171
321172 /**
@@ -328,15 +179,15 @@ protected function detectLocale(UrlBuilder $url)
328179 */
329180 protected function updateLocaleInSlugs (array $ slugs , $ locale )
330181 {
331- $ slug = $ this -> findSlugByLocale ($ locale );
182+ $ slug = LocaleConfig:: findSlugByLocale ($ locale );
332183
333184 if ($ this ->getLocaleFromSlugs ($ slugs )) {
334- $ slugs = $ this -> setLocaleInSlugs ( $ slugs , $ slug) ;
185+ $ slugs[ 0 ] = $ slug ;
335186 } else {
336187 array_unshift ($ slugs , $ slug );
337188 }
338189
339- if ($ this -> localeShouldBeOmitted ( $ locale )) {
190+ if ($ locale === LocaleConfig:: getOmittedLocale ( )) {
340191 array_shift ($ slugs );
341192 }
342193
@@ -485,24 +336,4 @@ protected function getBindingFieldFor($key, UrlRoutable $model)
485336
486337 return $ this ->route ->bindingFieldFor ($ key ) ?: $ model ->getRouteKeyName ();
487338 }
488-
489- /**
490- * Get the locale that should be omitted in the URI path.
491- *
492- * @return string|null
493- */
494- protected function getOmitLocale ()
495- {
496- return Config::get ('localized-routes.omitted_locale ' , null );
497- }
498-
499- /**
500- * Get the supported locales and not the custom domains.
501- *
502- * @return array
503- */
504- protected function getSupportedLocales ()
505- {
506- return Config::get ('localized-routes.supported_locales ' , []);
507- }
508339}
0 commit comments