-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathStorage.php
More file actions
119 lines (98 loc) · 2.96 KB
/
Storage.php
File metadata and controls
119 lines (98 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
declare(strict_types=1);
namespace Saloon\Helpers;
use Saloon\Exceptions\DirectoryNotFoundException;
use Saloon\Exceptions\UnableToCreateFileException;
use Saloon\Exceptions\UnableToCreateDirectoryException;
/**
* @internal
*/
class Storage
{
/**
* The base directory to access the files.
*/
protected string $baseDirectory;
/**
* Constructor
*
* @throws \Saloon\Exceptions\DirectoryNotFoundException
* @throws \Saloon\Exceptions\UnableToCreateDirectoryException
*/
public function __construct(string $baseDirectory, bool $createMissingBaseDirectory = false)
{
if (! is_dir($baseDirectory)) {
$createMissingBaseDirectory ? $this->createDirectory($baseDirectory) : throw new DirectoryNotFoundException($baseDirectory);
}
$this->baseDirectory = $baseDirectory;
}
/**
* Get the base directory
*/
public function getBaseDirectory(): string
{
return $this->baseDirectory;
}
/**
* Combine the base directory with a path.
*/
protected function buildPath(string $path): string
{
$trimRules = DIRECTORY_SEPARATOR . ' ';
return mb_rtrim($this->baseDirectory, $trimRules) . DIRECTORY_SEPARATOR . mb_ltrim($path, $trimRules);
}
/**
* Check if the file exists
*/
public function exists(string $path): bool
{
return file_exists($this->buildPath($path));
}
/**
* Check if the file is missing
*/
public function missing(string $path): bool
{
return ! $this->exists($path);
}
/**
* Retrieve an item from storage
*/
public function get(string $path): bool|string
{
return file_get_contents($this->buildPath($path));
}
/**
* Put an item in storage
*
* @return $this
* @throws \Saloon\Exceptions\UnableToCreateDirectoryException
* @throws \Saloon\Exceptions\UnableToCreateFileException
*/
public function put(string $path, string $contents): static
{
$fullPath = $this->buildPath($path);
$directoryWithoutFilename = implode(DIRECTORY_SEPARATOR, explode(DIRECTORY_SEPARATOR, $fullPath, -1));
if (empty($directoryWithoutFilename) === false && is_dir($directoryWithoutFilename) === false) {
$this->createDirectory($directoryWithoutFilename);
}
$createdFile = file_put_contents($fullPath, $contents);
if ($createdFile === false) {
throw new UnableToCreateFileException($fullPath);
}
return $this;
}
/**
* Create a directory
*
* @throws \Saloon\Exceptions\UnableToCreateDirectoryException
*/
public function createDirectory(string $directory): bool
{
$createdDirectory = mkdir($directory, 0777, true);
if ($createdDirectory === false && is_dir($directory) === false) {
throw new UnableToCreateDirectoryException($directory);
}
return true;
}
}