• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CPS-IT / frontend-asset-handler / 15251341592

26 May 2025 10:12AM UTC coverage: 99.233%. Remained the same
15251341592

Pull #659

github

web-flow
Merge c6e7b1587 into 8b95d2cff
Pull Request #659: [TASK] Update phpunit/phpunit to v12

2459 of 2478 relevant lines covered (99.23%)

15.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.94
/src/DependencyInjection/Cache/ContainerCache.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Composer package "cpsit/frontend-asset-handler".
7
 *
8
 * Copyright (C) 2022 Elias Häußler <e.haeussler@familie-redlich.de>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace CPSIT\FrontendAssetHandler\DependencyInjection\Cache;
25

26
use CPSIT\FrontendAssetHandler\DependencyInjection;
27
use CPSIT\FrontendAssetHandler\Exception;
28
use Symfony\Component\Config;
29
use Symfony\Component\DependencyInjection as SymfonyDI;
30
use Symfony\Component\Filesystem;
31

32
use function assert;
33
use function dirname;
34
use function function_exists;
35
use function implode;
36
use function is_a;
37
use function is_string;
38
use function str_replace;
39
use function ucwords;
40

41
/**
42
 * ConfigCache.
43
 *
44
 * @author Elias Häußler <e.haeussler@familie-redlich.de>
45
 * @license GPL-3.0-or-later
46
 *
47
 * @internal
48
 */
49
final class ContainerCache
50
{
51
    private const CONTAINER_NAMESPACE = 'CPSIT\\FrontendAssetHandler\\DependencyInjection';
52

53
    private readonly bool $debug;
54
    private readonly string $cacheDir;
55
    private Config\ConfigCache $cache;
56
    private string $containerClassName;
57

58
    public function __construct(
180✔
59
        private readonly ?string $configFile = null,
60
        private readonly bool $includeTestSources = false,
61
        ?bool $debug = null,
62
        ?string $cacheDir = null,
63
    ) {
64
        $this->debug = $debug ?? $this->isDebugEnabled();
180✔
65
        $this->cacheDir = $cacheDir ?? $this->getDefaultCacheDirectory();
180✔
66

67
        $this->createCache();
180✔
68
    }
69

70
    public function isFresh(): bool
180✔
71
    {
72
        return $this->cache->isFresh();
180✔
73
    }
74

75
    public function get(): ?SymfonyDI\ContainerInterface
180✔
76
    {
77
        if ($this->isFresh()) {
180✔
78
            return $this->doGet();
177✔
79
        }
80

81
        return null;
47✔
82
    }
83

84
    public function flush(): void
45✔
85
    {
86
        $cachePath = $this->cache->getPath();
45✔
87

88
        if (file_exists($cachePath)) {
45✔
89
            unlink($cachePath);
45✔
90
        }
91

92
        $this->createCache();
45✔
93
    }
94

95
    public function flushAll(): void
1✔
96
    {
97
        $filesystem = new Filesystem\Filesystem();
1✔
98
        $filesystem->remove([
1✔
99
            $this->getProjectCacheDirectory(),
1✔
100
            $this->getGlobalCacheDirectory(),
1✔
101
        ]);
1✔
102

103
        $this->createCache();
1✔
104
    }
105

106
    public function write(SymfonyDI\ContainerBuilder $containerBuilder): SymfonyDI\ContainerInterface
47✔
107
    {
108
        if ($this->debug) {
47✔
109
            $containerXmlFilename = basename($this->cache->getPath(), '.php');
47✔
110
            $containerXmlPath = dirname($this->cache->getPath()).'/'.$containerXmlFilename.'.xml';
47✔
111

112
            $containerBuilder->addCompilerPass(
47✔
113
                new DependencyInjection\CompilerPass\ContainerBuilderDebugDumpPass($containerXmlPath),
47✔
114
            );
47✔
115
        }
116

117
        $containerBuilder->compile();
47✔
118

119
        $dumper = new SymfonyDI\Dumper\PhpDumper($containerBuilder);
47✔
120
        $dumpedContainer = $dumper->dump([
47✔
121
            'namespace' => self::CONTAINER_NAMESPACE,
47✔
122
            'class' => $this->containerClassName,
47✔
123
        ]);
47✔
124
        assert(is_string($dumpedContainer));
125

126
        $this->cache->write($dumpedContainer, $containerBuilder->getResources());
47✔
127

128
        return $this->doGet() ?? $containerBuilder;
47✔
129
    }
130

131
    public function getConfigFile(): ?string
30✔
132
    {
133
        return $this->configFile;
30✔
134
    }
135

136
    public function getPath(): string
1✔
137
    {
138
        return $this->cache->getPath();
1✔
139
    }
140

141
    private function doGet(): ?SymfonyDI\ContainerInterface
180✔
142
    {
143
        if (!file_exists($this->cache->getPath())) {
180✔
144
            return null;
×
145
        }
146

147
        require_once $this->cache->getPath();
180✔
148

149
        /** @var class-string $className */
150
        $className = self::CONTAINER_NAMESPACE.'\\'.$this->containerClassName;
180✔
151

152
        // @codeCoverageIgnoreStart
153
        if (!is_a($className, SymfonyDI\ContainerInterface::class, true)) {
154
            throw Exception\UnsupportedClassException::create($className);
155
        }
156
        // @codeCoverageIgnoreEnd
157

158
        return new $className();
180✔
159
    }
160

161
    private function createCache(): void
180✔
162
    {
163
        $basenameComponents = ['container'];
180✔
164

165
        if ($this->includeTestSources) {
180✔
166
            $basenameComponents[] = 'test';
180✔
167
        }
168
        if (null !== $this->configFile) {
180✔
169
            $basenameComponents[] = sha1($this->configFile);
180✔
170
        }
171

172
        $cachePath = Filesystem\Path::join($this->cacheDir, implode('_', $basenameComponents).'.php');
180✔
173

174
        $this->cache = new Config\ConfigCache($cachePath, $this->debug);
180✔
175
        $this->containerClassName = 'FrontendAssetHandler'.str_replace(' ', '', ucwords(implode(' ', $basenameComponents)));
180✔
176
    }
177

178
    private function isDebugEnabled(): bool
×
179
    {
180
        return function_exists('xdebug_break');
×
181
    }
182

183
    private function getDefaultCacheDirectory(): string
180✔
184
    {
185
        if ($this->includeTestSources) {
180✔
186
            return $this->getProjectCacheDirectory();
180✔
187
        }
188

189
        return $this->getGlobalCacheDirectory();
×
190
    }
191

192
    private function getProjectCacheDirectory(): string
180✔
193
    {
194
        return dirname(__DIR__, 3).'/var/cache';
180✔
195
    }
196

197
    private function getGlobalCacheDirectory(): string
1✔
198
    {
199
        return sys_get_temp_dir().'/frontend-asset-handler/cache';
1✔
200
    }
201
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc