1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the box project. |
7
|
|
|
* |
8
|
|
|
* (c) Kevin Herrera <[email protected]> |
9
|
|
|
* Théo Fidry <[email protected]> |
10
|
|
|
* |
11
|
|
|
* This source file is subject to the MIT license that is bundled |
12
|
|
|
* with this source code in the file LICENSE. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace KevinGH\Box\Compactor; |
16
|
|
|
|
17
|
|
|
use function array_reduce; |
18
|
|
|
use Humbug\PhpScoper\Whitelist; |
19
|
|
|
use KevinGH\Box\PhpScoper\Scoper; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @private |
23
|
|
|
*/ |
24
|
|
|
final class Compactors |
25
|
|
|
{ |
26
|
|
|
private $compactors; |
27
|
|
|
private $scoperCompactor; |
28
|
|
|
|
29
|
|
|
public function __construct(Compactor ...$compactors) |
30
|
|
|
{ |
31
|
|
|
$this->compactors = $compactors; |
32
|
|
|
|
33
|
|
|
foreach ($compactors as $compactor) { |
34
|
|
|
if ($compactor instanceof PhpScoper) { |
35
|
|
|
$this->scoperCompactor = $compactor; |
36
|
|
|
|
37
|
|
|
break; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function compactContents(string $file, string $contents): string |
43
|
|
|
{ |
44
|
|
|
return (string) array_reduce( |
45
|
|
|
$this->compactors, |
46
|
|
|
static function (string $contents, Compactor $compactor) use ($file): string { |
47
|
|
|
return $compactor->compact($file, $contents); |
48
|
|
|
}, |
49
|
|
|
$contents |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getScoper(): ?Scoper |
54
|
|
|
{ |
55
|
|
|
return null !== $this->scoperCompactor ? $this->scoperCompactor->getScoper() : null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getScoperWhitelist(): ?Whitelist |
59
|
|
|
{ |
60
|
|
|
return null !== $this->scoperCompactor ? $this->scoperCompactor->getScoper()->getWhitelist() : null; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function registerWhitelist(Whitelist $whitelist): void |
64
|
|
|
{ |
65
|
|
|
if (null !== $this->scoperCompactor) { |
66
|
|
|
$this->scoperCompactor->getScoper()->changeWhitelist($whitelist); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function toArray(): array |
71
|
|
|
{ |
72
|
|
|
return $this->compactors; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|