|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* apparat-resource |
|
5
|
|
|
* |
|
6
|
|
|
* @category Apparat |
|
7
|
|
|
* @package Apparat\Resource |
|
8
|
|
|
* @subpackage Apparat\Resource\Infrastructure |
|
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
|
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
|
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/*********************************************************************************** |
|
15
|
|
|
* The MIT License (MIT) |
|
16
|
|
|
* |
|
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
|
18
|
|
|
* |
|
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
|
21
|
|
|
* the Software without restriction, including without limitation the rights to |
|
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
|
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
|
24
|
|
|
* subject to the following conditions: |
|
25
|
|
|
* |
|
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
27
|
|
|
* copies or substantial portions of the Software. |
|
28
|
|
|
* |
|
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
|
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
|
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
|
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
|
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
35
|
|
|
***********************************************************************************/ |
|
36
|
|
|
|
|
37
|
|
|
namespace Apparat\Resource\Infrastructure\Io\File; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* File writer trait |
|
41
|
|
|
* |
|
42
|
|
|
* @package Apparat\Resource |
|
43
|
|
|
* @subpackage Apparat\Resource\Infrastructure |
|
44
|
|
|
* @property string $file File path |
|
45
|
|
|
*/ |
|
46
|
|
|
trait WriterTrait |
|
47
|
|
|
{ |
|
48
|
|
|
/** |
|
49
|
|
|
* File options |
|
50
|
|
|
* |
|
51
|
|
|
* @var int |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $options; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Write data |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $data Data to write |
|
59
|
|
|
* @return int Bytes written |
|
60
|
|
|
*/ |
|
61
|
7 |
|
public function write($data) |
|
62
|
|
|
{ |
|
63
|
|
|
// If the parent directory does not exist but may be created |
|
64
|
7 |
|
if (!@is_dir(dirname($this->file)) && ($this->options & Writer::FILE_CREATE_DIRS)) { |
|
65
|
1 |
|
mkdir(dirname($this->file), 0777, true); |
|
66
|
1 |
|
} |
|
67
|
|
|
|
|
68
|
7 |
|
return file_put_contents($this->file, $data); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Set the file options |
|
73
|
|
|
* |
|
74
|
|
|
* @param int $options File options |
|
75
|
|
|
*/ |
|
76
|
16 |
|
protected function setOptions($options) |
|
77
|
|
|
{ |
|
78
|
16 |
|
$options = intval($options); |
|
79
|
16 |
|
$allOptions = Writer::FILE_CREATE | Writer::FILE_CREATE_DIRS | Writer::FILE_OVERWRITE; |
|
80
|
|
|
|
|
81
|
16 |
|
if (($options & $allOptions) != $options) { |
|
82
|
1 |
|
throw new InvalidArgumentException( |
|
83
|
1 |
|
sprintf('Invalid file writer option "%s"', $options & ~$allOptions), |
|
84
|
|
|
InvalidArgumentException::INVALID_FILE_WRITER_OPTIONS |
|
85
|
1 |
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
15 |
|
$this->options = $options; |
|
89
|
15 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Validate the writer file |
|
93
|
|
|
* |
|
94
|
|
|
* @throws InvalidArgumentException If the file cannot be created |
|
95
|
|
|
* @throws InvalidArgumentException If the file cannot be overwritten |
|
96
|
|
|
*/ |
|
97
|
15 |
|
protected function validateWriterFile() |
|
98
|
|
|
{ |
|
99
|
|
|
// If the parent directory does not exist and cannot be created |
|
100
|
15 |
|
if (!@is_dir(dirname($this->file)) && !($this->options & Writer::FILE_CREATE_DIRS)) { |
|
101
|
1 |
|
throw new InvalidArgumentException( |
|
102
|
1 |
|
sprintf('Parent directory "%s" cannot be created', dirname($this->file)), |
|
103
|
|
|
InvalidArgumentException::DIR_CANNOT_BE_CREATED |
|
104
|
1 |
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// If the file does not exist and cannot be created |
|
108
|
14 |
|
if (!@file_exists($this->file) && !($this->options & Writer::FILE_CREATE)) { |
|
109
|
1 |
|
throw new InvalidArgumentException( |
|
110
|
1 |
|
sprintf('File "%s" cannot be created', $this->file), |
|
111
|
|
|
InvalidArgumentException::FILE_CANNOT_BE_CREATED |
|
112
|
1 |
|
); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// If the file exists but cannot be overwritten |
|
116
|
13 |
|
if (@file_exists($this->file) && |
|
117
|
|
|
( |
|
118
|
4 |
|
!@is_file($this->file) || |
|
119
|
4 |
|
!@is_writeable( |
|
120
|
4 |
|
$this->file |
|
121
|
4 |
|
) || |
|
122
|
3 |
|
!($this->options & Writer::FILE_OVERWRITE) |
|
123
|
3 |
|
) |
|
124
|
13 |
|
) { |
|
125
|
2 |
|
throw new InvalidArgumentException( |
|
126
|
2 |
|
sprintf('File "%s" cannot be overwritten', $this->file), |
|
127
|
|
|
InvalidArgumentException::FILE_CANNOT_BE_OVERWRITTEN |
|
128
|
2 |
|
); |
|
129
|
|
|
} |
|
130
|
11 |
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|