-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMinify.php
More file actions
61 lines (50 loc) · 1.94 KB
/
Copy pathMinify.php
File metadata and controls
61 lines (50 loc) · 1.94 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
<?php
declare(strict_types=1);
/*
* This file is part of the SensioLabs MinifyBundle package.
*
* (c) Simon André - Sensiolabs
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sensiolabs\MinifyBundle;
use Sensiolabs\MinifyBundle\Exception\RuntimeException;
use Sensiolabs\MinifyBundle\Minifier\MinifierInterface;
use Sensiolabs\MinifyBundle\Minifier\Options\OptionsInterface;
use Symfony\Component\Process\Process;
/**
* @author Simon André <smn.andre@gmail.com>
*/
final class Minify implements MinifierInterface
{
public function __construct(
private readonly string $binaryPath,
) {
}
public function minify(string $input, string $type/* , ?OptionsInterface $options = null */): string
{
$options = \func_num_args() > 2 ? \func_get_arg(2) : null;
if ($options !== null && !$options instanceof OptionsInterface) {
throw new RuntimeException(sprintf('Expected $options to be an instance of "%s", got "%s".', OptionsInterface::class, get_debug_type($options)));
}
if ($options !== null && $options->getType() !== $type) {
throw new RuntimeException(sprintf('Options type "%s" does not match minify type "%s".', $options->getType(), $type));
}
$args = [$this->binaryPath, '--type', $type];
if ($options !== null) {
$args = [...$args, ...$options->toCliArgs()];
}
$process = new Process($args);
$process->setInput($input);
try {
$process->run();
} catch (\Throwable $e) {
throw new RuntimeException('Error during minify command: '.$e->getMessage(), 0, $e);
}
if (!$process->isSuccessful()) {
throw new RuntimeException(sprintf('Minify error %s: "%s".', $process->getExitCode(), $process->getExitCodeText()));
}
return $process->getOutput();
}
}