init
This commit is contained in:
31
components/AssetManager.php
Normal file
31
components/AssetManager.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
namespace app\components;
|
||||
|
||||
/**
|
||||
* Description of AssetManager
|
||||
*
|
||||
* @author Kdg
|
||||
*/
|
||||
class AssetManager extends \yii\web\AssetManager {
|
||||
|
||||
public $appendVersion = false;
|
||||
|
||||
public function getAssetUrl($bundle, $asset) {
|
||||
$url = parent::getAssetUrl($bundle, $asset);
|
||||
if (is_callable($this->appendVersion)) {
|
||||
$this->appendVersion = call_user_func($this->appendVersion);
|
||||
}
|
||||
if (!empty($this->appendVersion)) {
|
||||
$url .= (stripos($url, '?') === false ? '?ver=' : '&ver=') . $this->appendVersion;
|
||||
}
|
||||
return $url;
|
||||
}
|
||||
|
||||
}
|
||||
147
components/CacheRbacDbManager.php
Normal file
147
components/CacheRbacDbManager.php
Normal file
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
namespace app\components;
|
||||
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
/**
|
||||
* Description of CacheRbacManager
|
||||
*
|
||||
* @author Kdg
|
||||
*/
|
||||
class CacheRbacDbManager extends \yii\rbac\DbManager {
|
||||
|
||||
/**
|
||||
* @var string the ID of the cache application component that is used to cache rbac.
|
||||
* Defaults to 'cache' which refers to the primary cache application component.
|
||||
*/
|
||||
public $cache = 'cache';
|
||||
|
||||
/**
|
||||
* @var integer Lifetime of cached data in seconds
|
||||
*/
|
||||
public $cacheDuration = 3600;
|
||||
|
||||
/**
|
||||
* @var string cache key name
|
||||
*/
|
||||
public $cacheKeyName = 'RbacCached';
|
||||
|
||||
/**
|
||||
* @var array php cache
|
||||
*/
|
||||
protected $cachedData = [];
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function checkAccess($userId, $permissionName, $params = []) {
|
||||
if (!empty($params))
|
||||
return parent::checkAccess($userId, $permissionName, $params);
|
||||
$cacheKey = 'checkAccess:' . $userId . ':' . $permissionName;
|
||||
$cached = $this->getValue($cacheKey);
|
||||
if (empty($cached)) {
|
||||
$cached = parent::checkAccess($userId, $permissionName);
|
||||
$this->setValue($cacheKey, $cached);
|
||||
}
|
||||
return $cached;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function checkAccessRecursive($user, $itemName, $params, $assignments) {
|
||||
$cacheKey = 'checkAccessRecursive:' . $user . ':' . $itemName;
|
||||
if (!empty($params))
|
||||
$cacheKey .= ':' . current($params)->primaryKey;
|
||||
$cached = $this->getValue($cacheKey);
|
||||
if (empty($cached)) {
|
||||
$cached = parent::checkAccessRecursive($user, $itemName, $params, $assignments);
|
||||
$this->setValue($cacheKey, $cached);
|
||||
}
|
||||
return $cached;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getItem($name) {
|
||||
$cacheKey = 'Item:' . $name;
|
||||
$cached = $this->getValue($cacheKey);
|
||||
if (empty($cached)) {
|
||||
$cached = parent::getItem($name);
|
||||
$this->setValue($cacheKey, $cached);
|
||||
}
|
||||
return $cached;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getAssignments($userId) {
|
||||
if (empty($userId))
|
||||
return parent::getAssignments($userId);
|
||||
$cacheKey = 'Assignments:' . $userId;
|
||||
$cached = $this->getValue($cacheKey);
|
||||
if (empty($cached)) {
|
||||
$cached = parent::getAssignments($userId);
|
||||
$this->setValue($cacheKey, $cached);
|
||||
}
|
||||
return $cached;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a value in cache
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @return mixed
|
||||
*/
|
||||
protected function setValue($key, $value) {
|
||||
$this->cachedData = $this->getCache()->get($this->cacheKeyName);
|
||||
if (empty($this->cachedData))
|
||||
$this->cachedData = [];
|
||||
$this->cachedData[$key] = $value;
|
||||
return $this->getCache()->set($this->cacheKeyName, $this->cachedData, $this->cacheDuration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cached value
|
||||
* @param $key
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getValue($key) {
|
||||
$cached = ArrayHelper::getValue($this->cachedData, $key);
|
||||
if (!isset($cached)) {
|
||||
$cacheData = $this->getCache()->get($this->cacheKeyName);
|
||||
$cached = $this->cachedData[$key] = ArrayHelper::getValue($cacheData, $key);
|
||||
}
|
||||
return $cached;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cached value
|
||||
* @param $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function deleteAllCache() {
|
||||
return $this->getCache()->delete($this->cacheKeyName);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return \yii\caching\Cache
|
||||
*/
|
||||
private function getCache() {
|
||||
if (is_string($this->cache)) {
|
||||
$this->cache = \Yii::$app->get($this->cache);
|
||||
}
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user