|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright 2017 Vladimir Jimenez |
|
5
|
|
|
* @license https://github.com/allejo/stakx/blob/master/LICENSE.md MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace allejo\stakx\System; |
|
9
|
|
|
|
|
10
|
|
|
use Symfony\Component\Filesystem\Exception\FileNotFoundException; |
|
11
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* All folder paths stored inside of this class will **not** have the ending DIRECTORY_SEPARATOR. |
|
15
|
|
|
* |
|
16
|
|
|
* @internal |
|
17
|
|
|
*/ |
|
18
|
|
|
class Folder |
|
19
|
|
|
{ |
|
20
|
|
|
protected $fs; |
|
|
|
|
|
|
21
|
|
|
protected $absolutePath; |
|
22
|
|
|
protected $targetDirectories; |
|
23
|
|
|
|
|
24
|
13 |
|
public function __construct($folderPath) |
|
25
|
|
|
{ |
|
26
|
13 |
|
$this->fs = new Filesystem(); |
|
|
|
|
|
|
27
|
13 |
|
$this->targetDirectories = array(); |
|
28
|
|
|
|
|
29
|
|
|
// Setup the absolute path to the directory |
|
30
|
13 |
|
if (substr($folderPath, 0, 1) === DIRECTORY_SEPARATOR) |
|
31
|
13 |
|
{ |
|
32
|
|
|
$this->absolutePath = $folderPath; |
|
33
|
|
|
} |
|
34
|
|
|
else |
|
35
|
|
|
{ |
|
36
|
13 |
|
$this->absolutePath = $this->fs->absolutePath($folderPath); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// Ensure our directory paths will don't end with a '/' |
|
40
|
13 |
|
$this->absolutePath = rtrim($this->absolutePath, DIRECTORY_SEPARATOR); |
|
41
|
|
|
|
|
42
|
13 |
|
if (!$this->fs->isDir($this->absolutePath)) |
|
43
|
13 |
|
{ |
|
44
|
|
|
throw new \InvalidArgumentException(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
13 |
|
if (!$this->fs->exists($this->absolutePath)) |
|
48
|
13 |
|
{ |
|
49
|
|
|
throw new FileNotFoundException(); |
|
50
|
|
|
} |
|
51
|
13 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function __toString() |
|
54
|
|
|
{ |
|
55
|
|
|
return rtrim($this->absolutePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Set a base folder that will be prefixed before all file writes and copies. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $folderName |
|
62
|
|
|
* |
|
63
|
|
|
* @since 0.1.0 |
|
64
|
|
|
*/ |
|
65
|
|
|
public function setTargetDirectory($folderName) |
|
66
|
|
|
{ |
|
67
|
|
|
if (is_null($folderName) || empty($folderName)) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->targetDirectories = array(); |
|
70
|
|
|
} |
|
71
|
|
|
else |
|
72
|
|
|
{ |
|
73
|
|
|
$this->targetDirectories[] = trim($folderName, DIRECTORY_SEPARATOR); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Copy a file from an absolute file to a path relative to the Folder's location. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $absolutePath The absolute path of the file |
|
81
|
|
|
* @param string $targetPath The relative file path to the Folder's location |
|
82
|
|
|
* |
|
83
|
|
|
* @since 0.1.0 |
|
84
|
|
|
*/ |
|
85
|
|
|
public function copyFile($absolutePath, $targetPath) |
|
86
|
|
|
{ |
|
87
|
|
|
$targetPath = ltrim($targetPath, DIRECTORY_SEPARATOR); |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
$this->fs->copy( |
|
90
|
|
|
$absolutePath, |
|
91
|
|
|
$this->buildPath($this->getCwd(), $targetPath), |
|
92
|
|
|
true |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Write a file with the specified content. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $relativePath The file path relative to this Folder's location |
|
100
|
|
|
* @param string $content The content that will be written to the file |
|
101
|
|
|
* |
|
102
|
|
|
* @since 0.1.0 |
|
103
|
|
|
* |
|
104
|
|
|
* @return SplFileInfo |
|
105
|
|
|
*/ |
|
106
|
13 |
View Code Duplication |
public function writeFile($relativePath, $content) |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
13 |
|
$outputFolder = $this->fs->getFolderPath($relativePath); |
|
|
|
|
|
|
109
|
13 |
|
$targetFileName = $this->fs->getFileName($outputFolder); |
|
110
|
|
|
|
|
111
|
13 |
|
$absoluteFolderPath = $this->buildPath($this->getCwd(), $outputFolder); |
|
112
|
|
|
|
|
113
|
13 |
|
if (!file_exists($absoluteFolderPath)) |
|
114
|
13 |
|
{ |
|
115
|
10 |
|
mkdir($absoluteFolderPath, 0755, true); |
|
116
|
10 |
|
} |
|
117
|
|
|
|
|
118
|
13 |
|
file_put_contents( |
|
119
|
13 |
|
$this->buildPath($this->getCwd(), $relativePath), |
|
120
|
|
|
$content |
|
121
|
13 |
|
); |
|
122
|
|
|
|
|
123
|
13 |
|
return (new SplFileInfo( |
|
124
|
13 |
|
$targetFileName, |
|
125
|
13 |
|
$absoluteFolderPath, |
|
126
|
13 |
|
$this->buildPath($absoluteFolderPath, $targetFileName) |
|
127
|
13 |
|
)); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param string $pathFragments |
|
132
|
|
|
* |
|
133
|
|
|
* @return string |
|
134
|
|
|
*/ |
|
135
|
13 |
|
private function buildPath($pathFragments) |
|
|
|
|
|
|
136
|
|
|
{ |
|
137
|
13 |
|
$paths = func_get_args(); |
|
138
|
|
|
|
|
139
|
13 |
|
return implode(DIRECTORY_SEPARATOR, $paths); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Returns the absolute path of where files will be placed. |
|
144
|
|
|
* |
|
145
|
|
|
* @return string |
|
146
|
|
|
*/ |
|
147
|
13 |
|
private function getCwd() |
|
148
|
|
|
{ |
|
149
|
13 |
|
$location = array_merge(array($this->absolutePath), $this->targetDirectories); |
|
150
|
|
|
|
|
151
|
13 |
|
return implode(DIRECTORY_SEPARATOR, $location); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.