Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
malder1975 committed Oct 14, 2016
1 parent 30b21e6 commit 2aef3b8
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions common/components/staticpages/PagesUrlRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php


namespace common\components\staticpages;

use Yii;
use common\models\StaticPages;
use yii\caching\DbDependency;
use yii\web\CompositeUrlRule;
use yii\web\UrlRuleInterface;
use yii\base\InvalidConfigException;

/**
* Description of PagesUrlRule
*
* @author VGRovnov
*/
class PagesUrlRule extends CompositeUrlRule {

public $cacheComponent = 'cache';
public $cacheID = 'PagesUrlRules';
public $ruleConfig = ['class' => 'yii\web\UrlRule'];

protected function createRules()
{
$cache = \Yii::$app->get($this->cacheComponent)->get($this->cacheID);
if (!empty($cache))
{
return $cache;
}

$pages = StaticPages::find()->asArray()->all();

$rules = [];
foreach ($pages as $page) {
$rule = [
'pattern' => ltrim($page['alias'], '/'),
'route' => ltrim($page['route'], '/'),
];

$rule = \Yii::createObject(array_merge($this->ruleConfig, $rule));
if (!$rule instanceof UrlRuleInterface) {
throw new InvalidConfigException('Класс URL-Rule должен реализовать UrlRuleInterface.');
}
$rules[] = $rule;
}

$cd = new DbDependency();
$cd->sql = 'SELECT MAX(id) FROM ' .StaticPages::tableName();

Yii::$app->get($this->cacheComponent)->set($this->cacheID, $rules, 60,$cd);

return $rules;
}
public function __wakeup()
{
$this->init();
}
}

0 comments on commit 2aef3b8

Please sign in to comment.