Skip to content

Add support for trusted detectors #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions config/localizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@
CodeZero\Localizer\Detectors\AppDetector::class,
],

/**
* Add any of the above detector class names here to make it trusted.
* When a trusted detector returns a locale, it will be used
* as the app locale, regardless if it's a supported locale or not.
*/
'trusted-detectors' => [
//
],

/**
* The stores to store the first matching locale in.
*/
Expand Down
37 changes: 34 additions & 3 deletions src/Localizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Localizer
protected $locales;

/**
* \CoderZero\Localizer\Detectors\Detector instances.
* \CoderZero\Localizer\Detectors\Detector class names or instances.
*
* @var \Illuminate\Support\Collection|array
*/
Expand All @@ -27,18 +27,27 @@ class Localizer
*/
protected $stores;

/**
* \CoderZero\Localizer\Detectors\Detector class names.
*
* @var \Illuminate\Support\Collection|array
*/
protected $trustedDetectors;

/**
* Create a new Localizer instance.
*
* @param \Illuminate\Support\Collection|array $locales
* @param \Illuminate\Support\Collection|array $detectors
* @param \Illuminate\Support\Collection|array $stores
* @param \Illuminate\Support\Collection|array $trustedDetectors
*/
public function __construct($locales, $detectors, $stores = [])
public function __construct($locales, $detectors, $stores = [], $trustedDetectors = [])
{
$this->setSupportedLocales($locales);
$this->detectors = $detectors;
$this->stores = $stores;
$this->trustedDetectors = $trustedDetectors;
}

/**
Expand All @@ -52,7 +61,7 @@ public function detect()
$locales = (array) $this->getInstance($detector)->detect();

foreach ($locales as $locale) {
if ($this->isSupportedLocale($locale)) {
if ($locale && ($this->isSupportedLocale($locale) || $this->isTrustedDetector($detector))) {
return $locale;
}
}
Expand Down Expand Up @@ -105,6 +114,28 @@ protected function isSupportedLocale($locale)
return in_array($locale, $this->locales);
}

/**
* Check if the given Detector class is trusted.
*
* @param \CodeZero\Localizer\Detectors\Detector|string $detector
*
* @return bool
*/
protected function isTrustedDetector($detector)
{
if (is_string($detector)) {
return in_array($detector, $this->trustedDetectors);
}

foreach ($this->trustedDetectors as $trustedDetector) {
if ($detector instanceof $trustedDetector) {
return true;
}
}

return false;
}

/**
* Get the class from Laravel's IOC container if it is a string.
*
Expand Down
3 changes: 2 additions & 1 deletion src/LocalizerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ protected function registerLocalizer()
$locales = $app['config']->get("{$this->name}.supported-locales");
$detectors = $app['config']->get("{$this->name}.detectors");
$stores = $app['config']->get("{$this->name}.stores");
$trustedDetectors = $app['config']->get("{$this->name}.trusted-detectors");

return new Localizer($locales, $detectors, $stores);
return new Localizer($locales, $detectors, $stores, $trustedDetectors);
});
}

Expand Down
25 changes: 25 additions & 0 deletions tests/Feature/SetLocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,31 @@ public function it_defaults_to_the_current_app_locale()
$this->assertEquals('en', $response->original);
}

/** @test */
public function trusted_detectors_ignore_supported_locales_and_may_set_any_locale()
{
$this->setSupportedLocales(['en']);
$this->setAppLocale('en');

$routeAction = ['locale' => 'nl'];

Config::set('localizer.trusted-detectors', [
\CodeZero\Localizer\Detectors\RouteActionDetector::class,
]);

Route::group($routeAction, function () {
Route::get('some/route', function () {
return App::getLocale();
})->middleware(['web', SetLocale::class]);
});

$response = $this->get('some/route');

$response->assertSessionHas($this->sessionKey, 'nl');
$response->assertCookie($this->cookieName, 'nl');
$this->assertEquals('nl', $response->original);
}

/**
* Set the current app locale.
*
Expand Down
36 changes: 36 additions & 0 deletions tests/Unit/LocalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,42 @@ public function it_returns_the_best_match_if_an_array_of_locales_is_detected()
$this->assertEquals('nl', $localizer->detect());
}

/** @test */
public function trusted_detectors_ignore_supported_locales_and_may_set_any_locale()
{
$supportedLocales = ['en'];
$detectors = [
Mockery::mock(Detector::class)->allows()->detect()->andReturns('nl')->getMock(),
];
$trustedDetectors = [
Detector::class,
];

$localizer = new Localizer($supportedLocales, $detectors, [], $trustedDetectors);

$this->assertEquals('nl', $localizer->detect());
}

/** @test */
public function empty_locales_from_trusted_detectors_are_ignored()
{
$supportedLocales = ['en'];
$detectors = [
Mockery::mock(Detector::class)->allows()->detect()->andReturns(false)->getMock(),
Mockery::mock(Detector::class)->allows()->detect()->andReturns(null)->getMock(),
Mockery::mock(Detector::class)->allows()->detect()->andReturns([])->getMock(),
Mockery::mock(Detector::class)->allows()->detect()->andReturns('')->getMock(),
Mockery::mock(Detector::class)->allows()->detect()->andReturns('en')->getMock(),
];
$trustedDetectors = [
Detector::class,
];

$localizer = new Localizer($supportedLocales, $detectors, [], $trustedDetectors);

$this->assertEquals('en', $localizer->detect());
}

/** @test */
public function it_returns_false_if_no_supported_locale_could_be_detected()
{
Expand Down