Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
26.73% covered (danger)
26.73%
27 / 101
12.50% covered (danger)
12.50%
1 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
InstalledJson
26.73% covered (danger)
26.73%
27 / 101
12.50% covered (danger)
12.50%
1 / 8
291.88
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 copyInstalledJson
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 getJsonFile
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
 updatePackagePaths
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
30
 removeMissingPackages
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 updateNamespaces
67.86% covered (warning)
67.86%
19 / 28
0.00% covered (danger)
0.00%
0 / 1
13.32
 createAndCleanTargetDirInstalledJson
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
12
 cleanupVendorInstalledJson
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Changes "install-path" to point to vendor-prefixed target directory.
4 *
5 * * create new vendor-prefixed/composer/installed.json file with copied packages
6 * * when delete is enabled, update package paths in the original vendor/composer/installed.json
7 * * when delete is enabled, remove dead entries in the original vendor/composer/installed.jso
8 *
9 * @see vendor/composer/installed.json
10 *
11 * TODO: when delete_vendor_files is used, the original directory still exists so the paths are not updated.
12 *
13 * @package brianhenryie/strauss
14 */
15
16namespace BrianHenryIE\Strauss\Pipeline\Cleanup;
17
18use BrianHenryIE\Strauss\Composer\ComposerPackage;
19use BrianHenryIE\Strauss\Config\CleanupConfigInterface;
20use BrianHenryIE\Strauss\Helpers\FileSystem;
21use BrianHenryIE\Strauss\Types\DiscoveredSymbols;
22use Composer\Json\JsonFile;
23use Composer\Json\JsonValidationException;
24use Psr\Log\LoggerAwareTrait;
25use Psr\Log\LoggerInterface;
26use Seld\JsonLint\ParsingException;
27
28/**
29 * @phpstan-type InstalledJsonPackageSourceArray array{type:string, url:string, reference:string}
30 * @phpstan-type InstalledJsonPackageDistArray array{type:string, url:string, reference:string, shasum:string}
31 * @phpstan-type InstalledJsonPackageAutoloadArray array<string,array<string,string>>
32 * @phpstan-type InstalledJsonPackageAuthorArray array{name:string,email:string}
33 * @phpstan-type InstalledJsonPackageSupportArray array{issues:string, source:string}
34 *
35 * @phpstan-type InstalledJsonPackageArray array{name:string, version:string, version_normalized:string, source:InstalledJsonPackageSourceArray, dist:InstalledJsonPackageDistArray, require:array<string,string>, require-dev:array<string,string>, time:string, type:string, installation-source:string, autoload:InstalledJsonPackageAutoloadArray, notification-url:string, license:array<string>, authors:array<InstalledJsonPackageAuthorArray>, description:string, homepage:string, keywords:array<string>, support:InstalledJsonPackageSupportArray, install-path:string}
36 *
37 * @phpstan-type InstalledJsonArray array{packages:array<InstalledJsonPackageArray>, dev:bool, dev-package-names:array<string>}
38 */
39class InstalledJson
40{
41    use LoggerAwareTrait;
42
43    protected CleanupConfigInterface $config;
44
45    protected FileSystem $filesystem;
46
47    public function __construct(
48        CleanupConfigInterface $config,
49        FileSystem $filesystem,
50        LoggerInterface $logger
51    ) {
52        $this->config = $config;
53        $this->filesystem = $filesystem;
54
55        $this->setLogger($logger);
56    }
57
58    protected function copyInstalledJson(): void
59    {
60        $this->logger->info('Copying vendor/composer/installed.json to vendor-prefixed/composer/installed.json');
61
62        $this->filesystem->copy(
63            $this->config->getVendorDirectory() . 'composer/installed.json',
64            $this->config->getTargetDirectory() . 'composer/installed.json'
65        );
66
67        $this->logger->debug('Copied vendor/composer/installed.json to vendor-prefixed/composer/installed.json');
68        $this->logger->debug($this->filesystem->read($this->config->getTargetDirectory() . 'composer/installed.json'));
69    }
70
71    /**
72     * @throws JsonValidationException
73     * @throws ParsingException
74     */
75    protected function getJsonFile(string $vendorDir): JsonFile
76    {
77        $installedJsonFile = new JsonFile(
78            sprintf(
79                '%scomposer/installed.json',
80                $vendorDir
81            )
82        );
83        if (!$installedJsonFile->exists()) {
84            $this->logger->error('Expected vendor/composer/installed.json does not exist.');
85            throw new \Exception('Expected vendor/composer/installed.json does not exist.');
86        }
87
88        $installedJsonFile->validateSchema(JsonFile::LAX_SCHEMA);
89
90        $this->logger->info('Loaded installed.json file: ' . $installedJsonFile->getPath());
91
92        return $installedJsonFile;
93    }
94
95    /**
96     * @param InstalledJsonArray $installedJsonArray
97     * @param array<string,ComposerPackage> $flatDependencyTree
98     */
99    protected function updatePackagePaths(array $installedJsonArray, array $flatDependencyTree): array
100    {
101
102        foreach ($installedJsonArray['packages'] as $key => $package) {
103            // Skip packages that were never copied in the first place.
104            if (!in_array($package['name'], array_keys($flatDependencyTree))) {
105                $this->logger->debug('Skipping package: ' . $package['name']);
106                continue;
107            }
108            $this->logger->info('Checking package: ' . $package['name']);
109
110            // `composer/` is here because the install-path is relative to the `vendor/composer` directory.
111            $packageDir = $this->config->getVendorDirectory() . 'composer/' . $package['install-path'] . '/';
112            if (!$this->filesystem->directoryExists($packageDir)) {
113                $this->logger->debug('Original package directory does not exist at : ' . $packageDir);
114
115                $newInstallPath = $this->config->getTargetDirectory() . str_replace('../', '', $package['install-path']);
116
117                if (!$this->filesystem->directoryExists($newInstallPath)) {
118                    $this->logger->warning('Target package directory unexpectedly DOES NOT exist: ' . $newInstallPath);
119                    continue;
120                }
121
122                $newRelativePath = $this->filesystem->getRelativePath(
123                    $this->config->getVendorDirectory() . 'composer/',
124                    $newInstallPath
125                );
126
127                $installedJsonArray['packages'][$key]['install-path'] = $newRelativePath;
128            } else {
129                $this->logger->debug('Original package directory exists at : ' . $packageDir);
130            }
131        }
132        return $installedJsonArray;
133    }
134
135
136    /**
137     * Remove packages from `installed.json` whose target directory does not exist
138     *
139     * E.g. after the file is copied to the target directory, this will remove dev dependencies and unmodified dependencies from the second installed.json
140     */
141    protected function removeMissingPackages(array $installedJsonArray, string $vendorDir): array
142    {
143        foreach ($installedJsonArray['packages'] as $key => $package) {
144            $path = $vendorDir . 'composer/' . $package['install-path'];
145            $pathExists = $this->filesystem->directoryExists($path);
146            if (!$pathExists) {
147                $this->logger->info('Removing package from installed.json: ' . $package['name']);
148                unset($installedJsonArray['packages'][$key]);
149            }
150        }
151        return $installedJsonArray;
152    }
153
154
155    protected function updateNamespaces(array $installedJsonArray, DiscoveredSymbols $discoveredSymbols): array
156    {
157        $discoveredNamespaces = $discoveredSymbols->getNamespaces();
158
159        foreach ($installedJsonArray['packages'] as $key => $package) {
160            if (!isset($package['autoload'])) {
161                // woocommerce/action-scheduler
162                $this->logger->debug('Package has no autoload key: ' . $package['name'] . ' ' . $package['type']);
163                continue;
164            }
165
166            $autoload_key = $package['autoload'];
167            foreach ($autoload_key as $type => $autoload) {
168                switch ($type) {
169                    case 'psr-4':
170                        /**
171                         * e.g.
172                         * * {"psr-4":{"Psr\\Log\\":"Psr\/Log\/"}}
173                         * * {"psr-4":{"":"src\/"}}
174                         * * {"psr-4":{"Symfony\\Polyfill\\Mbstring\\":""}}
175                         */
176                        foreach ($autoload_key[$type] as $originalNamespace => $packageRelativeDirectory) {
177                            // Replace $originalNamespace with updated namespace
178
179                            // Just for dev â€“ find a package like this and write a test for it.
180                            if (empty($originalNamespace)) {
181                                // In the case of `nesbot/carbon`, it uses an empty namespace but the classes are in the `Carbon`
182                                // namespace, so using `override_autoload` should be a good solution if this proves to be an issue.
183                                // The package directory will be updated, so for whatever reason the original empty namespace
184                                // works, maybe the updated namespace will work too.
185                                $this->logger->warning('Empty namespace found in autoload. Behaviour is not fully documented: ' . $package['name']);
186                                continue;
187                            }
188
189                            $trimmedOriginalNamespace = trim($originalNamespace, '\\');
190
191                            $this->logger->info('Checking PSR-4 namespace: ' . $trimmedOriginalNamespace);
192
193                            if (isset($discoveredNamespaces[$trimmedOriginalNamespace])) {
194                                $namespaceSymbol = $discoveredNamespaces[$trimmedOriginalNamespace];
195                            } else {
196                                $this->logger->debug('Namespace not found in list of changes: ' . $trimmedOriginalNamespace);
197                                continue;
198                            }
199
200                            if ($trimmedOriginalNamespace === trim($namespaceSymbol->getReplacement(), '\\')) {
201                                $this->logger->debug('Namespace is unchanged: ' . $trimmedOriginalNamespace);
202                                continue;
203                            }
204
205                            // Update the namespace if it has changed.
206                            $this->logger->info('Updating namespace: ' . $trimmedOriginalNamespace . ' => ' . $namespaceSymbol->getReplacement());
207                            $autoload_key[$type][str_replace($trimmedOriginalNamespace, $namespaceSymbol->getReplacement(), $originalNamespace)] = $autoload_key[$type][$originalNamespace];
208                            unset($autoload_key[$type][$originalNamespace]);
209
210//                            if (is_array($packageRelativeDirectory)) {
211//                                $autoload_key[$type][$originalNamespace] = array_filter(
212//                                    $packageRelativeDirectory,
213//                                    function ($dir) use ($packageDir) {
214//                                                $dir = $packageDir . $dir;
215//                                                $exists = $this->filesystem->directoryExists($dir) || $this->filesystem->fileExists($dir);
216//                                        if (!$exists) {
217//                                            $this->logger->info('Removing non-existent directory from autoload: ' . $dir);
218//                                        } else {
219//                                            $this->logger->debug('Keeping directory in autoload: ' . $dir);
220//                                        }
221//                                        return $exists;
222//                                    }
223//                                );
224//                            } else {
225//                                $dir = $packageDir . $packageRelativeDirectory;
226//                                if (! ($this->filesystem->directoryExists($dir) || $this->filesystem->fileExists($dir))) {
227//                                    $this->logger->info('Removing non-existent directory from autoload: ' . $dir);
228//                                    // /../../../vendor-prefixed/lib
229//                                    unset($autoload_key[$type][$originalNamespace]);
230//                                } else {
231//                                    $this->logger->debug('Keeping directory in autoload: ' . $dir);
232//                                }
233//                            }
234                        }
235                        break;
236                    default: // files, classmap, psr-0
237                        /**
238                         * E.g.
239                         *
240                         * * {"classmap":["src\/"]}
241                         * * {"psr-0":{"PayPal":"lib\/"}}
242                         * * {"files":["src\/functions.php"]}
243                         *
244                         * Also:
245                         * * {"exclude-from-classmap":["\/Tests\/"]}
246                         */
247
248//                        $autoload_key[$type] = array_filter($autoload, function ($file) use ($packageDir) {
249//                            $filename = $packageDir . '/' . $file;
250//                            $exists = $this->filesystem->directoryExists($filename) || $this->filesystem->fileExists($filename);
251//                            if (!$exists) {
252//                                $this->logger->info('Removing non-existent file from autoload: ' . $filename);
253//                            } else {
254//                                $this->logger->debug('Keeping file in autoload: ' . $filename);
255//                            }
256//                        });
257                        break;
258                }
259            }
260            $installedJsonArray['packages'][$key]['autoload'] = array_filter($autoload_key);
261        }
262
263        return $installedJsonArray;
264    }
265    /**
266     * @param array<string,ComposerPackage> $flatDependencyTree
267     * @param DiscoveredSymbols $discoveredSymbols
268     */
269    public function createAndCleanTargetDirInstalledJson(array $flatDependencyTree, DiscoveredSymbols $discoveredSymbols): void
270    {
271        $this->copyInstalledJson();
272
273        $vendorDir = $this->config->getTargetDirectory();
274
275        $installedJsonFile = $this->getJsonFile($vendorDir);
276
277        /**
278         * @var InstalledJsonArray $installedJsonArray
279         */
280        $installedJsonArray = $installedJsonFile->read();
281
282        $this->logger->debug('Installed.json before: ' . json_encode($installedJsonArray));
283
284        $installedJsonArray = $this->updatePackagePaths($installedJsonArray, $flatDependencyTree);
285
286        $installedJsonArray = $this->removeMissingPackages($installedJsonArray, $vendorDir);
287
288        $installedJsonArray = $this->updateNamespaces($installedJsonArray, $discoveredSymbols);
289
290        foreach ($installedJsonArray['packages'] as $index => $package) {
291            if (!in_array($package['name'], array_keys($flatDependencyTree))) {
292                unset($installedJsonArray['packages'][$index]);
293            }
294        }
295        $installedJsonArray['dev'] = false;
296        $installedJsonArray['dev-package-names'] = [];
297
298        $this->logger->debug('Installed.json after: ' . json_encode($installedJsonArray));
299
300        $this->logger->info('Writing installed.json to ' . $vendorDir);
301
302        $installedJsonFile->write($installedJsonArray);
303
304        $this->logger->info('Installed.json written to ' . $vendorDir);
305    }
306
307
308    /**
309     * Composer creates a file `vendor/composer/installed.json` which is uses when running `composer dump-autoload`.
310     * When `delete-vendor-packages` or `delete-vendor-files` is true, files and directories which have been deleted
311     * must also be removed from `installed.json` or Composer will throw an error.
312     *
313     * @param array<string,ComposerPackage> $flatDependencyTree
314     */
315    public function cleanupVendorInstalledJson(array $flatDependencyTree, DiscoveredSymbols $discoveredSymbols): void
316    {
317        $this->logger->info('Cleaning up installed.json');
318
319        $vendorDir = $this->config->getVendorDirectory();
320
321        $vendorInstalledJsonFile = $this->getJsonFile($vendorDir);
322
323        /**
324         * @var InstalledJsonArray $installedJsonArray
325         */
326        $installedJsonArray = $vendorInstalledJsonFile->read();
327
328        $installedJsonArray = $this->updatePackagePaths($installedJsonArray, $flatDependencyTree);
329
330        $installedJsonArray = $this->updateNamespaces($installedJsonArray, $discoveredSymbols);
331
332        $installedJsonArray = $this->removeMissingPackages($installedJsonArray, $vendorDir);
333
334        $vendorInstalledJsonFile->write($installedJsonArray);
335    }
336}