Completed
Pull Request — master (#91)
by Vladimir
02:38
created

Filesystem::isVFS()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Filesystem;
9
10
use allejo\stakx\Service;
11
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
12
use Symfony\Component\Filesystem\Exception\IOException;
13
14
/**
15
 * Class Filesystem.
16
 *
17
 * This class extends Symfony's Filesystem to provide convenience functions
18
 */
19
class Filesystem extends \Symfony\Component\Filesystem\Filesystem
20
{
21
    /**
22
     * Build an absolute file or directory path separated by the OS specific directory separator.
23
     *
24
     * @param string ...$pathFragments
25
     *
26
     * @return string
27
     */
28 54
    public function absolutePath($pathFragments)
29
    {
30 54
        if ($pathFragments instanceof FilesystemPath)
31
        {
32 6
            $pathFragments = (string)$pathFragments;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $pathFragments. This often makes code more readable.
Loading history...
33
        }
34
35 54
        if ($this->isAbsolutePath($pathFragments))
36
        {
37 26
            return $pathFragments;
38
        }
39
40 50
        $args = func_get_args();
41 50
        array_unshift($args, Service::getWorkingDirectory());
42
43 50
        return implode(DIRECTORY_SEPARATOR, $args);
44
    }
45
46
    /**
47
     * Build a file or directory path separated by the OS specific directory separator.
48
     *
49
     * @param string ...$pathFragments
50
     *
51
     * @return string
52
     */
53 44
    public function appendPath($pathFragments)
0 ignored issues
show
Unused Code introduced by
The parameter $pathFragments is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
54
    {
55 44
        return implode(DIRECTORY_SEPARATOR, func_get_args());
56
    }
57
58
    /**
59
     * Copy a file or folder recursively.
60
     *
61
     * @param string $originFile          The original filename
62
     * @param string $targetFile          The target filename
63
     * @param bool   $overwriteNewerFiles If true, target files newer than origin files are overwritten
64
     *
65
     * @throws FileNotFoundException When originFile doesn't exist
66
     * @throws IOException           When copy fails
67
     */
68
    public function copy($originFile, $targetFile, $overwriteNewerFiles = false)
69
    {
70
        if ($this->isDir($originFile))
71
        {
72
            if (!$this->isDir($targetFile))
73
            {
74
                mkdir($targetFile, 0755, true);
75
            }
76
77
            $dir = dir($originFile);
78
79
            while (false !== $entry = $dir->read())
80
            {
81
                // Skip pointers
82
                if ($entry == '.' || $entry == '..')
83
                {
84
                    continue;
85
                }
86
87
                $this->copy("$originFile/$entry", "$targetFile/$entry", true);
88
            }
89
90
            $dir->close();
91
        }
92
        else
93
        {
94
            parent::copy($originFile, $targetFile, $overwriteNewerFiles);
95
        }
96
    }
97
98
    /**
99
     * Create an instance of stakx's File object with relative path information.
100
     *
101
     * @param string $filePath
102
     *
103
     * @return File
104
     */
105
    public function createFileObject($filePath)
106
    {
107
        return new File($this->absolutePath($filePath));
108
    }
109
110
    /**
111
     * Strip the current working directory from an absolute path.
112
     *
113
     * @param string $path An absolute path
114
     *
115
     * @return string
116
     */
117 84
    public function getRelativePath($path)
118
    {
119 84
        return str_replace(Service::getWorkingDirectory() . DIRECTORY_SEPARATOR, '', $path);
120
    }
121
122
    /**
123
     * Get the name of a given file without the extension.
124
     *
125
     * @param string $filePath A file path
126
     *
127
     * @return string
128
     */
129 3
    public function getBaseName($filePath)
130
    {
131 3
        return pathinfo($filePath, PATHINFO_FILENAME);
132
    }
133
134
    /**
135
     * Get the name of a given file.
136
     *
137
     * @param string $filePath A file path
138
     *
139
     * @return string
140
     */
141 1
    public function getFileName($filePath)
142
    {
143 1
        return pathinfo($filePath, PATHINFO_BASENAME);
144
    }
145
146
    /**
147
     * Get the parent directory of a given file.
148
     *
149
     * @param string $filePath A file path
150
     *
151
     * @return string
152
     */
153 15
    public function getFolderPath($filePath)
154
    {
155 15
        return pathinfo($filePath, PATHINFO_DIRNAME);
156
    }
157
158
    /**
159
     * Get the extension of a given file.
160
     *
161
     * @param string $filename A file path
162
     *
163
     * @return string The extension of the file
164
     */
165 35
    public function getExtension($filename)
166
    {
167 35
        return pathinfo($filename, PATHINFO_EXTENSION);
168
    }
169
170
    /**
171
     * Check whether or not if a given path is a directory.
172
     *
173
     * @param string $folderPath
174
     *
175
     * @return bool
176
     */
177
    public function isDir($folderPath)
178
    {
179
        return is_dir($folderPath);
180
    }
181
182
    /**
183
     * Check whether or not a given path is a file.
184
     *
185
     * @param string $filePath
186
     *
187
     * @return bool
188
     */
189
    public function isFile($filePath)
190
    {
191
        return is_file($filePath);
192
    }
193
194
    /**
195
     * Check whether a given file path is a symlink.
196
     *
197
     * @param string $filePath
198
     *
199
     * @return bool
200
     */
201
    public function isSymlink($filePath)
202
    {
203
        return is_link($filePath);
204
    }
205
206
    /**
207
     * Get the full path to the file without the extension.
208
     *
209
     * @param string $filename A file path
210
     *
211
     * @return string
212
     */
213 3
    public function removeExtension($filename)
214
    {
215 3
        return $this->appendPath(
216 3
            $this->getFolderPath($filename),
217 3
            $this->getBaseName($filename)
218
        );
219
    }
220
221 6
    public function path($path)
222
    {
223 6
        return new FilesystemPath($path);
224
    }
225
226 2
    public function getInternalResource($name)
227
    {
228 2
        $path = new FilesystemPath(__DIR__ . '/../Resources/' . $name);
229
230 2
        return file_get_contents($path);
231
    }
232
233
    /**
234
     * A vfsStream friendly way of getting the realpath() of something.
235
     *
236
     * @param string $path
237
     *
238
     * @return string
239
     */
240 178
    public function realpath($path)
241
    {
242 178
        return $this->isVFS($path) ? $path : realpath($path);
243
    }
244
245
    /**
246
     * Check whether a given path is on the virtual filesystem.
247
     *
248
     * @param string $path
249
     *
250
     * @return bool
251
     */
252 184
    public function isVFS($path)
253
    {
254 184
        return substr($path, 0, 6) == 'vfs://';
255
    }
256
}
257