Skip to content

Commit 5b6ade0

Browse files
committed
Extract configuration from LocalizedUrlGenerator (#81)
1 parent 5d2d036 commit 5b6ade0

File tree

3 files changed

+161
-188
lines changed

3 files changed

+161
-188
lines changed

src/LocaleConfig.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,74 @@ public function getLocales()
149149
return array_keys($locales);
150150
}
151151

152+
/**
153+
* Find the slug that belongs to the given locale.
154+
*
155+
* @param string $locale
156+
*
157+
* @return string|null
158+
*/
159+
public function findSlugByLocale($locale)
160+
{
161+
if ( ! $this->isSupportedLocale($locale) || $this->hasCustomDomains()) {
162+
return null;
163+
}
164+
165+
return $this->getSupportedLocales()[$locale] ?? $locale;
166+
}
167+
168+
/**
169+
* Find the domain that belongs to the given locale.
170+
*
171+
* @param string $locale
172+
*
173+
* @return string|null
174+
*/
175+
public function findDomainByLocale($locale)
176+
{
177+
if ( ! $this->isSupportedLocale($locale) || ! $this->hasCustomDomains()) {
178+
return null;
179+
}
180+
181+
return $this->getSupportedLocales()[$locale];
182+
}
183+
184+
/**
185+
* Find the locale that belongs to the given slug.
186+
*
187+
* @param string $slug
188+
*
189+
* @return string|null
190+
*/
191+
public function findLocaleBySlug($slug)
192+
{
193+
if ($this->hasCustomDomains()) {
194+
return null;
195+
}
196+
197+
if ($this->hasSimpleLocales() && $this->isSupportedLocale($slug)) {
198+
return $slug;
199+
}
200+
201+
return array_search($slug, $this->getSupportedLocales()) ?: null;
202+
}
203+
204+
/**
205+
* Find the locale that belongs to the given domain.
206+
*
207+
* @param string $domain
208+
*
209+
* @return string|null
210+
*/
211+
public function findLocaleByDomain($domain)
212+
{
213+
if ( ! $this->hasCustomDomains()) {
214+
return null;
215+
}
216+
217+
return array_search($domain, $this->getSupportedLocales()) ?: null;
218+
}
219+
152220
/**
153221
* Check if there are any locales configured.
154222
*

src/LocalizedUrlGenerator.php

Lines changed: 19 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
namespace CodeZero\LocalizedRoutes;
44

5-
use Illuminate\Support\Str;
5+
use CodeZero\LocalizedRoutes\Facades\LocaleConfig;
66
use InvalidArgumentException;
77
use Illuminate\Support\Collection;
88
use Illuminate\Support\Facades\App;
99
use Illuminate\Support\Facades\Route;
10-
use Illuminate\Support\Facades\Config;
1110
use Illuminate\Support\Facades\Request;
1211
use 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

Comments
 (0)