1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Yii 2 PHP file cache. |
4
|
|
|
* |
5
|
|
|
* @see https://github.com/sergeymakinen/yii2-php-file-cache |
6
|
|
|
* @copyright Copyright (c) 2016-2017 Sergey Makinen (https://makinen.ru) |
7
|
|
|
* @license https://github.com/sergeymakinen/yii2-php-file-cache/blob/master/LICENSE MIT License |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace sergeymakinen\caching; |
11
|
|
|
|
12
|
|
|
use yii\caching\FileCache; |
13
|
|
|
use yii\helpers\VarDumper; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* PhpFileCache implements a cache component using PHP files. |
17
|
|
|
*/ |
18
|
|
|
class PhpFileCache extends FileCache |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @inheritDoc |
22
|
|
|
*/ |
23
|
|
|
public $cacheFileSuffix = '.php'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @inheritDoc |
27
|
|
|
*/ |
28
|
29 |
|
public function init() |
29
|
|
|
{ |
30
|
29 |
|
parent::init(); |
31
|
29 |
|
if ($this->serializer === null) { |
32
|
29 |
|
$this->serializer = [ |
33
|
|
|
function ($value) { |
34
|
26 |
|
if ($value[0] instanceof ValueWithBootstrap) { |
35
|
15 |
|
if ($value[0]->bootstrap instanceof \Closure) { |
36
|
10 |
|
$bootstrap = ''; |
37
|
10 |
|
$namespaces = $this->extractClosureNamespaces($value[0]->bootstrap); |
38
|
10 |
|
if ($namespaces['namespace'] !== null) { |
39
|
10 |
|
$bootstrap .= 'namespace ' . $namespaces['namespace'] . ";\n\n"; |
40
|
10 |
|
} |
41
|
10 |
|
foreach ($namespaces['uses'] as $alias => $namespace) { |
42
|
10 |
|
$bootstrap .= 'use ' . $namespace; |
43
|
10 |
|
if (is_string($alias)) { |
44
|
5 |
|
$bootstrap .= ' as ' . $alias; |
45
|
5 |
|
} |
46
|
10 |
|
$bootstrap .= ";\n"; |
47
|
10 |
|
} |
48
|
10 |
|
$bootstrap .= 'call_user_func(' . VarDumper::export($value[0]->bootstrap) . ');'; |
49
|
10 |
|
} else { |
50
|
5 |
|
$bootstrap = $value[0]->bootstrap; |
51
|
|
|
} |
52
|
15 |
|
$bootstrap = trim($bootstrap) . "\n\n"; |
53
|
15 |
|
$value[0] = $value[0]->value; |
54
|
15 |
|
} else { |
55
|
11 |
|
$bootstrap = ''; |
56
|
|
|
} |
57
|
26 |
|
return "<?php\n\n{$bootstrap}return " . VarDumper::export($value) . ";\n"; |
58
|
29 |
|
}, |
59
|
29 |
|
function ($value) { |
60
|
26 |
|
return $value; |
61
|
29 |
|
}, |
62
|
|
|
]; |
63
|
29 |
|
} |
64
|
29 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritDoc |
68
|
|
|
*/ |
69
|
28 |
|
protected function getValue($key) |
70
|
|
|
{ |
71
|
28 |
|
$cacheFile = $this->getCacheFile($key); |
72
|
28 |
|
if (@filemtime($cacheFile) > time()) { |
73
|
|
|
/** @noinspection PhpIncludeInspection */ |
74
|
27 |
|
$cacheValue = @include $cacheFile; |
75
|
27 |
|
if (is_array($cacheValue) && array_key_exists(0, $cacheValue)) { |
76
|
26 |
|
return $cacheValue; |
|
|
|
|
77
|
|
|
} |
78
|
1 |
|
} |
79
|
|
|
|
80
|
3 |
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns Closure's namespace and uses. |
85
|
|
|
* @param \Closure $closure |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
11 |
|
private function extractClosureNamespaces(\Closure $closure) |
89
|
|
|
{ |
90
|
11 |
|
$function = new \ReflectionFunction($closure); |
91
|
11 |
|
if ($function->getFileName() === false || strpos($function->getFileName(), 'eval()\'d code') !== false) { |
92
|
|
|
return [ |
93
|
1 |
|
'namespace' => null, |
94
|
1 |
|
'uses' => [], |
95
|
1 |
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
11 |
|
$closureNamespace = null; |
99
|
11 |
|
$closureUses = []; |
100
|
11 |
|
$source = implode(array_slice( |
101
|
11 |
|
file($function->getFileName()), |
102
|
11 |
|
0, |
103
|
11 |
|
$function->getEndLine() |
104
|
11 |
|
)); |
105
|
11 |
|
$tokens = token_get_all('<?php ' . $source); |
106
|
11 |
|
$state = null; |
107
|
11 |
|
$namespace = null; |
108
|
11 |
|
$alias = null; |
109
|
11 |
|
foreach ($tokens as $token) { |
110
|
11 |
|
if (is_array($token)) { |
111
|
11 |
|
if ($state === null && ($token[0] === T_NAMESPACE || $token[0] === T_USE)) { |
112
|
11 |
|
$state = $token[0]; |
113
|
11 |
|
$namespace = ''; |
114
|
11 |
|
} elseif ($state === T_USE && $alias === null && $token[0] === T_AS) { |
115
|
6 |
|
$alias = ''; |
116
|
11 |
|
} elseif ($state !== null && ($token[0] === T_STRING || $token[0] === T_NS_SEPARATOR)) { |
117
|
11 |
|
if ($alias !== null) { |
118
|
6 |
|
$alias .= $token[1]; |
119
|
6 |
|
} else { |
120
|
11 |
|
$namespace .= $token[1]; |
121
|
|
|
} |
122
|
11 |
|
} |
123
|
11 |
|
} else { |
124
|
11 |
|
if ($state !== null && ($token === ';' || $token === '{')) { |
125
|
11 |
|
if ($alias === null) { |
126
|
11 |
|
if ($state === T_NAMESPACE) { |
127
|
11 |
|
$closureNamespace = $namespace; |
128
|
11 |
|
$closureUses = []; |
129
|
11 |
|
} else { |
130
|
11 |
|
$closureUses[] = $namespace; |
131
|
|
|
} |
132
|
11 |
|
} else { |
133
|
6 |
|
$closureUses[$alias] = $namespace; |
134
|
6 |
|
$alias = null; |
135
|
|
|
} |
136
|
11 |
|
$state = null; |
137
|
11 |
|
$namespace = null; |
138
|
11 |
|
} elseif ($state === T_USE && $token === ',') { |
139
|
6 |
|
if ($alias === null) { |
140
|
6 |
|
$closureUses[] = $namespace; |
141
|
6 |
|
} else { |
142
|
6 |
|
$closureUses[$alias] = $namespace; |
143
|
6 |
|
$alias = null; |
144
|
|
|
} |
145
|
6 |
|
$namespace = null; |
146
|
6 |
|
} |
147
|
|
|
} |
148
|
11 |
|
} |
149
|
|
|
return [ |
150
|
11 |
|
'namespace' => $closureNamespace, |
151
|
11 |
|
'uses' => $closureUses, |
152
|
11 |
|
]; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.