|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of CaptainHook |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace CaptainHook\App\Storage; |
|
13
|
|
|
|
|
14
|
|
|
use RuntimeException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class File |
|
18
|
|
|
* |
|
19
|
|
|
* @package CaptainHook |
|
20
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
21
|
|
|
* @link https://github.com/captainhook-git/captainhook |
|
22
|
|
|
* @since Class available since Release 0.9.0 |
|
23
|
|
|
*/ |
|
24
|
|
|
class File |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Path to file |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected string $path; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* File constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $path |
|
37
|
|
|
*/ |
|
38
|
91 |
|
public function __construct(string $path) |
|
39
|
|
|
{ |
|
40
|
91 |
|
$this->path = $path; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Path getter. |
|
45
|
|
|
* |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
66 |
|
public function getPath(): string |
|
49
|
|
|
{ |
|
50
|
66 |
|
return $this->path; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Checks whether the file exists. |
|
55
|
|
|
* |
|
56
|
|
|
* @return bool |
|
57
|
|
|
*/ |
|
58
|
72 |
|
public function exists(): bool |
|
59
|
|
|
{ |
|
60
|
72 |
|
return is_file($this->path); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Reads json file. |
|
65
|
|
|
* |
|
66
|
|
|
* @return mixed |
|
67
|
|
|
* @throws \RuntimeException |
|
68
|
|
|
*/ |
|
69
|
60 |
|
public function read() |
|
70
|
|
|
{ |
|
71
|
60 |
|
if (!file_exists($this->path)) { |
|
72
|
1 |
|
throw new RuntimeException('Could not read ' . $this->path); |
|
73
|
|
|
} |
|
74
|
59 |
|
return file_get_contents($this->path); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Writes file. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $content |
|
81
|
|
|
* @throws \RuntimeException |
|
82
|
|
|
*/ |
|
83
|
25 |
|
public function write($content): void |
|
84
|
|
|
{ |
|
85
|
25 |
|
$this->checkFile(); |
|
86
|
24 |
|
$this->checkDir(); |
|
87
|
|
|
|
|
88
|
21 |
|
file_put_contents($this->path, $content); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Check if file exists and isn't writable |
|
93
|
|
|
* |
|
94
|
|
|
* @return void |
|
95
|
|
|
* @throws \RuntimeException |
|
96
|
|
|
*/ |
|
97
|
25 |
|
private function checkFile(): void |
|
98
|
|
|
{ |
|
99
|
25 |
|
if (file_exists($this->path) && !is_writable($this->path)) { |
|
100
|
1 |
|
throw new RuntimeException('File exists and is not writable'); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Create directory if necessary |
|
106
|
|
|
* |
|
107
|
|
|
* @return void |
|
108
|
|
|
* @throws \RuntimeException |
|
109
|
|
|
*/ |
|
110
|
24 |
|
private function checkDir(): void |
|
111
|
|
|
{ |
|
112
|
24 |
|
$dir = dirname($this->path); |
|
113
|
24 |
|
if (!is_dir($dir)) { |
|
114
|
3 |
|
if (file_exists($dir)) { |
|
115
|
1 |
|
throw new RuntimeException($dir . ' exists and is not a directory.'); |
|
116
|
|
|
} |
|
117
|
2 |
|
if (!@mkdir($dir, 0755, true)) { |
|
118
|
2 |
|
throw new RuntimeException($dir . ' does not exist and could not be created.'); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Determines if the given path is a symbolic link |
|
125
|
|
|
* |
|
126
|
|
|
* @return bool True if the path is a symbolic link, false otherwise. |
|
127
|
|
|
*/ |
|
128
|
15 |
|
public function isLink(): bool |
|
129
|
|
|
{ |
|
130
|
15 |
|
return is_link($this->path); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Resolves and returns the target of a symbolic link |
|
135
|
|
|
* |
|
136
|
|
|
* @return string The resolved target of the symbolic link. |
|
137
|
|
|
*/ |
|
138
|
2 |
|
public function linkTarget(): string |
|
139
|
|
|
{ |
|
140
|
2 |
|
if (!$this->isLink()) { |
|
141
|
1 |
|
throw new RuntimeException('Not a symbolic link: ' . $this->path); |
|
142
|
|
|
} |
|
143
|
1 |
|
return (string)readlink($this->path); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|