1 | <?php |
||
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
115 | |||
116 | /** |
||
117 | * Saves the cache by calling the cache handler with it. |
||
118 | */ |
||
119 | 12 | private function saveCache() |
|
125 | } |
||
126 |