Completed
Push — master ( adff0c...fcdb68 )
by Vladimir
02:58
created

Filesystem::absolutePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.5

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 12
ccs 3
cts 6
cp 0.5
crap 2.5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file contains an extended Filesystem class.
5
 *
6
 * This file is part of the Stakx project.
7
 *
8
 * @copyright 2016 Vladimir Jimenez
9
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md
10
 */
11
12
namespace allejo\stakx\System;
13
14
use Symfony\Component\Filesystem\Exception\IOException;
15
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
16
use Symfony\Component\Finder\Finder;
17
use Symfony\Component\Finder\SplFileInfo;
18
19
/**
20
 * Class Filesystem
21
 *
22
 * This class extends Symfony's Filesystem to provide convenience functions
23
 *
24
 * @package allejo\stakx\Environment
25
 */
26
class Filesystem extends \Symfony\Component\Filesystem\Filesystem
27
{
28
    /**
29
     * Build an absolute file or directory path separated by the OS specific directory separator
30
     *
31
     * @param string ...$pathFragments
32
     *
33
     * @return string
34
     */
35 4
    public function absolutePath ($pathFragments)
36
    {
37 4
        if ($this->isAbsolutePath($pathFragments))
38
        {
39 4
            return $pathFragments;
40
        }
41
42
        $args = func_get_args();
43
        array_unshift($args, getcwd());
44
45
        return implode(DIRECTORY_SEPARATOR, $args);
46
    }
47
48
    /**
49
     * Build a file or directory path separated by the OS specific directory separator
50
     *
51
     * @param string ...$pathFragments
52
     *
53
     * @return string
54
     */
55 3
    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...
56
    {
57 3
        return implode(DIRECTORY_SEPARATOR, func_get_args());
58
    }
59
60
    /**
61
     * Copy a file or folder recursively
62
     *
63
     * @param string $originFile          The original filename
64
     * @param string $targetFile          The target filename
65
     * @param bool   $overwriteNewerFiles If true, target files newer than origin files are overwritten
66
     *
67
     * @throws FileNotFoundException When originFile doesn't exist
68
     * @throws IOException           When copy fails
69
     */
70
    public function copy($originFile, $targetFile, $overwriteNewerFiles = false)
71
    {
72
        if ($this->isDir($originFile))
73
        {
74
            if (!$this->isDir($targetFile))
75
            {
76
                mkdir($targetFile, 0755, true);
77
            }
78
79
            $dir = dir($originFile);
80
81
            while (false !== $entry = $dir->read())
82
            {
83
                // Skip pointers
84
                if ($entry == '.' || $entry == '..') { continue; }
85
86
                $this->copy("$originFile/$entry", "$targetFile/$entry", true);
87
            }
88
89
            $dir->close();
90
        }
91
        else
92
        {
93
            parent::copy($originFile, $targetFile, $overwriteNewerFiles);
94
        }
95
    }
96
97
    /**
98
     * Strip the current working directory from an absolute path
99
     *
100
     * @param  string $path An absolute path
101
     *
102
     * @return string
103
     */
104 4
    public function getRelativePath ($path)
105
    {
106 4
        return str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $path);
107
    }
108
109 4
    public function getFinder ($explicitIncludes = array(), $explicitIgnores = array(), $searchIn = "")
110
    {
111 4
        $finder = new Finder();
112 4
        $finder->files()
113 4
               ->ignoreVCS(true)
114 4
               ->ignoreDotFiles(true)
115 4
               ->ignoreUnreadableDirs();
116
117 4
        $finder->in(
118 4
            empty(trim($searchIn)) ? getcwd() : $searchIn
119
        );
120
121 4
        foreach ($explicitIgnores as $ignoreRule)
122
        {
123
            $isRegex = @preg_match($ignoreRule, null);
124
125
            if (substr($ignoreRule, -1, 1) === '/' || $isRegex !== false)
126
            {
127
                $finder->notPath($ignoreRule);
128
            }
129
            else
130
            {
131
                $finder->notName($ignoreRule);
132
            }
133
        }
134
135 4
        if (count($explicitIncludes) > 0)
136
        {
137
            foreach ($explicitIncludes as &$include)
138
            {
139
                $include = new SplFileInfo($include, $this->getFolderPath($include), $include);
140
            }
141
142
            $finder->append($explicitIncludes);
143
        }
144
145 4
        return $finder;
146
    }
147
148
    /**
149
     * Get the name of a given file without the extension
150
     *
151
     * @param  string $filePath A file path
152
     *
153
     * @return string
154
     */
155 7
    public function getBaseName ($filePath)
156
    {
157 7
        return pathinfo($filePath, PATHINFO_FILENAME);
158
    }
159
160
    /**
161
     * Get the name of a given file
162
     *
163
     * @param  string $filePath A file path
164
     *
165
     * @return string
166
     */
167 2
    public function getFileName ($filePath)
168
    {
169 2
        return pathinfo($filePath, PATHINFO_BASENAME);
170
    }
171
172
    /**
173
     * Get the parent directory of a given file
174
     *
175
     * @param  string $filePath A file path
176
     *
177
     * @return string
178
     */
179 3
    public function getFolderPath ($filePath)
180
    {
181 3
        return pathinfo($filePath, PATHINFO_DIRNAME);
182
    }
183
184
    /**
185
     * Get the extension of a given file
186
     *
187
     * @param  string $filename A file path
188
     *
189
     * @return string The extension of the file
190
     */
191 27
    public function getExtension ($filename)
192
    {
193 27
        return pathinfo($filename, PATHINFO_EXTENSION);
194
    }
195
196
    /**
197
     * @param string $folderPath
198
     */
199
    public function isDir ($folderPath)
200
    {
201
        return is_dir($folderPath);
202
    }
203
204
    /**
205
     * Get the full path to the file without the extension
206
     *
207
     * @param  string $filename A file path
208
     *
209
     * @return string
210
     */
211 3
    public function removeExtension ($filename)
212
    {
213 3
        return $this->appendPath(
214 3
                   $this->getFolderPath($filename),
215 3
                   $this->getBaseName($filename)
216
               );
217
    }
218
219
    /**
220
     * Write a file
221
     *
222
     * @param string $targetDir The directory of where the file will be created; the file name is a separate variable
223
     * @param string $fileName  The name of the file
224
     * @param string $content   The content that belongs in the file
225
     *
226
     * @return SplFileInfo A reference to the newly created file
227
     */
228 View Code Duplication
    public function writeFile ($targetDir, $fileName, $content)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
229
    {
230
        $outputFolder = $this->getFolderPath($this->absolutePath($targetDir, $fileName));
231
        $targetFile   = $this->getFileName($fileName);
232
233
        if (!file_exists($outputFolder))
234
        {
235
            mkdir($outputFolder, 0755, true);
236
        }
237
238
        file_put_contents($this->appendPath($outputFolder, $targetFile), $content, LOCK_EX);
239
240
        return (new SplFileInfo(
241
            $fileName,
242
            $this->absolutePath($targetDir),
243
            $this->absolutePath($targetDir, $fileName))
244
        );
245
    }
246
}