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\Configuration; |
||
16 | |||
17 | use Closure; |
||
18 | use KevinGH\Box\Compactor\Compactors; |
||
19 | use KevinGH\Box\Composer\Artifact\ComposerArtifact; |
||
20 | use KevinGH\Box\MapFile; |
||
21 | use SplFileInfo; |
||
22 | use Symfony\Component\Filesystem\Path; |
||
23 | use Symfony\Component\Finder\SplFileInfo as SymfonySplFileInfo; |
||
24 | use Symfony\Component\VarDumper\Cloner\VarCloner; |
||
25 | use Symfony\Component\VarDumper\Dumper\CliDumper; |
||
26 | use function array_map; |
||
27 | use function sort; |
||
28 | use const SORT_STRING; |
||
29 | |||
30 | /** |
||
31 | * A class similar to {@see Configuration} but for which the property types and values might change in order to improve |
||
32 | * its readability when dumping it into a file. |
||
33 | * |
||
34 | * @internal |
||
35 | */ |
||
36 | final readonly class ExportableConfiguration |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
37 | { |
||
38 | public static function create(Configuration $configuration): self |
||
39 | { |
||
40 | $normalizePath = self::createPathNormalizer($configuration->getBasePath()); |
||
41 | $normalizePaths = static function (array $files) use ($normalizePath): array { |
||
42 | $files = array_map($normalizePath, $files); |
||
43 | sort($files, SORT_STRING); |
||
44 | |||
45 | return $files; |
||
46 | }; |
||
47 | |||
48 | $composerJson = $configuration->getComposerJson(); |
||
49 | $composerLock = $configuration->getComposerLock(); |
||
50 | |||
51 | return new self( |
||
52 | $normalizePath($configuration->getConfigurationFile()), |
||
53 | $configuration->getAlias(), |
||
54 | $configuration->getBasePath(), |
||
55 | null === $composerJson |
||
56 | ? null |
||
57 | : new ComposerArtifact( |
||
58 | $normalizePath($composerJson->path), |
||
59 | $composerJson->decodedContents, |
||
60 | ), |
||
61 | null === $composerLock |
||
62 | ? null |
||
63 | : new ComposerArtifact( |
||
64 | $normalizePath($composerLock->path), |
||
65 | $composerLock->decodedContents, |
||
66 | ), |
||
67 | $normalizePaths($configuration->getFiles()), |
||
68 | $normalizePaths($configuration->getBinaryFiles()), |
||
69 | $configuration->hasAutodiscoveredFiles(), |
||
70 | $configuration->dumpAutoload(), |
||
71 | $configuration->excludeComposerArtifacts(), |
||
72 | $configuration->excludeDevFiles(), |
||
73 | array_map('get_class', $configuration->getCompactors()->toArray()), |
||
74 | $configuration->getCompressionAlgorithm()->name, |
||
75 | '0'.decoct($configuration->getFileMode()), |
||
76 | $normalizePath($configuration->getMainScriptPath()), |
||
77 | $configuration->getMainScriptContents(), |
||
78 | $configuration->getFileMapper(), |
||
79 | $configuration->getMetadata(), |
||
80 | $normalizePath($configuration->getTmpOutputPath()), |
||
81 | $normalizePath($configuration->getOutputPath()), |
||
82 | // TODO: remove this from the dump & add the SensitiveParam annotation |
||
83 | $configuration->getPrivateKeyPassphrase(), |
||
84 | $normalizePath($configuration->getPrivateKeyPath()), |
||
85 | $configuration->promptForPrivateKey(), |
||
86 | $configuration->getReplacements(), |
||
87 | $configuration->getShebang(), |
||
88 | $configuration->getSigningAlgorithm()->name, |
||
89 | $configuration->getStubBannerContents(), |
||
90 | $normalizePath($configuration->getStubBannerPath()), |
||
91 | $normalizePath($configuration->getStubPath()), |
||
92 | $configuration->isInterceptFileFuncs(), |
||
93 | $configuration->isStubGenerated(), |
||
94 | $configuration->checkRequirements(), |
||
95 | $configuration->getWarnings(), |
||
96 | $configuration->getRecommendations(), |
||
97 | ); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @return Closure(null|SplFileInfo|string): string|null |
||
102 | */ |
||
103 | private static function createPathNormalizer(string $basePath): Closure |
||
104 | { |
||
105 | return static function (null|SplFileInfo|string $path) use ($basePath): ?string { |
||
106 | if (null === $path) { |
||
107 | return null; |
||
108 | } |
||
109 | |||
110 | if ($path instanceof SplFileInfo) { |
||
111 | $path = $path->getPathname(); |
||
112 | } |
||
113 | |||
114 | return Path::makeRelative($path, $basePath); |
||
115 | }; |
||
116 | } |
||
117 | |||
118 | /** @noinspection PhpPropertyOnlyWrittenInspection */ |
||
119 | private function __construct( |
||
120 | private ?string $file, |
||
121 | private string $alias, |
||
122 | private string $basePath, |
||
123 | private ?ComposerArtifact $composerJson, |
||
124 | private ?ComposerArtifact $composerLock, |
||
125 | private array $files, |
||
126 | private array $binaryFiles, |
||
127 | private bool $autodiscoveredFiles, |
||
128 | private bool $dumpAutoload, |
||
129 | private bool $excludeComposerArtifacts, |
||
130 | private bool $excludeDevFiles, |
||
131 | private array|Compactors $compactors, |
||
132 | private string $compressionAlgorithm, |
||
133 | private null|int|string $fileMode, |
||
134 | private ?string $mainScriptPath, |
||
135 | private ?string $mainScriptContents, |
||
136 | private MapFile $fileMapper, |
||
137 | private mixed $metadata, |
||
138 | private string $tmpOutputPath, |
||
139 | private string $outputPath, |
||
140 | private ?string $privateKeyPassphrase, |
||
141 | private ?string $privateKeyPath, |
||
142 | private bool $promptForPrivateKey, |
||
143 | private array $processedReplacements, |
||
144 | private ?string $shebang, |
||
145 | private string $signingAlgorithm, |
||
146 | private ?string $stubBannerContents, |
||
147 | private ?string $stubBannerPath, |
||
148 | private ?string $stubPath, |
||
149 | private bool $isInterceptFileFuncs, |
||
150 | private bool $isStubGenerated, |
||
151 | private bool $checkRequirements, |
||
152 | private array $warnings, |
||
153 | private array $recommendations, |
||
154 | ) { |
||
155 | } |
||
156 | |||
157 | public function export(): string |
||
158 | { |
||
159 | $cloner = new VarCloner(); |
||
160 | $cloner->setMaxItems(-1); |
||
161 | $cloner->setMaxString(-1); |
||
162 | |||
163 | $normalizePath = self::createPathNormalizer($this->basePath); |
||
164 | $splInfoCaster = static fn (SplFileInfo $fileInfo): array => [$normalizePath($fileInfo)]; |
||
165 | |||
166 | $cloner->addCasters([ |
||
167 | SplFileInfo::class => $splInfoCaster, |
||
168 | SymfonySplFileInfo::class => $splInfoCaster, |
||
169 | ]); |
||
170 | |||
171 | return (new CliDumper())->dump( |
||
172 | $cloner->cloneVar($this), |
||
173 | true, |
||
174 | ); |
||
175 | } |
||
176 | } |
||
177 |