-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDependencyChecker.php
More file actions
108 lines (99 loc) · 3.68 KB
/
Copy pathDependencyChecker.php
File metadata and controls
108 lines (99 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
declare(strict_types=1);
namespace Mindee\Dependency;
use Exception;
use Mindee\Error\ErrorCode;
use Mindee\Error\MindeeUnhandledException;
use Imagick;
use TestingUtilities;
use function extension_loaded;
/**
* Utility class to check the availability of potentially incompatible libraries.
*/
class DependencyChecker
{
/**
* Throws if GhostScript isn't available on the system.
*
* @throws MindeeUnhandledException Throws if the GhostScript command cannot be found on the system.
*/
public static function isGhostscriptAvailable(): void
{
try {
$commandWasExecuted = false;
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$possiblePaths = [
'C:\Program Files\gs\gs*\bin\gswin64c.exe',
'C:\Program Files (x86)\gs\gs*\bin\gswin32c.exe',
'C:\Program Files\gs\gs*\bin\gswin32c.exe',
];
foreach ($possiblePaths as $path) {
$matches = glob($path);
if (!empty($matches)) {
$commandWasExecuted = true;
}
}
$pathDirs = explode(';', getenv('PATH'));
foreach ($pathDirs as $dir) {
if (file_exists($dir . '\gswin64c.exe') || file_exists($dir . '\gswin32c.exe')) {
$commandWasExecuted = true;
}
}
} else {
$commandWasExecuted = (bool) shell_exec('which gs');
}
} catch (Exception $e) {
throw new MindeeUnhandledException(
"To enable full support of PDF features, you need "
. "to enable Ghostscript on your PHP installation.",
ErrorCode::USER_MISSING_DEPENDENCY
);
}
if (!$commandWasExecuted) {
throw new MindeeUnhandledException(
"To enable full support of PDF features, you need "
. "to enable Ghostscript on your PHP installation.",
ErrorCode::USER_MISSING_DEPENDENCY
);
}
}
/**
* Throws if ImageMagick isn't available on the system.
*
* @throws MindeeUnhandledException Throws if ImageMagick isn't loaded.
*/
public static function isImageMagickAvailable(): void
{
if (!extension_loaded('imagick')) {
throw new MindeeUnhandledException(
"To enable full support of PDF features, you need "
. "to enable ImageMagick on your PHP installation. Also, you "
. "should setup ImageMagick's policy to allow for PDF operations.",
ErrorCode::USER_MISSING_DEPENDENCY
);
}
}
/**
* Checks whether Imagick is blocked by restrictive policy.
*
* @throws MindeeUnhandledException Throws if the local ImageMagick policy does not allow for PDF manipulations.
*/
public static function isImageMagickPolicyAllowed(): void
{
self::isImageMagickAvailable();
$imagick = new Imagick();
try {
$imagick->readImage(
/** @phpstan-ignore-next-line */
TestingUtilities::getV1DataDir() . "/products/expense_receipts/default_sample.jpg"
);
} catch (Exception $e) {
throw new MindeeUnhandledException(
"To enable full support of PDF features, you need "
. "to enable ImageMagick on your PHP installation. Also, you "
. "should setup ImageMagick's policy to allow for PDF operations.",
ErrorCode::USER_MISSING_DEPENDENCY
);
}
}
}