• 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

94.87
/src/Helper/FilesystemHelper.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\Helper;
25

26
use Composer\InstalledVersions;
27
use CPSIT\FrontendAssetHandler\Exception;
28
use Ergebnis\Json;
29
use OutOfBoundsException;
30
use Phar;
31
use Symfony\Component\Filesystem;
32

33
use function getcwd;
34
use function ltrim;
35
use function pathinfo;
36
use function register_shutdown_function;
37
use function str_ends_with;
38
use function strtolower;
39
use function substr;
40

41
/**
42
 * FilesystemHelper.
43
 *
44
 * @author Elias Häußler <e.haeussler@familie-redlich.de>
45
 * @license GPL-3.0-or-later
46
 *
47
 * @api
48
 */
49
final class FilesystemHelper
50
{
51
    private static ?Filesystem\Filesystem $filesystem = null;
52

53
    /**
54
     * @var list<string>
55
     */
56
    private static array $tempFiles = [];
57

58
    public static function getProjectDirectory(): string
1✔
59
    {
60
        $projectDirectory = null;
1✔
61

62
        try {
63
            $projectDirectory = InstalledVersions::getInstallPath('cpsit/frontend-asset-handler');
1✔
64
        } catch (OutOfBoundsException) {
×
65
            // Intentionally left blank.
66
        }
67

68
        // @codeCoverageIgnoreStart
69
        if (null === $projectDirectory) {
70
            throw Exception\FilesystemFailureException::forUnresolvableProjectDirectory();
71
        }
72
        // @codeCoverageIgnoreEnd
73

74
        return Filesystem\Path::canonicalize($projectDirectory);
1✔
75
    }
76

77
    public static function getWorkingDirectory(): string
13✔
78
    {
79
        if ('' !== Phar::running()) {
13✔
80
            $cwd = getcwd();
×
81
        } else {
82
            $cwd = InstalledVersions::getRootPackage()['install_path'];
13✔
83
        }
84

85
        // @codeCoverageIgnoreStart
86
        if (false === $cwd) {
87
            throw Exception\FilesystemFailureException::forUnresolvableWorkingDirectory();
88
        }
89
        // @codeCoverageIgnoreEnd
90

91
        return Filesystem\Path::canonicalize($cwd);
13✔
92
    }
93

94
    public static function resolveRelativePath(string $relativePath): string
198✔
95
    {
96
        $filesystem = self::getFilesystem();
198✔
97

98
        if ($filesystem->isAbsolutePath($relativePath)) {
198✔
99
            return $relativePath;
196✔
100
        }
101

102
        return Filesystem\Path::join(self::getWorkingDirectory(), $relativePath);
6✔
103
    }
104

105
    public static function parseJsonFileContents(string $filePath): Json\Json
64✔
106
    {
107
        if (!file_exists($filePath)) {
64✔
108
            throw Exception\FilesystemFailureException::forMissingPath($filePath);
2✔
109
        }
110

111
        $encoded = (string) file_get_contents($filePath);
62✔
112

113
        return Json\Json::fromString($encoded);
62✔
114
    }
115

116
    public static function createTemporaryFile(string $extension = '', bool $filenameOnly = false): string
18✔
117
    {
118
        $filesystem = self::getFilesystem();
18✔
119

120
        // Remove all temporary files on shutdown
121
        if ([] === self::$tempFiles) {
18✔
122
            register_shutdown_function(fn () => $filesystem->remove(self::$tempFiles));
1✔
123
        }
124

125
        // Create temporary file
126
        $extension = ltrim($extension, '.');
18✔
127
        $tempFile = $filesystem->tempnam(sys_get_temp_dir(), 'frontend_asset_handler_', '' !== $extension ? '.'.$extension : '');
18✔
128

129
        if ($filenameOnly) {
18✔
130
            // Remove file if only filename should be returned
131
            $filesystem->remove($tempFile);
6✔
132
        } else {
133
            // Register temporary file to be removed on shutdown
134
            self::$tempFiles[] = $tempFile;
12✔
135
        }
136

137
        return $tempFile;
18✔
138
    }
139

140
    public static function getFileExtension(string $path): string
12✔
141
    {
142
        $normalizedPath = strtolower($path);
12✔
143

144
        if (str_ends_with($normalizedPath, '.gz')) {
12✔
145
            return pathinfo(substr($normalizedPath, 0, -3), PATHINFO_EXTENSION).'.gz';
10✔
146
        }
147

148
        return pathinfo($normalizedPath, PATHINFO_EXTENSION);
2✔
149
    }
150

151
    private static function getFilesystem(): Filesystem\Filesystem
202✔
152
    {
153
        if (null === self::$filesystem) {
202✔
154
            self::$filesystem = new Filesystem\Filesystem();
1✔
155
        }
156

157
        return self::$filesystem;
202✔
158
    }
159
}
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

© 2026 Coveralls, Inc