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 FilesystemPath */ |
19
|
|
|
private $folder; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string $folderPath |
23
|
|
|
*/ |
24
|
12 |
|
public function __construct($folderPath) |
25
|
|
|
{ |
26
|
12 |
|
$this->folder = new FilesystemPath($folderPath); |
27
|
|
|
|
28
|
12 |
|
if (!$this->folder->isDir()) |
29
|
12 |
|
{ |
30
|
|
|
throw new FileNotFoundException(sprintf('The folder could not be found: %s', $folderPath)); |
31
|
|
|
} |
32
|
12 |
|
} |
33
|
|
|
|
34
|
|
|
public function __toString() |
35
|
|
|
{ |
36
|
|
|
return $this->folder->getAbsolutePath(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set a base folder that will be prefixed before all file writes and copies. |
41
|
|
|
* |
42
|
|
|
* @param string $folderName |
43
|
|
|
* |
44
|
|
|
* @since 0.1.0 |
45
|
|
|
*/ |
46
|
|
|
public function setTargetDirectory($folderName) |
47
|
|
|
{ |
48
|
|
|
if ($folderName === null || empty($folderName)) |
49
|
|
|
{ |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->folder->appendToPath($folderName); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Copy a file from an absolute file to a path relative to the Folder's location. |
58
|
|
|
* |
59
|
|
|
* @param string $absolutePath The absolute path of the file |
60
|
|
|
* @param string $targetPath The relative file path to the Folder's location |
61
|
|
|
* |
62
|
|
|
* @since 0.1.0 |
63
|
|
|
*/ |
64
|
|
|
public function copyFile($absolutePath, $targetPath) |
65
|
|
|
{ |
66
|
|
|
$targetPath = $this->folder->generatePath($targetPath); |
|
|
|
|
67
|
|
|
|
68
|
|
|
fs::copy($absolutePath, $targetPath, true); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Write a file with the specified content. |
73
|
|
|
* |
74
|
|
|
* @param string $relativePath The file path relative to this Folder's location |
75
|
|
|
* @param string $content The content that will be written to the file |
76
|
|
|
* |
77
|
|
|
* @since 0.1.0 |
78
|
|
|
* |
79
|
|
|
* @return File |
80
|
|
|
*/ |
81
|
12 |
|
public function writeFile($relativePath, $content) |
82
|
|
|
{ |
83
|
12 |
|
$targetFile = $this->folder->generatePath($relativePath); |
84
|
12 |
|
$targetFolderPath = fs::getFolderPath($targetFile); |
|
|
|
|
85
|
|
|
|
86
|
12 |
|
if (!file_exists($targetFolderPath)) |
87
|
12 |
|
{ |
88
|
9 |
|
mkdir($targetFolderPath, 0755, true); |
89
|
9 |
|
} |
90
|
|
|
|
91
|
12 |
|
file_put_contents($targetFile, $content); |
92
|
|
|
|
93
|
12 |
|
return new File($targetFile); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|