AutoloaderFileBackend   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 70
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 9 2
A get() 0 10 2
A has() 0 4 1
A remove() 0 4 1
A flush() 0 7 2
A collectGarbage() 0 5 1
A getCacheFileName() 0 4 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace HDNET\Autoloader\Cache;
6
7
use TYPO3\CMS\Core\Core\Environment;
8
use TYPO3\CMS\Core\Utility\GeneralUtility;
9
10
/**
11
 * AutoloaderFileBackend.
12
 *
13
 * Note: This backend is usable without the caching framework
14
 */
15
class AutoloaderFileBackend extends \TYPO3\CMS\Core\Cache\Backend\AbstractBackend
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function set($entryIdentifier, $data, array $tags = [], $lifetime = null)
21
    {
22
        if (\is_array($data)) {
23
            $cacheFile = $this->getCacheFileName($entryIdentifier);
24
            GeneralUtility::writeFile($cacheFile, '<?php return ' . var_export($data, true) . ';');
25
        }
26
27
        return null;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function get($entryIdentifier)
34
    {
35
        if ($this->has($entryIdentifier)) {
36
            $content = include $this->getCacheFileName($entryIdentifier);
37
38
            return $content;
39
        }
40
41
        return null;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function has($entryIdentifier)
48
    {
49
        return is_file($this->getCacheFileName($entryIdentifier));
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function remove($entryIdentifier)
56
    {
57
        return null;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function flush(): void
64
    {
65
        $files = glob(Environment::getVarPath() . '/autoloader_*');
66
        foreach ($files as $file) {
67
            unlink($file);
68
        }
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function collectGarbage()
75
    {
76
        // Write by Loader::class
77
        return null;
78
    }
79
80
    protected function getCacheFileName($entryIdentifier): string
81
    {
82
        return Environment::getVarPath() . '/autoloader_' . $entryIdentifier . '.php';
83
    }
84
}
85