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

Cecilapp / Cecil / 14620241211

23 Apr 2025 02:06PM UTC coverage: 83.787%. First build
14620241211

Pull #2148

github

web-flow
Merge 12fc09dec into 6d7ba8f0a
Pull Request #2148: refactor: configuration and cache

361 of 423 new or added lines in 26 files covered. (85.34%)

3049 of 3639 relevant lines covered (83.79%)

0.84 hits per line

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

74.65
/src/Util/File.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <arnaud@ligny.fr>
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 Cecil\Util;
15

16
use Cecil\Exception\RuntimeException;
17
use Symfony\Component\Filesystem\Filesystem;
18
use Symfony\Component\Mime\MimeTypes;
19

20
class File
21
{
22
    /** @var Filesystem */
23
    protected static $fs;
24

25
    /**
26
     * Returns a Symfony\Component\Filesystem instance.
27
     */
28
    public static function getFS(): Filesystem
29
    {
30
        if (!self::$fs instanceof Filesystem) {
1✔
31
            self::$fs = new Filesystem();
1✔
32
        }
33

34
        return self::$fs;
1✔
35
    }
36

37
    /**
38
     * file_get_contents() function with error handler.
39
     *
40
     * @return string|false
41
     */
42
    public static function fileGetContents(string $filename, bool $userAgent = false)
43
    {
44
        if (empty($filename)) {
1✔
45
            return false;
×
46
        }
47

48
        set_error_handler(
1✔
49
            function ($severity, $message, $file, $line) {
1✔
50
                throw new \ErrorException($message, 0, $severity, $file, $line, null);
1✔
51
            }
1✔
52
        );
1✔
53

54
        try {
55
            if ($userAgent) {
1✔
56
                $options = [
1✔
57
                    'http' => [
1✔
58
                        'method'          => 'GET',
1✔
59
                        'header'          => 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.47 Safari/537.36',
1✔
60
                        'follow_location' => true,
1✔
61
                    ],
1✔
62
                ];
1✔
63

64
                return file_get_contents($filename, false, stream_context_create($options));
1✔
65
            }
66

67
            return file_get_contents($filename);
1✔
68
        } catch (\ErrorException) {
1✔
69
            return false;
1✔
70
        } finally {
71
            restore_error_handler();
1✔
72
        }
73
    }
74

75
    /**
76
     * Returns the media type and subtype of a file.
77
     *
78
     * ie: ['text', 'text/plain']
79
     */
80
    public static function getMediaType(string $filename): array
81
    {
82
        try {
83
            if (false !== $subtype = mime_content_type($filename)) {
1✔
84
                return [explode('/', $subtype)[0], $subtype];
1✔
85
            }
NEW
86
            $mimeTypes = new MimeTypes();
×
NEW
87
            $subtype = $mimeTypes->guessMimeType($filename);
×
NEW
88
            if ($subtype === null) {
×
NEW
89
                throw new RuntimeException('Can\'t guess the media type.');
×
90
            }
91

NEW
92
            return [explode('/', $subtype)[0], $subtype];
×
NEW
93
        } catch (\Exception $e) {
×
NEW
94
            throw new RuntimeException(\sprintf('Can\'t get media type of "%s" (%s).', $filename, $e->getMessage()));
×
95
        }
96
    }
97

98
    /**
99
     * Returns the extension of a file.
100
     */
101
    public static function getExtension(string $filename): string
102
    {
103
        try {
104
            $ext = pathinfo($filename, PATHINFO_EXTENSION);
1✔
105
            if (!empty($ext)) {
1✔
106
                return $ext;
1✔
107
            }
108
            // guess the extension
109
            $mimeTypes = new MimeTypes();
1✔
110
            $mimeType = $mimeTypes->guessMimeType($filename);
1✔
111
            if ($mimeType === null) {
1✔
NEW
112
                throw new RuntimeException('Can\'t guess the media type.');
×
113
            }
114
            $exts = $mimeTypes->getExtensions($mimeType);
1✔
115

116
            return $exts[0];
1✔
NEW
117
        } catch (\Exception $e) {
×
NEW
118
            throw new RuntimeException(\sprintf('Can\'t get extension of "%s" (%s).', $filename, $e->getMessage()));
×
119
        }
120
    }
121

122
    /**
123
     * exif_read_data() function with error handler.
124
     */
125
    public static function readExif(string $filename): array
126
    {
127
        if (empty($filename)) {
1✔
128
            return [];
×
129
        }
130

131
        set_error_handler(
1✔
132
            function ($severity, $message, $file, $line) {
1✔
133
                throw new \ErrorException($message, 0, $severity, $file, $line, null);
×
134
            }
1✔
135
        );
1✔
136

137
        try {
138
            if (!\function_exists('exif_read_data')) {
1✔
139
                throw new \ErrorException('`exif` extension is not available.');
×
140
            }
141
            $exif = exif_read_data($filename, null, true);
1✔
142
            if ($exif === false) {
1✔
143
                return [];
×
144
            }
145

146
            return $exif;
1✔
147
        } catch (\ErrorException) {
×
148
            return [];
×
149
        } finally {
150
            restore_error_handler();
1✔
151
        }
152
    }
153

154
    /**
155
     * Returns the real path of a relative file path.
156
     */
157
    public static function getRealPath(string $path): string
158
    {
159
        // if file exists
160
        $filePath = realpath(\Cecil\Util::joinFile(__DIR__, '/../', $path));
1✔
161
        if ($filePath !== false) {
1✔
162
            return $filePath;
1✔
163
        }
164
        // if Phar
165
        if (Platform::isPhar()) {
1✔
166
            return \Cecil\Util::joinPath(Platform::getPharPath(), str_replace('../', '/', $path));
×
167
        }
168

169
        throw new RuntimeException(\sprintf('Can\'t get the real path of file "%s".', $path));
1✔
170
    }
171

172
    /**
173
     * Tests if a file path is remote.
174
     */
175
    public static function isRemote(string $path): bool
176
    {
177
        return (bool) preg_match('~^(?:f|ht)tps?://~i', $path);
1✔
178
    }
179

180
    /**
181
     * Tests if a remote file exists.
182
     */
183
    public static function isRemoteExists(string $path): bool
184
    {
185
        if (self::isRemote($path)) {
1✔
186
            $handle = @fopen($path, 'r');
1✔
187
            if (\is_resource($handle)) {
1✔
188
                return true;
1✔
189
            }
190
        }
191

192
        return false;
1✔
193
    }
194
}
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