|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bicycle\TesseractBridgeBundle\DataCollector; |
|
4
|
|
|
|
|
5
|
|
|
use Bicycle\Tesseract\Bridge; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
8
|
|
|
use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
|
9
|
|
|
|
|
10
|
|
|
class TesseractBridgeDataCollector extends DataCollector |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var string */ |
|
13
|
|
|
public const NAME = 'tesseract_bridge'; |
|
14
|
|
|
|
|
15
|
|
|
/** @var Bridge\CLI|null */ |
|
16
|
|
|
private ?Bridge\CLI $cliIntegration; |
|
17
|
|
|
|
|
18
|
|
|
/** @var Bridge\FFI|null */ |
|
19
|
|
|
private ?Bridge\FFI $ffiIntegration; |
|
20
|
|
|
|
|
21
|
|
|
/** @var array */ |
|
22
|
|
|
private array $configuration; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param Bridge\CLI|null $cliIntegration |
|
26
|
|
|
* @param Bridge\FFI|null $ffiIntegration |
|
27
|
|
|
* @param array $configuration |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(?Bridge\CLI $cliIntegration, ?Bridge\FFI $ffiIntegration, array $configuration) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->cliIntegration = $cliIntegration; |
|
32
|
|
|
$this->ffiIntegration = $ffiIntegration; |
|
33
|
|
|
$this->configuration = $configuration; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritDoc} |
|
38
|
|
|
*/ |
|
39
|
|
|
public function collect(Request $request, Response $response, \Throwable $exception = null): void |
|
40
|
|
|
{ |
|
41
|
|
|
$this->data = ['configuration' => $this->configuration, 'cliIntegration' => [], 'ffiIntegration' => []]; |
|
42
|
|
|
if ($this->cliIntegration instanceof Bridge\CLI) { |
|
43
|
|
|
$this->data['cliIntegration'] = [ |
|
44
|
|
|
'tesseractVersion' => $this->cliIntegration->getVersion(), |
|
45
|
|
|
'availableLanguages' => $this->cliIntegration->getAvailableLanguages(), |
|
46
|
|
|
]; |
|
47
|
|
|
} |
|
48
|
|
|
if ($this->ffiIntegration instanceof Bridge\FFI) { |
|
49
|
|
|
/* |
|
50
|
|
|
* Here we have to do some magic because of bug in libtesseract 4.0.0. We cannot get languages because of |
|
51
|
|
|
* !strcmp(locale, "C"):Error:Assert failed:in file baseapi.cpp, line 209 |
|
52
|
|
|
* Segmentation fault |
|
53
|
|
|
* Workaround is perform this magic with locales |
|
54
|
|
|
*/ |
|
55
|
|
|
$version = $this->ffiIntegration->getVersion(); |
|
56
|
|
|
if ('4.0.0' === $version) { |
|
57
|
|
|
$oldLocale = setlocale(LC_ALL, 0); |
|
58
|
|
|
setlocale(LC_ALL, 'C'); |
|
59
|
|
|
} |
|
60
|
|
|
$this->data['ffiIntegration'] = [ |
|
61
|
|
|
'tesseractVersion' => $version, |
|
62
|
|
|
'availableLanguages' => $this->ffiIntegration->getAvailableLanguages(), |
|
63
|
|
|
]; |
|
64
|
|
|
if (isset($oldLocale)) { // Returning locale back to original state |
|
65
|
|
|
setlocale(LC_ALL, $oldLocale); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function reset(): void |
|
71
|
|
|
{ |
|
72
|
|
|
$this->data = []; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritDoc} |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getName(): string |
|
79
|
|
|
{ |
|
80
|
|
|
return static::NAME; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return bool |
|
85
|
|
|
*/ |
|
86
|
|
|
public function hasFFI(): bool |
|
87
|
|
|
{ |
|
88
|
|
|
return extension_loaded('ffi'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
|
|
public function isCliIntegrationEnabled(): bool |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->data['configuration']['integrations']['cli']['enabled'] ?? false; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return bool |
|
101
|
|
|
*/ |
|
102
|
|
|
public function isFfiIntegrationEnabled(): bool |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->data['configuration']['integrations']['ffi']['enabled'] ?? false; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return array |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getCliIntegrationData(): array |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->data['cliIntegration'] ?? []; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getFfiIntegrationData(): array |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->data['ffiIntegration'] ?? []; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|