1 | <?php |
||
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 | * @param bool $gzip_file |
||
25 | */ |
||
26 | 24 | public function __construct($resource, $use_locking = false, $gzip_file = false) |
|
32 | |||
33 | 4 | public function lock() |
|
34 | { |
||
35 | 4 | $this->use_locking = true; |
|
36 | |||
37 | 4 | 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 | * @param string $content |
||
51 | */ |
||
52 | 24 | public function write($content) |
|
53 | { |
||
54 | 24 | $resource = $this->getResource(); |
|
55 | |||
56 | 12 | if ($this->use_locking) { |
|
57 | 4 | flock($resource, LOCK_EX); |
|
58 | } |
||
59 | |||
60 | 12 | gzwrite($resource, $content); |
|
61 | |||
62 | 12 | if ($this->use_locking) { |
|
63 | 4 | flock($resource, LOCK_UN); |
|
64 | } |
||
65 | 12 | } |
|
66 | |||
67 | 24 | protected function getResource() |
|
85 | |||
86 | 8 | protected function openResource() |
|
87 | { |
||
88 | 8 | if ($this->gzip_file) { |
|
89 | return gzopen($this->resource, 'a'); |
||
90 | } |
||
91 | |||
92 | 8 | return fopen($this->resource, 'a'); |
|
93 | } |
||
94 | |||
95 | public function _destruct() |
||
101 | } |
||
102 |