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
|
12 |
|
/** |
25
|
|
|
* @param string $folderPath |
26
|
12 |
|
*/ |
27
|
|
|
public function __construct($folderPath) |
28
|
12 |
|
{ |
29
|
|
|
$this->frozen = false; |
30
|
|
|
$this->folder = new FilesystemPath($folderPath); |
31
|
|
|
|
32
|
12 |
|
if (!$this->folder->isDir()) |
33
|
|
|
{ |
34
|
|
|
throw new FileNotFoundException(sprintf('The folder could not be found: %s', $folderPath)); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function __toString() |
39
|
|
|
{ |
40
|
|
|
return (string)$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
|
|
|
public function getPath() |
49
|
|
|
{ |
50
|
|
|
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
|
|
|
public function freeze() |
57
|
|
|
{ |
58
|
|
|
$this->frozen = true; |
59
|
|
|
} |
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 Support for freezing was added |
77
|
|
|
* @since 0.1.0 |
78
|
|
|
* |
79
|
|
|
* @throws \Exception |
80
|
|
|
*/ |
81
|
12 |
|
public function setTargetDirectory($folderName) |
82
|
|
|
{ |
83
|
12 |
|
if ($this->isFrozen()) |
84
|
12 |
|
{ |
85
|
|
|
throw new \Exception('A frozen folder object cannot be modified.'); |
86
|
12 |
|
} |
87
|
|
|
|
88
|
9 |
|
if ($folderName === null || empty($folderName)) |
89
|
|
|
{ |
90
|
|
|
return; |
91
|
12 |
|
} |
92
|
|
|
|
93
|
12 |
|
$this->folder->appendToPath($folderName); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Copy a file from an absolute file to a path relative to the Folder's location. |
98
|
|
|
* |
99
|
|
|
* @param string $absolutePath The absolute path of the file |
100
|
|
|
* @param string $targetPath The relative file path to the Folder's location |
101
|
|
|
* |
102
|
|
|
* @since 0.1.0 |
103
|
|
|
*/ |
104
|
|
|
public function copyFile($absolutePath, $targetPath) |
105
|
|
|
{ |
106
|
|
|
$targetPath = $this->folder->generatePath($targetPath); |
|
|
|
|
107
|
|
|
|
108
|
|
|
fs::copy($absolutePath, $targetPath, true); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Write a file with the specified content. |
113
|
|
|
* |
114
|
|
|
* @param string $relativePath The file path relative to this Folder's location |
115
|
|
|
* @param string $content The content that will be written to the file |
116
|
|
|
* |
117
|
|
|
* @since 0.1.0 |
118
|
|
|
* |
119
|
|
|
* @return File |
120
|
|
|
*/ |
121
|
|
|
public function writeFile($relativePath, $content) |
122
|
|
|
{ |
123
|
|
|
$targetFile = $this->folder->generatePath($relativePath); |
124
|
|
|
$targetFolderPath = fs::getFolderPath($targetFile); |
|
|
|
|
125
|
|
|
|
126
|
|
|
if (!file_exists($targetFolderPath)) |
127
|
|
|
{ |
128
|
|
|
mkdir($targetFolderPath, 0755, true); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
file_put_contents($targetFile, $content); |
132
|
|
|
|
133
|
|
|
return new File($targetFile); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|