Skip to content

Commit fc55056

Browse files
committed
Move config stuff out of LocaleHandler
1 parent 609bde8 commit fc55056

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/LocaleHandler.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use CodeZero\Localizer\Localizer;
66
use Illuminate\Support\Facades\App;
7-
use Illuminate\Support\Facades\Config;
87

98
class LocaleHandler
109
{
@@ -22,23 +21,16 @@ class LocaleHandler
2221
*/
2322
protected $supportedLocales;
2423

25-
/**
26-
* Should the Localizer package be used?
27-
*
28-
* @var bool
29-
*/
30-
protected $shouldUseLocalizer;
31-
3224
/**
3325
* Create a new SetLocale instance.
3426
*
27+
* @param array $supportedLocales
3528
* @param \CodeZero\Localizer\Localizer $localizer
3629
*/
37-
public function __construct(Localizer $localizer)
30+
public function __construct(array $supportedLocales, Localizer $localizer = null)
3831
{
32+
$this->supportedLocales = $supportedLocales;
3933
$this->localizer = $localizer;
40-
$this->supportedLocales = Config::get('localized-routes.supported-locales', []);
41-
$this->shouldUseLocalizer = Config::get('localized-routes.use_localizer', false);
4234
}
4335

4436
/**
@@ -64,7 +56,7 @@ public function handleLocale($locale)
6456
*/
6557
protected function detectLocales()
6658
{
67-
if ( ! $this->shouldUseLocalizer) return false;
59+
if ( ! $this->localizer) return false;
6860

6961
$this->localizer->setSupportedLocales($this->supportedLocales);
7062

@@ -80,7 +72,7 @@ protected function detectLocales()
8072
*/
8173
protected function setLocale($locale)
8274
{
83-
$this->shouldUseLocalizer
75+
$this->localizer
8476
? $this->localizer->store($locale)
8577
: App::setLocale($locale);
8678
}

src/LocalizedRoutesServiceProvider.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use CodeZero\LocalizedRoutes\Macros\UriTranslationMacro;
66
use CodeZero\LocalizedRoutes\Macros\LocalizedRoutesMacro;
7+
use CodeZero\Localizer\Localizer;
78
use Illuminate\Support\ServiceProvider;
89

910
class LocalizedRoutesServiceProvider extends ServiceProvider
@@ -35,6 +36,7 @@ public function register()
3536
{
3637
$this->mergeConfig();
3738
$this->registerUrlGenerator();
39+
$this->registerLocaleHandler();
3840
}
3941

4042
/**
@@ -134,4 +136,20 @@ protected function requestRebinder()
134136
$app['url']->setRequest($request);
135137
};
136138
}
139+
140+
/**
141+
* Register the LocaleHandler..
142+
*
143+
* @return void
144+
*/
145+
protected function registerLocaleHandler()
146+
{
147+
$this->app->bind(LocaleHandler::class, function ($app) {
148+
$supportedLocales = $app['config']->get('localized-routes.supported-locales', []);
149+
$useLocalizer = $app['config']->get('localized-routes.use_localizer', false);
150+
$localizer = $useLocalizer ? $app->make(Localizer::class) : null;
151+
152+
return new LocaleHandler($supportedLocales, $localizer);
153+
});
154+
}
137155
}

0 commit comments

Comments
 (0)