| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * @link https://dukt.net/analytics/ |
||||
| 4 | * @copyright Copyright (c) 2022, Dukt |
||||
| 5 | * @license https://github.com/dukt/analytics/blob/master/LICENSE.md |
||||
| 6 | */ |
||||
| 7 | |||||
| 8 | namespace dukt\analytics\services; |
||||
| 9 | |||||
| 10 | use Craft; |
||||
| 11 | use craft\helpers\DateTimeHelper; |
||||
| 12 | use yii\base\Component; |
||||
| 13 | use dukt\analytics\Plugin as Analytics; |
||||
|
0 ignored issues
–
show
|
|||||
| 14 | use DateInterval; |
||||
| 15 | |||||
| 16 | class Cache extends Component |
||||
| 17 | { |
||||
| 18 | // Public Methods |
||||
| 19 | // ========================================================================= |
||||
| 20 | |||||
| 21 | /** |
||||
| 22 | * Get cache |
||||
| 23 | * |
||||
| 24 | * @param $id |
||||
| 25 | * |
||||
| 26 | * @return mixed |
||||
| 27 | */ |
||||
| 28 | public function get($id) |
||||
| 29 | { |
||||
| 30 | if (Analytics::$plugin->getSettings()->enableCache == true) { |
||||
| 31 | $cacheKey = $this->getCacheKey($id); |
||||
| 32 | |||||
| 33 | return Craft::$app->getCache()->get($cacheKey); |
||||
| 34 | } |
||||
| 35 | |||||
| 36 | return null; |
||||
| 37 | } |
||||
| 38 | |||||
| 39 | /** |
||||
| 40 | * Set cache |
||||
| 41 | * |
||||
| 42 | * @param $id |
||||
| 43 | * @param $value |
||||
| 44 | * @param null $expire |
||||
|
0 ignored issues
–
show
|
|||||
| 45 | * @param null $dependency |
||||
|
0 ignored issues
–
show
|
|||||
| 46 | * @param null $enableCache |
||||
|
0 ignored issues
–
show
|
|||||
| 47 | * |
||||
| 48 | * @return mixed |
||||
| 49 | * @throws \Exception |
||||
| 50 | */ |
||||
| 51 | public function set($id, $value, $expire = null, $dependency = null, $enableCache = null) |
||||
| 52 | { |
||||
| 53 | if (null === $enableCache) { |
||||
|
0 ignored issues
–
show
|
|||||
| 54 | $enableCache = Analytics::$plugin->getSettings()->enableCache; |
||||
| 55 | } |
||||
| 56 | |||||
| 57 | if ($enableCache) { |
||||
| 58 | $cacheKey = $this->getCacheKey($id); |
||||
| 59 | |||||
| 60 | if (!$expire) { |
||||
|
0 ignored issues
–
show
|
|||||
| 61 | $expire = $this->getCacheDuration(); |
||||
| 62 | } |
||||
| 63 | |||||
| 64 | return Craft::$app->getCache()->set($cacheKey, $value, $expire, $dependency); |
||||
| 65 | } |
||||
| 66 | |||||
| 67 | return null; |
||||
| 68 | } |
||||
| 69 | |||||
| 70 | /** |
||||
| 71 | * Deletes a value with the specified key from cache. |
||||
| 72 | * |
||||
| 73 | * @param string $id The key of the value to be deleted. |
||||
| 74 | * |
||||
| 75 | * @return bool If no error happens during deletion. |
||||
| 76 | */ |
||||
| 77 | public function delete($id) |
||||
| 78 | { |
||||
| 79 | $cacheKey = $this->getCacheKey($id); |
||||
|
0 ignored issues
–
show
$id of type string is incompatible with the type array expected by parameter $request of dukt\analytics\services\Cache::getCacheKey().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 80 | |||||
| 81 | return Craft::$app->getCache()->delete($cacheKey); |
||||
| 82 | } |
||||
| 83 | |||||
| 84 | // Private Methods |
||||
| 85 | // ========================================================================= |
||||
| 86 | |||||
| 87 | /** |
||||
| 88 | * Get cache duration (in seconds) |
||||
| 89 | * |
||||
| 90 | * @return int |
||||
| 91 | * @throws \Exception |
||||
| 92 | */ |
||||
| 93 | private function getCacheDuration() |
||||
| 94 | { |
||||
| 95 | $duration = Analytics::$plugin->getSettings()->cacheDuration; |
||||
| 96 | |||||
| 97 | return DateTimeHelper::intervalToSeconds(new DateInterval($duration)); |
||||
| 98 | } |
||||
| 99 | |||||
| 100 | /** |
||||
| 101 | * Get cache key |
||||
| 102 | * |
||||
| 103 | * @param array $request |
||||
| 104 | * |
||||
| 105 | * @return string |
||||
| 106 | */ |
||||
| 107 | private function getCacheKey(array $request) |
||||
| 108 | { |
||||
| 109 | $dataSourceClassName = 'GoogleAnalytics'; |
||||
| 110 | |||||
| 111 | unset($request['CRAFT_CSRF_TOKEN']); |
||||
| 112 | |||||
| 113 | $request[] = $dataSourceClassName; |
||||
| 114 | |||||
| 115 | $hash = md5(serialize($request)); |
||||
| 116 | |||||
| 117 | $cacheKey = 'analytics.'.$hash; |
||||
| 118 | |||||
| 119 | return $cacheKey; |
||||
| 120 | } |
||||
| 121 | } |
||||
| 122 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: