|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\CLImate\Util\Writer; |
|
4
|
|
|
|
|
5
|
|
|
use League\CLImate\Exceptions\RuntimeException; |
|
6
|
|
|
|
|
7
|
|
|
class File implements WriterInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var resource|string */ |
|
10
|
|
|
protected $resource; |
|
11
|
|
|
|
|
12
|
|
|
/** @var boolean $close_locally */ |
|
13
|
|
|
protected $close_locally = false; |
|
14
|
|
|
|
|
15
|
|
|
/** @var boolean $use_locking */ |
|
16
|
|
|
protected $use_locking = false; |
|
17
|
|
|
|
|
18
|
|
|
/** @var boolean $gzip_file */ |
|
19
|
|
|
protected $gzip_file = false; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param string|resource $resource |
|
23
|
|
|
* @param bool $use_locking |
|
24
|
24 |
|
* @param bool $gzip_file |
|
25
|
|
|
*/ |
|
26
|
24 |
|
public function __construct($resource, $use_locking = false, $gzip_file = false) |
|
27
|
24 |
|
{ |
|
28
|
24 |
|
$this->resource = $resource; |
|
29
|
24 |
|
$this->use_locking = $use_locking; |
|
30
|
|
|
$this->gzip_file = $gzip_file; |
|
31
|
4 |
|
} |
|
32
|
|
|
|
|
33
|
4 |
|
public function lock() |
|
34
|
|
|
{ |
|
35
|
4 |
|
$this->use_locking = true; |
|
36
|
|
|
|
|
37
|
|
|
return $this; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function gzipped() |
|
41
|
|
|
{ |
|
42
|
|
|
$this->gzip_file = true; |
|
43
|
|
|
|
|
44
|
|
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Write the content to the stream |
|
49
|
|
|
* |
|
50
|
24 |
|
* @param string $content |
|
51
|
|
|
*/ |
|
52
|
24 |
|
public function write($content) |
|
53
|
|
|
{ |
|
54
|
12 |
|
$resource = $this->getResource(); |
|
55
|
4 |
|
|
|
56
|
4 |
|
if ($this->use_locking) { |
|
57
|
|
|
flock($resource, LOCK_EX); |
|
58
|
12 |
|
} |
|
59
|
|
|
|
|
60
|
12 |
|
gzwrite($resource, $content); |
|
61
|
4 |
|
|
|
62
|
4 |
|
if ($this->use_locking) { |
|
63
|
12 |
|
flock($resource, LOCK_UN); |
|
64
|
|
|
} |
|
65
|
24 |
|
} |
|
66
|
|
|
|
|
67
|
24 |
|
protected function getResource() |
|
68
|
8 |
|
{ |
|
69
|
|
|
if (is_resource($this->resource)) { |
|
70
|
|
|
return $this->resource; |
|
71
|
16 |
|
} |
|
72
|
|
|
|
|
73
|
16 |
|
$this->close_locally = true; |
|
74
|
8 |
|
|
|
75
|
|
|
if (!is_writable($this->resource)) { |
|
76
|
|
|
throw new RuntimeException("The resource [{$this->resource}] is not writable"); |
|
77
|
8 |
|
} |
|
78
|
4 |
|
|
|
79
|
|
|
if (!($this->resource = $this->openResource())) { |
|
80
|
|
|
throw new RuntimeException("The resource could not be opened"); |
|
81
|
4 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $this->resource; |
|
84
|
8 |
|
} |
|
85
|
|
|
|
|
86
|
8 |
|
protected function openResource() |
|
87
|
|
|
{ |
|
88
|
|
|
if ($this->gzip_file) { |
|
89
|
|
|
return gzopen($this->resource, 'a'); |
|
90
|
8 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
return fopen($this->resource, 'a'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function _destruct() |
|
96
|
|
|
{ |
|
97
|
|
|
if ($this->close_locally) { |
|
98
|
|
|
gzclose($this->getResource()); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|