Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Raven0us/dev
add the HostName handler
- Loading branch information
Showing
3 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]] | ||
: []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |