@@ -2,92 +2,92 @@ |
||
| 2 | 2 | |
| 3 | 3 | class CachingDecorator |
| 4 | 4 | { |
| 5 | - /** |
|
| 6 | - * The repo implementation. |
|
| 7 | - * |
|
| 8 | - * @var string |
|
| 9 | - */ |
|
| 10 | - protected $repo; |
|
| 5 | + /** |
|
| 6 | + * The repo implementation. |
|
| 7 | + * |
|
| 8 | + * @var string |
|
| 9 | + */ |
|
| 10 | + protected $repo; |
|
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * The cache implementation. |
|
| 14 | - * |
|
| 15 | - * @var object |
|
| 16 | - */ |
|
| 17 | - protected $cache; |
|
| 12 | + /** |
|
| 13 | + * The cache implementation. |
|
| 14 | + * |
|
| 15 | + * @var object |
|
| 16 | + */ |
|
| 17 | + protected $cache; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The model implementation. |
|
| 21 | - * |
|
| 22 | - * @var string |
|
| 23 | - */ |
|
| 24 | - public $model; |
|
| 19 | + /** |
|
| 20 | + * The model implementation. |
|
| 21 | + * |
|
| 22 | + * @var string |
|
| 23 | + */ |
|
| 24 | + public $model; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * The cacheConfig implementation. |
|
| 28 | - * |
|
| 29 | - * @var array |
|
| 30 | - */ |
|
| 31 | - public $cacheConfig; |
|
| 26 | + /** |
|
| 27 | + * The cacheConfig implementation. |
|
| 28 | + * |
|
| 29 | + * @var array |
|
| 30 | + */ |
|
| 31 | + public $cacheConfig; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Create new CachingDecorator instance. |
|
| 35 | - */ |
|
| 36 | - public function __construct($repo, $cache) |
|
| 37 | - { |
|
| 38 | - $this->repo = $repo; |
|
| 39 | - $this->cache = $cache; |
|
| 40 | - $this->model = get_class($this->repo->model); |
|
| 41 | - } |
|
| 33 | + /** |
|
| 34 | + * Create new CachingDecorator instance. |
|
| 35 | + */ |
|
| 36 | + public function __construct($repo, $cache) |
|
| 37 | + { |
|
| 38 | + $this->repo = $repo; |
|
| 39 | + $this->cache = $cache; |
|
| 40 | + $this->model = get_class($this->repo->model); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Handle the cache mechanism for the called method |
|
| 45 | - * based the configurations. |
|
| 46 | - * |
|
| 47 | - * @param string $name the called method name |
|
| 48 | - * @param array $arguments the method arguments |
|
| 49 | - * @return object |
|
| 50 | - */ |
|
| 51 | - public function __call($name, $arguments) |
|
| 52 | - { |
|
| 53 | - $this->setCacheConfig($name); |
|
| 43 | + /** |
|
| 44 | + * Handle the cache mechanism for the called method |
|
| 45 | + * based the configurations. |
|
| 46 | + * |
|
| 47 | + * @param string $name the called method name |
|
| 48 | + * @param array $arguments the method arguments |
|
| 49 | + * @return object |
|
| 50 | + */ |
|
| 51 | + public function __call($name, $arguments) |
|
| 52 | + { |
|
| 53 | + $this->setCacheConfig($name); |
|
| 54 | 54 | |
| 55 | - if ($this->cacheConfig && $this->cacheConfig == 'cache') |
|
| 56 | - { |
|
| 57 | - $page = \Request::get('page') ?? '1'; |
|
| 58 | - $cacheKey = $name . $page . serialize($arguments); |
|
| 59 | - return $this->cache->tags([$this->model])->rememberForever($cacheKey, function() use ($arguments, $name) { |
|
| 60 | - return call_user_func_array([$this->repo, $name], $arguments); |
|
| 61 | - }); |
|
| 62 | - } |
|
| 63 | - else if ($this->cacheConfig) |
|
| 64 | - { |
|
| 65 | - $this->cache->tags($this->cacheConfig)->flush(); |
|
| 66 | - return call_user_func_array([$this->repo, $name], $arguments); |
|
| 67 | - } |
|
| 55 | + if ($this->cacheConfig && $this->cacheConfig == 'cache') |
|
| 56 | + { |
|
| 57 | + $page = \Request::get('page') ?? '1'; |
|
| 58 | + $cacheKey = $name . $page . serialize($arguments); |
|
| 59 | + return $this->cache->tags([$this->model])->rememberForever($cacheKey, function() use ($arguments, $name) { |
|
| 60 | + return call_user_func_array([$this->repo, $name], $arguments); |
|
| 61 | + }); |
|
| 62 | + } |
|
| 63 | + else if ($this->cacheConfig) |
|
| 64 | + { |
|
| 65 | + $this->cache->tags($this->cacheConfig)->flush(); |
|
| 66 | + return call_user_func_array([$this->repo, $name], $arguments); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - return call_user_func_array([$this->repo, $name], $arguments); |
|
| 70 | - } |
|
| 69 | + return call_user_func_array([$this->repo, $name], $arguments); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Set cache config based on the called method. |
|
| 74 | - * |
|
| 75 | - * @param string $name |
|
| 76 | - * @return void |
|
| 77 | - */ |
|
| 78 | - private function setCacheConfig($name) |
|
| 79 | - { |
|
| 80 | - $config = \CoreConfig::getConfig(); |
|
| 81 | - $cacheConfig = array_key_exists($this->model, $config['cacheConfig']) ? $config['cacheConfig'][$this->model] : false; |
|
| 82 | - $this->cacheConfig = false; |
|
| 72 | + /** |
|
| 73 | + * Set cache config based on the called method. |
|
| 74 | + * |
|
| 75 | + * @param string $name |
|
| 76 | + * @return void |
|
| 77 | + */ |
|
| 78 | + private function setCacheConfig($name) |
|
| 79 | + { |
|
| 80 | + $config = \CoreConfig::getConfig(); |
|
| 81 | + $cacheConfig = array_key_exists($this->model, $config['cacheConfig']) ? $config['cacheConfig'][$this->model] : false; |
|
| 82 | + $this->cacheConfig = false; |
|
| 83 | 83 | |
| 84 | - if (in_array($cacheConfig['cache'], $name)) |
|
| 85 | - { |
|
| 86 | - $this->cacheConfig = 'cache'; |
|
| 87 | - } |
|
| 88 | - else if (in_array($cacheConfig['clear'], $name)) |
|
| 89 | - { |
|
| 90 | - $this->cacheConfig = $cacheConfig['clear'][$name]; |
|
| 91 | - } |
|
| 92 | - } |
|
| 84 | + if (in_array($cacheConfig['cache'], $name)) |
|
| 85 | + { |
|
| 86 | + $this->cacheConfig = 'cache'; |
|
| 87 | + } |
|
| 88 | + else if (in_array($cacheConfig['clear'], $name)) |
|
| 89 | + { |
|
| 90 | + $this->cacheConfig = $cacheConfig['clear'][$name]; |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | 93 | } |
| 94 | 94 | \ No newline at end of file |