Skip to content

Commit

Permalink
Merge pull request #2 from Raven0us/dev
Browse files Browse the repository at this point in the history
add the HostName handler
  • Loading branch information
cetver committed Jul 1, 2018
2 parents ba943ec + e266463 commit b780753
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -47,6 +47,16 @@ return [
*/
// Order is important
'handlers' => [
[
// Detects a language based on host name
'class' => 'cetver\LanguagesDispatcher\handlers\HostNameHandler',
'request' => 'request', // optional, the Request component ID.
// hostMap can be either an array or a callable that returns an array
'hostMap' => [
'ru.example.com' => 'ru',
'uk.example.com' => 'uk'
]
],
[
// Detects a language from the query parameter.
'class' => 'cetver\LanguagesDispatcher\handlers\QueryParamHandler',
Expand Down
62 changes: 62 additions & 0 deletions handlers/HostNameHandler.php
@@ -0,0 +1,62 @@
<?php

namespace cetver\LanguagesDispatcher\handlers;

use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\web\Request;

/**
* Class HostNameHandler handles languages based on the hostname of the request.
*/
class HostNameHandler extends AbstractHandler
{
/**
* @var string|Request the Request component ID.
*/
public $request = 'request';

/** @var array|callable An array that maps hostnames to languages or a callable function that returns it.
*/
public $hostMap = [];

/**
* @inheritdoc
*/
function init()
{
parent::init();

$request = Yii::$app->get($this->request, false);
if (!$request instanceof Request) {
throw new InvalidConfigException(sprintf(
'The component with the specified ID "%s" must be an instance of "%s"',
$this->request,
get_class(new Request())
));
}

$this->request = $request;

if (is_callable($this->hostMap)) {
$this->hostMap = call_user_func($this->hostMap);
}

if (!is_array($this->hostMap)) {
throw new InvalidConfigException(
'The "hostMap" property must be an array or callable function that returns an array'
);
}
}

/**
* @inheritdoc
*/
public function getLanguages()
{
return (isset($this->hostMap[$this->request->hostName]))
? [$this->hostMap[$this->request->hostName]]
: [];
}
}
89 changes: 89 additions & 0 deletions tests/unit/handlers/HostNameHandlerTest.php
@@ -0,0 +1,89 @@
<?php

namespace cetver\LanguagesDispatcher\tests\unit\handlers;

use cetver\LanguagesDispatcher\handlers\HostNameHandler;
use cetver\LanguagesDispatcher\tests\AbstractUnitTest;
use yii\base\InvalidConfigException;
use yii\web\Request;

/**
* Class HostHandlerTest
*
* @package cetver\LanguagesDispatcher\tests\unit\handlers
*/
class HostNameHandlerTest extends AbstractUnitTest
{
public function testInit()
{
$request = 'invalid-request';
$invalidConfigExceptionClassName = get_class(new InvalidConfigException());

$this->tester->expectException($invalidConfigExceptionClassName, function () use ($request) {
$this->mockWebApplication();
new HostNameHandler([
'request' => $request,
]);
});

$this->tester->expectException($invalidConfigExceptionClassName, function () {
$this->mockWebApplication();
new HostNameHandler([
'hostMap' => 'non-array'
]);
});

$this->tester->expectException($invalidConfigExceptionClassName, function () {
$this->mockWebApplication();
new HostNameHandler([
'hostMap' => function () {
return false;
}
]);
});

$handler = new HostNameHandler([
'hostMap' => function () {
return [
'ru.example.com' => 'ru'
];
}
]);
$this->tester->assertArrayHasKey('ru.example.com', $handler->hostMap);

$handler = new HostNameHandler();
$this->tester->assertInstanceOf(get_class(new Request()), $handler->request);
}

public function testGetLanguages()
{
$hostMap = [
'ru.example.com' => 'ru',
'cn.example.com' => 'cn'
];

$this->mockWebApplication();
$handler = new HostNameHandler([
'hostMap' => $hostMap
]);

$handler->request->setHostInfo('http://uk.example.com');
$this->tester->assertSame([], $handler->getLanguages());

$handler->request->setHostInfo('https://ru.example.com');
$this->tester->assertSame(['ru'], $handler->getLanguages());

$this->mockWebApplication();
$handler = new HostNameHandler([
'hostMap' => function () use ($hostMap) {
return $hostMap;
}
]);

$handler->request->setHostInfo('http://ro.example.com');
$this->tester->assertSame([], $handler->getLanguages());

$handler->request->setHostInfo('https://cn.example.com');
$this->tester->assertSame(['cn'], $handler->getLanguages());
}
}

0 comments on commit b780753

Please sign in to comment.