Skip to content

Commit 5d2d036

Browse files
committed
Centralize configuration queries to a dedicated class (#79)
1 parent d6a6cbc commit 5d2d036

File tree

7 files changed

+447
-70
lines changed

7 files changed

+447
-70
lines changed

src/Facades/LocaleConfig.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace CodeZero\LocalizedRoutes\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
/**
8+
* LocaleConfig Facade
9+
*
10+
* @mixin \CodeZero\LocalizedRoutes\LocaleConfig
11+
*/
12+
class LocaleConfig extends Facade
13+
{
14+
/**
15+
* Get the registered name of the component.
16+
*
17+
* @return string
18+
*/
19+
protected static function getFacadeAccessor()
20+
{
21+
return 'locale-config';
22+
}
23+
}

src/Illuminate/Routing/UrlGenerator.php

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace CodeZero\LocalizedRoutes\Illuminate\Routing;
44

5+
use CodeZero\LocalizedRoutes\Facades\LocaleConfig;
56
use Illuminate\Routing\UrlGenerator as BaseUrlGenerator;
67
use Illuminate\Support\Facades\App;
7-
use Illuminate\Support\Facades\Config;
88
use Illuminate\Support\Facades\Route;
99

1010
class UrlGenerator extends BaseUrlGenerator
@@ -99,8 +99,8 @@ protected function resolveLocalizedRouteName($name, $locale, $currentLocale)
9999

100100
// If the locale is not supported, use a fallback
101101
// locale if one is configured.
102-
if ( ! $this->isSupportedLocale($locale)) {
103-
$locale = $this->getFallbackLocale() ?: $locale;
102+
if ( ! LocaleConfig::isSupportedLocale($locale)) {
103+
$locale = LocaleConfig::getFallbackLocale() ?: $locale;
104104
}
105105

106106
// Normalize the route name by removing any locale prefix.
@@ -140,7 +140,7 @@ protected function stripLocaleFromRouteName($name)
140140

141141
// If the first part of the route name is a valid
142142
// locale, then remove it from the array.
143-
if ($this->isSupportedLocale($parts[0])) {
143+
if (LocaleConfig::isSupportedLocale($parts[0])) {
144144
array_shift($parts);
145145
}
146146

@@ -149,42 +149,4 @@ protected function stripLocaleFromRouteName($name)
149149

150150
return $name;
151151
}
152-
153-
/**
154-
* Check if the given locale is supported.
155-
*
156-
* @param string $locale
157-
*
158-
* @return bool
159-
*/
160-
protected function isSupportedLocale($locale)
161-
{
162-
return in_array($locale, $this->getSupportedLocales());
163-
}
164-
165-
/**
166-
* Get the supported locales and not the custom slugs or domains.
167-
*
168-
* @return array
169-
*/
170-
protected function getSupportedLocales()
171-
{
172-
$locales = Config::get('localized-routes.supported_locales', []);
173-
174-
if (is_numeric(key($locales))) {
175-
return $locales;
176-
}
177-
178-
return array_keys($locales);
179-
}
180-
181-
/**
182-
* Get the fallback locale.
183-
*
184-
* @return string|null
185-
*/
186-
protected function getFallbackLocale()
187-
{
188-
return Config::get('localized-routes.fallback_locale');
189-
}
190152
}

src/LocaleConfig.php

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
<?php
2+
3+
namespace CodeZero\LocalizedRoutes;
4+
5+
class LocaleConfig
6+
{
7+
/**
8+
* The configured supported locales.
9+
*
10+
* @var array
11+
*/
12+
protected $supportedLocales;
13+
14+
/**
15+
* The configured omitted locale.
16+
*
17+
* @var string|null
18+
*/
19+
protected $omittedLocale;
20+
21+
/**
22+
* The configured fallback locale.
23+
*
24+
* @var string|null
25+
*/
26+
protected $fallbackLocale;
27+
28+
/**
29+
* The configured route action that holds a route's locale.
30+
*
31+
* @var string
32+
*/
33+
protected $routeAction;
34+
35+
/**
36+
* Create a new LocaleConfig instance.
37+
*
38+
* @param array $config
39+
*/
40+
public function __construct($config = [])
41+
{
42+
$this->supportedLocales = $config['supported_locales'] ?? [];
43+
$this->omittedLocale = $config['omitted_locale'] ?? null;
44+
$this->fallbackLocale = $config['fallback_locale'] ?? null;
45+
$this->routeAction = $config['route_action'] ?? null;
46+
}
47+
48+
/**
49+
* Get the configured supported locales.
50+
*
51+
* @return array
52+
*/
53+
public function getSupportedLocales()
54+
{
55+
return $this->supportedLocales;
56+
}
57+
58+
/**
59+
* Set the supported locales.
60+
*
61+
* @param array $locales
62+
*
63+
* @return void
64+
*/
65+
public function setSupportedLocales($locales)
66+
{
67+
$this->supportedLocales = $locales;
68+
}
69+
70+
/**
71+
* Get the locale that should be omitted in the URI path.
72+
*
73+
* @return string|null
74+
*/
75+
public function getOmittedLocale()
76+
{
77+
return $this->omittedLocale;
78+
}
79+
80+
/**
81+
* Set the locale that should be omitted in the URI path.
82+
*
83+
* @param string|null $locale
84+
*
85+
* @return void
86+
*/
87+
public function setOmittedLocale($locale)
88+
{
89+
$this->omittedLocale = $locale;
90+
}
91+
92+
/**
93+
* Get the fallback locale.
94+
*
95+
* @return string|null
96+
*/
97+
public function getFallbackLocale()
98+
{
99+
return $this->fallbackLocale;
100+
}
101+
102+
/**
103+
* Set the fallback locale.
104+
*
105+
* @param string|null $locale
106+
*
107+
* @return void
108+
*/
109+
public function setFallbackLocale($locale)
110+
{
111+
$this->fallbackLocale = $locale;
112+
}
113+
114+
/**
115+
* Get the route action that holds a route's locale.
116+
*
117+
* @return string
118+
*/
119+
public function getRouteAction()
120+
{
121+
return $this->routeAction;
122+
}
123+
124+
/**
125+
* Set the route action that holds a route's locale.
126+
*
127+
* @param string $locale
128+
*
129+
* @return string
130+
*/
131+
public function setRouteAction($locale)
132+
{
133+
return $this->routeAction = $locale;
134+
}
135+
136+
/**
137+
* Get the locales (not the slugs or domains).
138+
*
139+
* @return array
140+
*/
141+
public function getLocales()
142+
{
143+
$locales = $this->getSupportedLocales();
144+
145+
if ($this->hasSimpleLocales()) {
146+
return $locales;
147+
}
148+
149+
return array_keys($locales);
150+
}
151+
152+
/**
153+
* Check if there are any locales configured.
154+
*
155+
* @return bool
156+
*/
157+
public function hasLocales()
158+
{
159+
return count($this->getSupportedLocales()) > 0;
160+
}
161+
162+
/**
163+
* Check if there are only locales configured,
164+
* and not custom slugs or domains.
165+
*
166+
* @return bool
167+
*/
168+
public function hasSimpleLocales()
169+
{
170+
return is_numeric(key($this->getSupportedLocales()));
171+
}
172+
173+
/**
174+
* Check if custom slugs are configured.
175+
*
176+
* @return bool
177+
*/
178+
public function hasCustomSlugs()
179+
{
180+
return $this->hasLocales() && ! $this->hasSimpleLocales() && ! $this->hasCustomDomains();
181+
}
182+
183+
/**
184+
* Check if custom domains are configured.
185+
*
186+
* @return bool
187+
*/
188+
public function hasCustomDomains()
189+
{
190+
$firstValue = array_values($this->getSupportedLocales())[0] ?? '';
191+
$containsDot = strpos($firstValue, '.') !== false;
192+
193+
return $containsDot;
194+
}
195+
196+
/**
197+
* Check if the given locale is supported.
198+
*
199+
* @param string $locale
200+
*
201+
* @return bool
202+
*/
203+
public function isSupportedLocale($locale)
204+
{
205+
return in_array($locale, $this->getLocales());
206+
}
207+
}

0 commit comments

Comments
 (0)