Skip to content

Commit 464f9e1

Browse files
committed
Extract logic from middleware
1 parent 1b441c3 commit 464f9e1

File tree

2 files changed

+89
-54
lines changed

2 files changed

+89
-54
lines changed

src/LocaleHandler.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace CodeZero\LocalizedRoutes;
4+
5+
use CodeZero\Localizer\Localizer;
6+
use Illuminate\Support\Facades\App;
7+
use Illuminate\Support\Facades\Config;
8+
9+
class LocaleHandler
10+
{
11+
/**
12+
* Localizer.
13+
*
14+
* @var \CodeZero\Localizer\Localizer
15+
*/
16+
protected $localizer;
17+
18+
/**
19+
* Supported locales.
20+
*
21+
* @var array
22+
*/
23+
protected $supportedLocales;
24+
25+
/**
26+
* Should the Localizer package be used?
27+
*
28+
* @var bool
29+
*/
30+
protected $shouldUseLocalizer;
31+
32+
/**
33+
* Create a new SetLocale instance.
34+
*
35+
* @param \CodeZero\Localizer\Localizer $localizer
36+
*/
37+
public function __construct(Localizer $localizer)
38+
{
39+
$this->localizer = $localizer;
40+
$this->supportedLocales = Config::get('localized-routes.supported-locales', []);
41+
$this->shouldUseLocalizer = Config::get('localized-routes.use_localizer', false);
42+
}
43+
44+
/**
45+
* Detect and/or set the locale.
46+
*
47+
* @param string|null $locale
48+
*
49+
* @return void
50+
*/
51+
public function handleLocale($locale)
52+
{
53+
$locale = $locale ?: $this->detectLocales();
54+
55+
if ($locale) {
56+
$this->setLocale($locale);
57+
}
58+
}
59+
60+
/**
61+
* Detect locales.
62+
*
63+
* @return string|false
64+
*/
65+
protected function detectLocales()
66+
{
67+
if ( ! $this->shouldUseLocalizer) return false;
68+
69+
$this->localizer->setSupportedLocales($this->supportedLocales);
70+
71+
return $this->localizer->detect();
72+
}
73+
74+
/**
75+
* Set the locale.
76+
*
77+
* @param string $locale
78+
*
79+
* @return void
80+
*/
81+
protected function setLocale($locale)
82+
{
83+
$this->shouldUseLocalizer
84+
? $this->localizer->store($locale)
85+
: App::setLocale($locale);
86+
}
87+
}

src/Middleware/SetLocale.php

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,11 @@
33
namespace CodeZero\LocalizedRoutes\Middleware;
44

55
use Closure;
6-
use CodeZero\Localizer\Localizer;
6+
use CodeZero\LocalizedRoutes\LocaleHandler;
77
use Illuminate\Support\Facades\App;
8-
use Illuminate\Support\Facades\Config;
98

109
class SetLocale
1110
{
12-
/**
13-
* Localizer.
14-
*
15-
* @var \CodeZero\Localizer\Localizer
16-
*/
17-
protected $localizer;
18-
19-
/**
20-
* Create a new SetLocale instance.
21-
*
22-
* @param \CodeZero\Localizer\Localizer $localizer
23-
*/
24-
public function __construct(Localizer $localizer)
25-
{
26-
$this->localizer = $localizer;
27-
}
28-
2911
/**
3012
* Handle an incoming request.
3113
*
@@ -40,42 +22,8 @@ public function handle($request, Closure $next)
4022
// If it is present, use this as the locale.
4123
$locale = $request->route()->getAction('localized-routes-locale');
4224

43-
if ( ! $locale && $this->shouldUseLocalizer()) {
44-
$locale = $this->detectLocales();
45-
}
46-
47-
if ($locale && $this->shouldUseLocalizer()) {
48-
$this->localizer->store($locale);
49-
}
50-
51-
if ($locale && ! $this->shouldUseLocalizer()) {
52-
App::setLocale($locale);
53-
}
25+
App::make(LocaleHandler::class)->handleLocale($locale);
5426

5527
return $next($request);
5628
}
57-
58-
/**
59-
* Detect locales.
60-
*
61-
* @return string|false
62-
*/
63-
public function detectLocales()
64-
{
65-
$supportedLocales = Config::get('localized-routes.supported-locales', []);
66-
67-
$this->localizer->setSupportedLocales($supportedLocales);
68-
69-
return $this->localizer->detect();
70-
}
71-
72-
/**
73-
* Check if the 'use_localizer' option is enabled.
74-
*
75-
* @return bool
76-
*/
77-
protected function shouldUseLocalizer()
78-
{
79-
return Config::get('localized-routes.use_localizer', false);
80-
}
8129
}

0 commit comments

Comments
 (0)