|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ddeboer\DataImport\Writer; |
|
4
|
|
|
|
|
5
|
|
|
use Ddeboer\DataImport\Writer; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Base class to write into streams |
|
9
|
|
|
* |
|
10
|
|
|
* @author Benoît Burnichon <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class AbstractStreamWriter implements Writer |
|
13
|
|
|
{ |
|
14
|
|
|
use WriterTemplate; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var resource |
|
18
|
|
|
*/ |
|
19
|
|
|
private $stream; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var boolean |
|
23
|
|
|
*/ |
|
24
|
|
|
private $closeStreamOnFinish = true; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param resource $stream |
|
28
|
|
|
*/ |
|
29
|
27 |
|
public function __construct($stream = null) |
|
30
|
|
|
{ |
|
31
|
27 |
|
if (null !== $stream) { |
|
32
|
4 |
|
$this->setStream($stream); |
|
33
|
4 |
|
} |
|
34
|
27 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Set the stream resource |
|
38
|
|
|
* |
|
39
|
|
|
* @param resource $stream |
|
40
|
|
|
* |
|
41
|
|
|
* @throws \InvalidArgumentException |
|
42
|
|
|
* |
|
43
|
|
|
* @return $this |
|
44
|
|
|
*/ |
|
45
|
19 |
|
public function setStream($stream) |
|
46
|
|
|
{ |
|
47
|
19 |
|
if (! is_resource($stream) || ! 'stream' == get_resource_type($stream)) { |
|
48
|
2 |
|
throw new \InvalidArgumentException(sprintf( |
|
49
|
2 |
|
'Expects argument to be a stream resource, got %s', |
|
50
|
2 |
|
is_resource($stream) ? get_resource_type($stream) : gettype($stream) |
|
51
|
2 |
|
)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
17 |
|
$this->stream = $stream; |
|
55
|
|
|
|
|
56
|
17 |
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return resource |
|
61
|
|
|
*/ |
|
62
|
15 |
|
public function getStream() |
|
63
|
|
|
{ |
|
64
|
15 |
|
if (null === $this->stream) { |
|
65
|
9 |
|
$this->setStream(fopen('php://temp', 'rb+')); |
|
66
|
9 |
|
$this->setCloseStreamOnFinish(false); |
|
67
|
9 |
|
} |
|
68
|
|
|
|
|
69
|
15 |
|
return $this->stream; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
6 |
|
public function finish() |
|
76
|
|
|
{ |
|
77
|
6 |
|
if (is_resource($this->stream) && $this->getCloseStreamOnFinish()) { |
|
78
|
6 |
|
fclose($this->stream); |
|
79
|
6 |
|
} |
|
80
|
6 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Should underlying stream be closed on finish? |
|
84
|
|
|
* |
|
85
|
|
|
* @param boolean $closeStreamOnFinish |
|
86
|
|
|
* |
|
87
|
|
|
* @return boolean |
|
88
|
|
|
*/ |
|
89
|
14 |
|
public function setCloseStreamOnFinish($closeStreamOnFinish = true) |
|
90
|
|
|
{ |
|
91
|
14 |
|
$this->closeStreamOnFinish = (bool) $closeStreamOnFinish; |
|
92
|
|
|
|
|
93
|
14 |
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Is Stream closed on finish? |
|
98
|
|
|
* |
|
99
|
|
|
* @return boolean |
|
100
|
|
|
*/ |
|
101
|
10 |
|
public function getCloseStreamOnFinish() |
|
102
|
|
|
{ |
|
103
|
10 |
|
return $this->closeStreamOnFinish; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|