-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
30b21e6
commit 2aef3b8
Showing
1 changed file
with
59 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
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(); | ||
} | ||
} |