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\Filesystem\FilesystemLoader as fs; |
11
|
|
|
use Symfony\Component\Filesystem\Exception\FileNotFoundException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A representation of a folder on a given filesystem. |
15
|
|
|
*/ |
16
|
|
|
final class Folder |
17
|
|
|
{ |
18
|
|
|
/** @var bool */ |
19
|
|
|
private $frozen; |
20
|
|
|
|
21
|
|
|
/** @var FilesystemPath */ |
22
|
|
|
private $folder; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $folderPath |
26
|
|
|
*/ |
27
|
13 |
|
public function __construct($folderPath) |
28
|
|
|
{ |
29
|
13 |
|
$this->frozen = false; |
30
|
13 |
|
$this->folder = new FilesystemPath($folderPath); |
31
|
|
|
|
32
|
13 |
|
if (!$this->folder->isDir()) |
33
|
|
|
{ |
34
|
|
|
throw new FileNotFoundException(sprintf('The folder could not be found: %s', $folderPath)); |
35
|
|
|
} |
36
|
13 |
|
} |
37
|
|
|
|
38
|
1 |
|
public function __toString() |
39
|
|
|
{ |
40
|
1 |
|
return $this->folder->getAbsolutePath(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get the file path to this Folder in an OOP friendly way. |
45
|
|
|
* |
46
|
|
|
* @return FilesystemPath |
47
|
|
|
*/ |
48
|
1 |
|
public function getFilesystemPath() |
49
|
|
|
{ |
50
|
1 |
|
return new FilesystemPath($this->__toString()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Set this Folder to a "frozen" state meaning its path can no longer be modified. |
55
|
|
|
*/ |
56
|
1 |
|
public function freeze() |
57
|
|
|
{ |
58
|
1 |
|
$this->frozen = true; |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Check whether or not this Folder's path has been frozen. |
63
|
|
|
* |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
public function isFrozen() |
67
|
|
|
{ |
68
|
|
|
return $this->frozen; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Set a base folder that will be prefixed before all file writes and copies. |
73
|
|
|
* |
74
|
|
|
* @param string $folderName |
75
|
|
|
* |
76
|
|
|
* @since 0.2.0 An \Exception is thrown when a frozen Folder is attempted to |
77
|
|
|
* be modified |
78
|
|
|
* @since 0.1.0 |
79
|
|
|
* |
80
|
|
|
* @throws \Exception |
81
|
|
|
*/ |
82
|
|
|
public function setTargetDirectory($folderName) |
83
|
|
|
{ |
84
|
|
|
if ($this->isFrozen()) |
85
|
|
|
{ |
86
|
|
|
throw new \Exception('A frozen folder object cannot be modified.'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($folderName === null || empty($folderName)) |
90
|
|
|
{ |
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->folder->appendToPath($folderName); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Copy a file from an absolute file to a path relative to the Folder's location. |
99
|
|
|
* |
100
|
|
|
* @param string $absolutePath The absolute path of the file |
101
|
|
|
* @param string $targetPath The relative file path to the Folder's location |
102
|
|
|
* |
103
|
|
|
* @since 0.1.0 |
104
|
|
|
*/ |
105
|
|
|
public function copyFile($absolutePath, $targetPath) |
106
|
|
|
{ |
107
|
|
|
$targetPath = $this->folder->generatePath($targetPath); |
|
|
|
|
108
|
|
|
|
109
|
|
|
fs::copy($absolutePath, $targetPath, true); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Write a file with the specified content. |
114
|
|
|
* |
115
|
|
|
* @param string $relativePath The file path relative to this Folder's location |
116
|
|
|
* @param string $content The content that will be written to the file |
117
|
|
|
* |
118
|
|
|
* @since 0.1.0 |
119
|
|
|
* |
120
|
|
|
* @return File |
121
|
|
|
*/ |
122
|
12 |
|
public function writeFile($relativePath, $content) |
123
|
|
|
{ |
124
|
12 |
|
$targetFile = $this->folder->generatePath($relativePath); |
125
|
12 |
|
$targetFolderPath = fs::getFolderPath($targetFile); |
|
|
|
|
126
|
|
|
|
127
|
12 |
|
if (!file_exists($targetFolderPath)) |
128
|
|
|
{ |
129
|
9 |
|
mkdir($targetFolderPath, 0755, true); |
130
|
|
|
} |
131
|
|
|
|
132
|
12 |
|
file_put_contents($targetFile, $content); |
133
|
|
|
|
134
|
12 |
|
return new File($targetFile); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|