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 Humbug\PhpScoper\Symbol\SymbolsRegistry; |
18
|
|
|
use function array_reduce; |
19
|
|
|
use function count; |
20
|
|
|
use Countable; |
21
|
|
|
use Humbug\PhpScoper\Whitelist; |
22
|
|
|
use KevinGH\Box\PhpScoper\Scoper; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @private |
26
|
|
|
*/ |
27
|
|
|
final class Compactors implements Compactor, Countable |
28
|
|
|
{ |
29
|
|
|
private $compactors; |
30
|
|
|
private $scoperCompactor; |
31
|
|
|
|
32
|
|
|
public function __construct(Compactor ...$compactors) |
33
|
|
|
{ |
34
|
|
|
$this->compactors = $compactors; |
35
|
|
|
|
36
|
|
|
foreach ($compactors as $compactor) { |
37
|
|
|
if ($compactor instanceof PhpScoper) { |
38
|
|
|
$this->scoperCompactor = $compactor; |
39
|
|
|
|
40
|
|
|
break; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function compact(string $file, string $contents): string |
49
|
|
|
{ |
50
|
|
|
return (string) array_reduce( |
51
|
|
|
$this->compactors, |
52
|
|
|
static function (string $contents, Compactor $compactor) use ($file): string { |
53
|
|
|
return $compactor->compact($file, $contents); |
54
|
|
|
}, |
55
|
|
|
$contents |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getScoper(): ?Scoper |
60
|
|
|
{ |
61
|
|
|
return null !== $this->scoperCompactor ? $this->scoperCompactor->getScoper() : null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getScoperSymbolsRegistry(): ?SymbolsRegistry |
65
|
|
|
{ |
66
|
|
|
return null !== $this->scoperCompactor ? $this->scoperCompactor->getScoper()->getSymbolsRegistry() : null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function registerSymbolsRegistry(SymbolsRegistry $symbolsRegistry): void |
70
|
|
|
{ |
71
|
|
|
if (null !== $this->scoperCompactor) { |
72
|
|
|
$this->scoperCompactor->getScoper()->changeSymbolsRegistry($symbolsRegistry); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function toArray(): array |
77
|
|
|
{ |
78
|
|
|
return $this->compactors; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function count(): int |
85
|
|
|
{ |
86
|
|
|
return count($this->compactors); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|