Completed
Push — master ( 89c3e8...c161b3 )
by Craig
05:56 queued 40s
created

CacheClearer::clear()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 11
c 0
b 0
f 0
nc 12
nop 1
dl 0
loc 22
rs 8.8333
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\Bundle\CoreBundle;
15
16
use FilesystemIterator;
17
use FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractorInterface;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
use Symfony\Component\Filesystem\Filesystem;
20
21
class CacheClearer
22
{
23
    /**
24
     * @var ContainerInterface
25
     */
26
    private $container;
27
28
    /**
29
     * @var string
30
     */
31
    private $cacheDir;
32
33
    /**
34
     * @var string
35
     */
36
    private $kernelContainerClass;
37
38
    /**
39
     * @var string
40
     */
41
    private $containerDirectory;
42
43
    /**
44
     * @var bool
45
     */
46
    private $installed;
47
48
    /**
49
     * @var array
50
     */
51
    private $routingLocales = [];
52
53
    /**
54
     * @var array
55
     */
56
    private $cacheTypes = [];
57
58
    /**
59
     * @var Filesystem
60
     */
61
    private $fileSystem;
62
63
    public function __construct(
64
        ContainerInterface $container,
65
        string $cacheDir,
66
        string $kernelContainerClass,
67
        bool $installed,
68
        array $routingLocales = []
69
    ) {
70
        $this->container = $container;
71
        $this->cacheDir = $cacheDir;
72
        $refClass = new \ReflectionClass($container);
73
        $this->containerDirectory = $cacheDir . DIRECTORY_SEPARATOR . $refClass->getNamespaceName();
74
        $this->kernelContainerClass = $kernelContainerClass;
75
        $this->installed = $installed;
76
        $this->routingLocales = $routingLocales;
77
        $this->fileSystem = new Filesystem();
78
    }
79
80
    public function clear(string $type): void
81
    {
82
        if (!count($this->cacheTypes)) {
83
            $this->initialiseCacheTypeMap();
84
        }
85
86
        foreach ($this->cacheTypes as $cacheType => $files) {
87
            if (0 !== mb_strpos($cacheType, $type)) {
88
                continue;
89
            }
90
            foreach ($files as $file) {
91
                if (is_dir($file)) {
92
                    // Do not delete the folder itself, but all files in it.
93
                    // Otherwise Symfony somehow can't create the folder anymore.
94
                    $file = new FilesystemIterator($file);
95
                }
96
                // This silently ignores non existing files.
97
                $this->fileSystem->remove($file);
98
            }
99
        }
100
        if (in_array($type, ['symfony', 'symfony.config'])) {
101
            $this->fileSystem->remove($this->containerDirectory);
102
        }
103
    }
104
105
    private function initialiseCacheTypeMap()
106
    {
107
        $fosJsRoutingFiles = [];
108
        if ($this->installed) {
109
            // avoid accessing FOS extractor before/during installation
110
            // because this requires request context
111
112
            /** @var ExposedRoutesExtractorInterface */
113
            $fosJsRoutesExtractor = $this->container->get('fos_js_routing.extractor');
114
            foreach ($this->routingLocales as $locale) {
115
                $fosJsRoutingFiles[] = $fosJsRoutesExtractor->getCachePath($locale);
116
            }
117
        }
118
119
        $cacheFolder = $this->cacheDir . DIRECTORY_SEPARATOR;
120
121
        $this->cacheTypes = [
122
            'symfony.annotations' => [
123
                $cacheFolder . 'annotations.map',
124
                $cacheFolder . 'annotations.php'
125
            ],
126
            'symfony.routing.generator' => [
127
                $cacheFolder . 'url_generating_routes.php',
128
                $cacheFolder . 'url_generating_routes.php.meta'
129
            ],
130
            'symfony.routing.matcher' => [
131
                $cacheFolder . 'url_matching_routes.php',
132
                $cacheFolder . 'url_matching_routes.php.meta'
133
            ],
134
            'symfony.routing.fosjs' => $fosJsRoutingFiles,
135
            'symfony.config' => [
136
                $cacheFolder . $this->kernelContainerClass . '.php',
137
                $cacheFolder . $this->kernelContainerClass . '.php.meta',
138
                $cacheFolder . $this->kernelContainerClass . '.preload.php',
139
                $cacheFolder . $this->kernelContainerClass . '.xml',
140
                $cacheFolder . $this->kernelContainerClass . '.xml.meta',
141
                $cacheFolder . $this->kernelContainerClass . 'Compiler.log',
142
                $cacheFolder . $this->kernelContainerClass . 'Deprecations.log',
143
                $cacheFolder . 'classes.map'
144
            ],
145
            'symfony.translations' => [
146
                $cacheFolder . '/translations'
147
            ],
148
            'twig' => [
149
                $cacheFolder . 'twig'
150
            ],
151
            'purifier' => [
152
                $cacheFolder . 'purifier'
153
            ],
154
            'assets' => [
155
                $cacheFolder . 'assets'
156
            ]
157
        ];
158
    }
159
}
160