|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Posprint\Connectors; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class File |
|
7
|
|
|
* Create a binary file and writes the data line by line. |
|
8
|
|
|
* And it can also be used to provide direct connections to USB ports. |
|
9
|
|
|
* |
|
10
|
|
|
* @category NFePHP |
|
11
|
|
|
* @package Posprint |
|
12
|
|
|
* @copyright Copyright (c) 2016 |
|
13
|
|
|
* @license http://www.gnu.org/licenses/lesser.html LGPL v3 |
|
14
|
|
|
* @author Roberto L. Machado <linux.rlm at gmail dot com> |
|
15
|
|
|
* @link http://github.com/nfephp-org/posprint for the canonical source repository |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
use Posprint\Connectors\ConnectorInterface; |
|
19
|
|
|
use RuntimeException; |
|
20
|
|
|
use InvalidArgumentException; |
|
21
|
|
|
|
|
22
|
|
|
class File implements ConnectorInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var resource The file pointer to send data to. |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $resource = false; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Construct new connector, given a filename |
|
31
|
|
|
* If created a binary file must be granted the necessary |
|
32
|
|
|
* permissions to create and write the system file |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $filename |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct($filename = null) |
|
37
|
|
|
{ |
|
38
|
|
|
if (is_null($filename) || empty($filename)) { |
|
39
|
|
|
throw new InvalidArgumentException("A filepath must be passed!"); |
|
40
|
|
|
} |
|
41
|
|
|
$command = 'rb+'; |
|
42
|
|
|
if (! is_file($filename)) { |
|
43
|
|
|
$command = 'wb+'; |
|
44
|
|
|
} |
|
45
|
|
|
$this->resource = @fopen($filename, $command); |
|
46
|
|
|
if ($this->resource === false) { |
|
47
|
|
|
throw new RuntimeException("Failed to open the file. Check the permissions!"); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Destruct conection closing the file |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __destruct() |
|
55
|
|
|
{ |
|
56
|
|
|
if ($this->resource != false) { |
|
57
|
|
|
$this->close(); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Close file pointer |
|
63
|
|
|
*/ |
|
64
|
|
|
public function close() |
|
65
|
|
|
{ |
|
66
|
|
|
if (is_resource($this->resource)) { |
|
67
|
|
|
@fclose($this->resource); |
|
|
|
|
|
|
68
|
|
|
$this->resource = false; |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Write data to the file |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $data |
|
76
|
|
|
*/ |
|
77
|
|
|
public function write($data = '') |
|
78
|
|
|
{ |
|
79
|
|
|
if (is_resource($this->resource) && !empty($data)) { |
|
80
|
|
|
return fwrite($this->resource, $data); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
/** |
|
84
|
|
|
* Read some bytes from file |
|
85
|
|
|
* |
|
86
|
|
|
* @param int $len |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
public function read($len = null) |
|
90
|
|
|
{ |
|
91
|
|
|
$data = ''; |
|
92
|
|
|
if (!is_null($len) && is_numeric($len)) { |
|
93
|
|
|
$len = ceil($len); |
|
94
|
|
|
return fread($this->resource, $len); |
|
95
|
|
|
} |
|
96
|
|
|
while (!feof($this->resource)) { |
|
97
|
|
|
$data .= fread($this->resource, 4096); |
|
98
|
|
|
} |
|
99
|
|
|
return $data; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: