Skip to content

Commit

Permalink
Merge pull request #4 from Raven0us/master
Browse files Browse the repository at this point in the history
push language handler at the beginning of the beforeAction handler list
  • Loading branch information
cetver committed Jul 21, 2018
2 parents 7c2efb9 + 3359098 commit 54d397d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
15 changes: 14 additions & 1 deletion Component.php
Expand Up @@ -30,6 +30,13 @@ class Component extends \yii\base\Component implements BootstrapInterface
*/
public $handlers = [];

/**
* @var bool append the language handler to the existing handler list or push at the beginning
* useful if you rely on having language available before reaching RBAC or other logic that involves beforeAction
* @link https://www.yiiframework.com/doc/guide/2.0/en/concept-events#event-handler-order
*/
public $appendSetLanguageHandler = true;

/**
* Bootstrap method to be called during application bootstrap stage.
*
Expand All @@ -48,6 +55,12 @@ public function bootstrap($app)
'The "languages" property must be an array or callable function that returns an array'
);
}

if (!is_bool($this->appendSetLanguageHandler)) {
throw new InvalidConfigException(
'The "appendSetLanguageHandler" property must be a boolean'
);
}
foreach ($this->handlers as &$handler) {
if (is_object($handler) === false) {
$handler = Yii::createObject($handler);
Expand All @@ -59,7 +72,7 @@ public function bootstrap($app)
));
}
}
$app->on($app::EVENT_BEFORE_ACTION, [$this, 'setLanguage'], $app);
$app->on($app::EVENT_BEFORE_ACTION, [$this, 'setLanguage'], $app, $this->appendSetLanguageHandler);
}
}

Expand Down
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -39,6 +39,8 @@ return [
'languagesDispatcher' => [
'class' => 'cetver\LanguagesDispatcher\Component',
'languages' => ['en', 'ru'],
// useful if you want to push the language handler at the beginning of beforeAction event handlers list
'appendSetLanguageHandler' => false, // defaults to true
/*
or
'languages' => function () {
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/ComponentTest.php
Expand Up @@ -92,6 +92,22 @@ function () {

$this->mockWebApplication();
$this->tester->assertTrue(Yii::$app->hasEventHandlers(Application::EVENT_BEFORE_ACTION));

$this->tester->expectException(
new InvalidConfigException(
'The "appendSetLanguageHandler" property must be a boolean'
),
function () {
$this->mockWebApplication([
'components' => [
'ld' => [
'languages' => $this->languages,
'appendSetLanguageHandler' => 'non-sense'
],
],
]);
}
);
}

public function testSetLanguage()
Expand Down Expand Up @@ -158,5 +174,19 @@ public function testSetLanguage()
],
]);
$this->tester->assertTrue(Yii::$app->hasEventHandlers(Component::EVENT_AFTER_SETTING_LANGUAGE));

$this->mockWebApplication([
'components' => [
'ld' => [
'languages' => $this->languages,
'appendSetLanguageHandler' => false,
'handlers' => [
'cetver\LanguagesDispatcher\handlers\SessionHandler',
]
],
],
]);
Yii::$app->trigger(Application::EVENT_BEFORE_ACTION);
$this->tester->assertTrue(Yii::$app->hasEventHandlers(Component::EVENT_AFTER_SETTING_LANGUAGE));
}
}

0 comments on commit 54d397d

Please sign in to comment.