Skip to content

Commit

Permalink
Add GlobalPageCacheBehaviour class
Browse files Browse the repository at this point in the history
  • Loading branch information
saiat3 committed Apr 25, 2019
1 parent 73fb5af commit c097dd5
Showing 1 changed file with 136 additions and 0 deletions.
136 changes: 136 additions & 0 deletions behaviours/GlobalPageCacheBehaviour.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

namespace apollo11\yii2GlobalBehaviours\behaviours;

use apollo11\yii2GlobalBehaviours\exceptions\GlobalPageCacheException;
use yii\base\Behavior;
use yii\base\Controller;
use Yii;

/**
* Class GlobalCacheBehavior
* @package common\behaviors
*/
class GlobalPageCacheBehaviour extends Behavior
{

const VARIATION_BY_LANGUAGE = 'variation_by_language';
const VARIATION_BY_URL = 'variation_by_url';

/**
* @var array
*/
public $rules = [];

/**
* @var array
*/
public $variations = [];

/**
* @var integer
*/
public $duration = 60;

/**
* @return array
* @author Saiat Kalbiev <[email protected]>
*/
public function events()
{
return [
Controller::EVENT_BEFORE_ACTION => 'beforeAction'
];
}

/**
* @author Saiat Kalbiev <[email protected]>
* @throws GlobalPageCacheException
*/
public function beforeAction()
{
if (!$this->rules) {
throw new GlobalPageCacheException('Invalid rules provided');
}

$currentControllerId = Yii::$app->controller->id;
$currentActionId = Yii::$app->controller->action->id;

foreach ($this->rules as $rule) {

if (!isset($rule['controller']) || !$rule['controller']) {
throw new GlobalPageCacheException('Invalid rule. Controller id not provided.');
}

if ($rule['controller'] == $currentControllerId) {

if (isset($rule['duration']) && $rule['duration']) {
$this->duration = $rule['duration'];
}

if (!isset($rule['actions']) || !$rule['actions']) {
throw new GlobalPageCacheException('Invalid rule. Action id(s) not provided.');
}

if (isset($rule['except'])) {

if (!is_array($rule['except'])) {
throw new GlobalPageCacheException('Invalid rule. Except parameter should be of type array.');
}

if (in_array($currentActionId, $rule['except'])) {
return;
}
}

if (in_array('*', $rule['actions']) || in_array($currentActionId, $rule['actions'])) {
$this->attachBehavior($rule);
}
}
}
}

/**
* @param $rule array
* @return array
* @author Saiat Kalbiev <[email protected]>
* @throws GlobalPageCacheException
*/
private function generateVariations($rule)
{
$variations = [];

if (isset($rule['variations'])) {

if (!is_array($rule['variations'])) {
throw new GlobalPageCacheException('Invalid rule. Variations parameter should be of type array.');
}

foreach ($rule['variations'] as $variation) {
if ($variation == self::VARIATION_BY_LANGUAGE) {
$variations[] = \Yii::$app->language;
} else if ($variation == self::VARIATION_BY_URL) {
$variations[] = Yii::$app->request->absoluteUrl;
}
}
}

return $variations;
}

/**
* @param $rule array
* @author Saiat Kalbiev <[email protected]>
* @throws GlobalPageCacheException
*/
private function attachBehavior($rule)
{
$variations = $this->generateVariations($rule);

Yii::$app->controller->attachBehavior('pageCacheBehaviour', [
'class' => 'yii\filters\PageCache',
'duration' => $this->duration,
'variations' => $variations
]);
}
}

0 comments on commit c097dd5

Please sign in to comment.