1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RobbieP\ZbarQrdecoder; |
4
|
|
|
|
5
|
|
|
use RobbieP\ZbarQrdecoder\Result\AbstractResult; |
6
|
|
|
use RobbieP\ZbarQrdecoder\Result\ErrorResult; |
7
|
|
|
use RobbieP\ZbarQrdecoder\Result\Parser\ParserXML; |
8
|
|
|
use RobbieP\ZbarQrdecoder\Result\Result; |
9
|
|
|
use RobbieP\ZbarQrdecoder\Result\ResultCollection; |
10
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
11
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
12
|
|
|
|
13
|
|
|
class ZbarDecoder |
14
|
|
|
{ |
15
|
|
|
const EXECUTABLE = 'zbarimg'; |
16
|
|
|
|
17
|
|
|
private $path; |
18
|
|
|
private $filePath; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var AbstractResult|ResultCollection |
22
|
|
|
*/ |
23
|
|
|
private $result; |
24
|
|
|
/** |
25
|
|
|
* @var ProcessBuilder |
26
|
|
|
*/ |
27
|
|
|
private $processBuilder; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param array $config |
31
|
|
|
* @param ProcessBuilder $processBuilder |
32
|
|
|
*/ |
33
|
|
|
public function __construct(array $config = [], $processBuilder = null) |
34
|
|
|
{ |
35
|
|
|
if (isset($config['path'])) { |
36
|
|
|
$this->setPath($config['path']); |
37
|
|
|
} |
38
|
|
|
$this->processBuilder = null === $processBuilder ? new ProcessBuilder() : $processBuilder; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Main constructor - builds the process, runs it then returns the Result object |
43
|
|
|
* |
44
|
|
|
* @param $filename |
45
|
|
|
* |
46
|
|
|
* @return AbstractResult |
47
|
|
|
* @throws \Exception |
48
|
|
|
*/ |
49
|
|
|
public function make($filename) |
50
|
|
|
{ |
51
|
|
|
$this->setFilePath($filename); |
52
|
|
|
$this->buildProcess(); |
53
|
|
|
$this->runProcess(); |
54
|
|
|
|
55
|
|
|
return $this->output(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns the path to the executable zbarimg |
60
|
|
|
* Defaults to /usr/bin |
61
|
|
|
* |
62
|
|
|
* @throws \Exception |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
|
|
public function getPath() |
66
|
|
|
{ |
67
|
|
|
if (!$this->path) { |
68
|
|
|
$this->setPath('/usr/bin'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->path; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param mixed $path |
76
|
|
|
*/ |
77
|
|
|
public function setPath($path) |
78
|
|
|
{ |
79
|
|
|
$this->path = rtrim($path, '/'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
|
|
public function getFilePath() |
86
|
|
|
{ |
87
|
|
|
return $this->filePath; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param mixed $filePath |
92
|
|
|
* |
93
|
|
|
* @throws \Exception |
94
|
|
|
*/ |
95
|
|
|
public function setFilePath($filePath) |
96
|
|
|
{ |
97
|
|
|
if (!is_file($filePath)) { |
98
|
|
|
throw new \RuntimeException('Invalid filepath given'); |
99
|
|
|
} |
100
|
|
|
$this->filePath = $filePath; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Builds the process |
105
|
|
|
* TODO: Configurable arguments |
106
|
|
|
* |
107
|
|
|
* @throws \Exception |
108
|
|
|
*/ |
109
|
|
|
private function buildProcess() |
110
|
|
|
{ |
111
|
|
|
$path = $this->getPath(); |
112
|
|
|
$this->processBuilder->setPrefix($path . DIRECTORY_SEPARATOR . static::EXECUTABLE); |
113
|
|
|
$this->processBuilder->setArguments(['-D', '--xml', '-q', $this->getFilePath()])->enableOutput(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Runs the process |
118
|
|
|
* |
119
|
|
|
* @throws \Exception |
120
|
|
|
*/ |
121
|
|
|
private function runProcess() |
122
|
|
|
{ |
123
|
|
|
$process = $this->processBuilder->getProcess(); |
124
|
|
|
try { |
125
|
|
|
$process->mustRun(); |
126
|
|
|
$parser = new ParserXML(); |
127
|
|
|
$result = $parser->parse($process->getOutput()); |
128
|
|
|
if (count($result) === 1) { |
129
|
|
|
$result = $result->getResults()[0]; |
130
|
|
|
} |
131
|
|
|
$this->result = $result; |
132
|
|
|
} catch (ProcessFailedException $e) { |
133
|
|
|
switch ($e->getProcess()->getExitCode()) { |
134
|
|
|
case 1: |
135
|
|
|
throw new \RuntimeException('An error occurred while processing the image. It could be bad arguments, I/O errors and image handling errors from ImageMagick'); |
136
|
|
|
case 2: |
137
|
|
|
throw new \RuntimeException('ImageMagick fatal error'); |
138
|
|
|
case 4: |
139
|
|
|
$this->result = new ErrorResult('No barcode detected'); |
140
|
|
|
break; |
141
|
|
|
default: |
142
|
|
|
throw new \RuntimeException('Problem with decode - check you have zbar-tools installed'); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Only return the output class to the end user |
150
|
|
|
* |
151
|
|
|
* @return AbstractResult|ResultCollection |
152
|
|
|
*/ |
153
|
|
|
private function output() |
154
|
|
|
{ |
155
|
|
|
return $this->result; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|