|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace artkost\rbac; |
| 4 | + |
| 5 | +use artkost\rbac\models\RbacDefinition; |
| 6 | +use Yii; |
| 7 | +use yii\base\Component; |
| 8 | +use yii\base\InvalidConfigException; |
| 9 | +use yii\caching\TagDependency; |
| 10 | + |
| 11 | +/** |
| 12 | + * This class finds rules definitions in other modules |
| 13 | + * and instantiates it |
| 14 | + * @package app\modules\rbac |
| 15 | + */ |
| 16 | +class DefinitionManager extends Component |
| 17 | +{ |
| 18 | + const DEFINITION_FILE = 'rules.php'; |
| 19 | + |
| 20 | + const CACHE_KEY = 'rbacModuleDefinition'; |
| 21 | + const CACHE_TAG = 'rbacModuleDefinition'; |
| 22 | + |
| 23 | + public $cacheDuration = 604800; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var RbacDefinition[] |
| 27 | + */ |
| 28 | + protected $definitions = []; |
| 29 | + |
| 30 | + public function init() |
| 31 | + { |
| 32 | + parent::init(); |
| 33 | + |
| 34 | + $this->createDefinitions(); |
| 35 | + } |
| 36 | + |
| 37 | + protected function createDefinitions() |
| 38 | + { |
| 39 | + foreach (Yii::$app->getModules() as $id => $config) { |
| 40 | + $this->createDefinition($id, $this->getDefinitionConfig($id, $config)); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @return models\RbacDefinition[] |
| 46 | + */ |
| 47 | + public function getDefinitions() |
| 48 | + { |
| 49 | + return $this->definitions; |
| 50 | + } |
| 51 | + |
| 52 | + public function getDefinitionConfig($id, $config = []) |
| 53 | + { |
| 54 | + $module = Yii::$app->getModule($id); |
| 55 | + |
| 56 | + return $module->getBasePath() . DIRECTORY_SEPARATOR . self::DEFINITION_FILE; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @param $id |
| 61 | + * @param $path |
| 62 | + * @return RbacDefinition|bool |
| 63 | + */ |
| 64 | + protected function createDefinition($id, $path) |
| 65 | + { |
| 66 | + if (!isset($this->definitions[$id])) { |
| 67 | + $rules = []; |
| 68 | + |
| 69 | + $cached = $this->getCache()->get(self::CACHE_KEY . ':' . $path); |
| 70 | + $dependency = Yii::createObject(TagDependency::className(), ['tags' => [self::CACHE_TAG]]); |
| 71 | + |
| 72 | + if ($cached) { |
| 73 | + $rules = $cached; |
| 74 | + } else { |
| 75 | + if (is_array($path)) { |
| 76 | + $rules = $path; |
| 77 | + } elseif (file_exists($path)) { |
| 78 | + $rules = include $path; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (is_array($rules) && !empty($rules)) { |
| 83 | + $rules['module'] = $id; |
| 84 | + $this->getCache()->set(self::CACHE_KEY . ':' . $path, $rules, $this->cacheDuration, $dependency); |
| 85 | + $this->definitions[$id] = new RbacDefinition($rules); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return isset($this->definitions[$id]) ? $this->definitions[$id] : false; |
| 90 | + } |
| 91 | + |
| 92 | + public function refreshDefinitions() |
| 93 | + { |
| 94 | + TagDependency::invalidate($this->getCache(), [self::CACHE_TAG]); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @return \yii\caching\Cache |
| 99 | + */ |
| 100 | + protected function getCache() |
| 101 | + { |
| 102 | + return Yii::$app->cache; |
| 103 | + } |
| 104 | +} |
0 commit comments