1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* HiPanel core package |
5
|
|
|
* |
6
|
|
|
* @link https://hipanel.com/ |
7
|
|
|
* @package hipanel-core |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hipanel\components; |
13
|
|
|
|
14
|
|
|
use Closure; |
15
|
|
|
use ReflectionFunction; |
16
|
|
|
use Yii; |
17
|
|
|
|
18
|
|
|
class Cache extends \yii\caching\FileCache |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Caches a [[\Closure]] call, invalidates on timeout. |
22
|
|
|
* |
23
|
|
|
* @param int $timeout seconds |
24
|
|
|
* @param array $params an array of params that will be passed to the $func |
25
|
|
|
* @param Closure $function the function that will be called |
26
|
|
|
* @return mixed |
27
|
|
|
*/ |
28
|
|
|
public function getTimeCached($timeout, array $params, Closure $function) |
29
|
|
|
{ |
30
|
|
|
$key = array_merge([__CLASS__, ReflectionFunction::export($function, true)], $params); |
31
|
|
|
$res = $this->get($key); |
32
|
|
|
if ($res === false) { |
33
|
|
|
$res = call_user_func_array($function, $params); |
34
|
|
|
$this->set($key, $res, $timeout); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $res; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Caches a [[\Closure]] call, invalidates on timeout. |
42
|
|
|
* Forcefully adds current logged in user ID to the params to be a part of the key. |
43
|
|
|
* |
44
|
|
|
* @param int $timeout seconds |
45
|
|
|
* @param array $params an array of params that will be passed to the $func |
46
|
|
|
* @param Closure $function the function that will be called |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
|
|
public function getAuthTimeCached($timeout, array $params, Closure $function) |
50
|
|
|
{ |
51
|
|
|
$params[] = Yii::$app->user->id; |
52
|
|
|
return $this->getTimeCached($timeout, $params, $function); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/* TRY to realize later |
|
|
|
|
56
|
|
|
public function init() |
57
|
|
|
{ |
58
|
|
|
parent::init(); |
59
|
|
|
if (!is_object($this->cache)) { |
60
|
|
|
$this->cache = Instance::ensure($this->cache, yiiCache::className()); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getTimeCached ($timeout, array $params, $func) { |
65
|
|
|
$key = array_merge([__CLASS__], $params); |
66
|
|
|
$res = $this->cache->get($key); |
67
|
|
|
if ($res === false) { |
68
|
|
|
$res = call_user_func_array($func, $params); |
69
|
|
|
$this->getCache()->set($key, $res, $timeout); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $res; |
73
|
|
|
} |
74
|
|
|
*/ |
75
|
|
|
} |
76
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.