|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Riimu\Kit\ClassLoader; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Provides a simple method of caching the list of class file locations. |
|
7
|
|
|
* |
|
8
|
|
|
* CacheListClassLoader provides a simple way to implement your own caching |
|
9
|
|
|
* handlers for the ClassLoader. The base idea of this cache is to call a |
|
10
|
|
|
* provided cache save handler when a new class location is found with the |
|
11
|
|
|
* whole class location cache. The saved cache location should be provided |
|
12
|
|
|
* in the constructor when the class loader is constructed. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Riikka Kalliomäki <[email protected]> |
|
15
|
|
|
* @copyright Copyright (c) 2014-2017 Riikka Kalliomäki |
|
16
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT License |
|
17
|
|
|
*/ |
|
18
|
|
|
class CacheListClassLoader extends ClassLoader |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var string[] List of class file locations */ |
|
21
|
|
|
private $cache; |
|
22
|
|
|
|
|
23
|
|
|
/** @var callable|null Callback used for storing the cache */ |
|
24
|
|
|
private $cacheHandler; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Creates a new CacheListClassLoader instance. |
|
28
|
|
|
* |
|
29
|
|
|
* The parameter should contain the paths provided to your cache save |
|
30
|
|
|
* handler. If no cache exists yet, an empty array should be provided |
|
31
|
|
|
* instead. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string[] $cache The cached paths stored by your cache handler |
|
34
|
|
|
*/ |
|
35
|
24 |
|
public function __construct(array $cache) |
|
36
|
|
|
{ |
|
37
|
24 |
|
parent::__construct(); |
|
38
|
|
|
|
|
39
|
24 |
|
$this->cache = $cache; |
|
40
|
24 |
|
$this->cacheHandler = null; |
|
41
|
24 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Sets the callback used to store the cache. |
|
45
|
|
|
* |
|
46
|
|
|
* Whenever a new file location for class is found, the cache handler is |
|
47
|
|
|
* called with an associative array containing the paths for different |
|
48
|
|
|
* classes. The cache handler should store the array and provide it in the |
|
49
|
|
|
* constructor in following requests. |
|
50
|
|
|
* |
|
51
|
|
|
* @param callable $callback Callback for storing cache |
|
52
|
|
|
* @return CacheListClassLoader Returns self for call chaining |
|
53
|
|
|
*/ |
|
54
|
15 |
|
public function setCacheHandler(callable $callback) |
|
55
|
|
|
{ |
|
56
|
15 |
|
$this->cacheHandler = $callback; |
|
57
|
|
|
|
|
58
|
15 |
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Loads the class by first checking if the file path is cached. |
|
63
|
|
|
* @param string $class Full name of the class |
|
64
|
|
|
* @return bool|null True if the class was loaded, false if not |
|
65
|
|
|
*/ |
|
66
|
21 |
|
public function loadClass($class) |
|
67
|
|
|
{ |
|
68
|
21 |
|
$result = $this->loadCachedClass($class); |
|
69
|
|
|
|
|
70
|
21 |
|
if ($result === false) { |
|
71
|
18 |
|
$result = parent::loadClass($class); |
|
72
|
6 |
|
} |
|
73
|
|
|
|
|
74
|
21 |
|
if ($this->verbose) { |
|
75
|
18 |
|
return $result !== false; |
|
76
|
|
|
} |
|
77
|
3 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Attempts loading class from the known class cache. |
|
81
|
|
|
* @param string $class Full name of the class |
|
82
|
|
|
* @return bool True if the class was loaded, false if not |
|
83
|
|
|
*/ |
|
84
|
21 |
|
private function loadCachedClass($class) |
|
85
|
|
|
{ |
|
86
|
21 |
|
$result = false; |
|
87
|
|
|
|
|
88
|
21 |
|
if (isset($this->cache[$class])) { |
|
89
|
9 |
|
$result = include $this->cache[$class]; |
|
90
|
|
|
|
|
91
|
9 |
|
if ($result === false) { |
|
92
|
3 |
|
unset($this->cache[$class]); |
|
93
|
3 |
|
$this->saveCache(); |
|
94
|
1 |
|
} |
|
95
|
3 |
|
} |
|
96
|
|
|
|
|
97
|
21 |
|
return $result !== false; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Loads the class from the given file and stores the path into cache. |
|
102
|
|
|
* @param string $file Full path to the file |
|
103
|
|
|
* @param string $class Full name of the class |
|
104
|
|
|
* @return bool Always returns true |
|
105
|
|
|
* @throws \RuntimeException If the class was not defined in the included file |
|
106
|
|
|
*/ |
|
107
|
12 |
|
protected function loadFile($file, $class) |
|
108
|
|
|
{ |
|
109
|
12 |
|
parent::loadFile($file, $class); |
|
110
|
9 |
|
$this->cache[$class] = $file; |
|
111
|
9 |
|
$this->saveCache(); |
|
112
|
|
|
|
|
113
|
9 |
|
return true; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Saves the cache by calling the cache handler with it. |
|
118
|
|
|
*/ |
|
119
|
12 |
|
private function saveCache() |
|
120
|
|
|
{ |
|
121
|
12 |
|
if ($this->cacheHandler !== null) { |
|
122
|
12 |
|
call_user_func($this->cacheHandler, $this->cache); |
|
123
|
4 |
|
} |
|
124
|
12 |
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|