FileCacheClassLoader::getCacheFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Riimu\Kit\ClassLoader;
4
5
/**
6
 * Provides a simplified PHP file storage for the class file path cache.
7
 *
8
 * FileCacheClassLoader implements the CacheListClassLoader by storing the
9
 * class paths in a PHP file that is loaded when the class is loaded. If changes
10
 * are made to the cache, a new cache file is generated at the end of the
11
 * request, instead of on every new file to reduce file writes.
12
 *
13
 * @author Riikka Kalliomäki <[email protected]>
14
 * @copyright Copyright (c) 2014-2017 Riikka Kalliomäki
15
 * @license http://opensource.org/licenses/mit-license.php MIT License
16
 */
17
class FileCacheClassLoader extends CacheListClassLoader
18
{
19
    /** @var string Path to the cache file */
20
    private $cacheFile;
21
22
    /** @var string[]|null Cache to store at the end of the request */
23
    private $store;
24
25
    /**
26
     * Creates new FileCacheClassLoader instance and loads the cache file.
27
     * @param string $cacheFile Path to cache file
28
     */
29 9
    public function __construct($cacheFile)
30
    {
31 9
        $this->store = null;
32 9
        $this->cacheFile = $cacheFile;
33
34 9
        if (file_exists($cacheFile)) {
35 3
            $cache = include $cacheFile;
36 1
        }
37
38 9
        if (empty($cache) || !is_array($cache)) {
39 9
            $cache = [];
40 3
        }
41
42 9
        parent::__construct($cache);
43 9
        $this->setCacheHandler([$this, 'storeCache']);
44 9
    }
45
46
    /**
47
     * Writes the cache file if changes were made.
48
     */
49 9
    public function saveCacheFile()
50
    {
51 9
        if ($this->store !== null) {
52 6
            file_put_contents($this->cacheFile, $this->createCache($this->store), LOCK_EX);
53 6
            $this->store = null;
54 2
        }
55 9
    }
56
57
    /**
58
     * Returns the path to the cache file.
59
     * @return string Path to the cache file
60
     */
61 6
    public function getCacheFile()
62
    {
63 6
        return $this->cacheFile;
64
    }
65
66
    /**
67
     * Stores the cache to be saved at the end of the request.
68
     * @param string[] $cache Class location cache
69
     */
70 6
    public function storeCache(array $cache)
71
    {
72 6
        if ($this->store === null) {
73 6
            register_shutdown_function([$this, 'saveCacheFile']);
74 2
        }
75
76 6
        $this->store = $cache;
77 6
    }
78
79
    /**
80
     * Creates the PHP code for the class cache.
81
     * @param string[] $cache Class location cache
82
     * @return string PHP code for the cache file
83
     */
84 6
    private function createCache(array $cache)
85
    {
86 6
        ksort($cache);
87
88 6
        $format = "\t%s => %s," . PHP_EOL;
89 6
        $rows = [];
90
91 6
        foreach ($cache as $key => $value) {
92 6
            $rows[] = sprintf($format, var_export($key, true), var_export($value, true));
93 2
        }
94
95 6
        return sprintf('<?php return [%s];' . PHP_EOL, PHP_EOL . implode('', $rows));
96
    }
97
}
98