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