Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Autoload
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 generate
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Generate an `autoload.php` file in the root of the target directory.
4 *
5 * @see \Composer\Autoload\ClassMapGenerator
6 */
7
8namespace BrianHenryIE\Strauss\Pipeline;
9
10use BrianHenryIE\Strauss\Composer\ComposerPackage;
11use BrianHenryIE\Strauss\Composer\Extra\StraussConfig;
12use BrianHenryIE\Strauss\Config\AutoloadConfigInterface;
13use BrianHenryIE\Strauss\Helpers\FileSystem;
14use BrianHenryIE\Strauss\Pipeline\Autoload\ComposerAutoloadGeneratorFactory;
15use BrianHenryIE\Strauss\Pipeline\Autoload\DumpAutoload;
16use BrianHenryIE\Strauss\Pipeline\Cleanup\InstalledJson;
17use BrianHenryIE\Strauss\Types\DiscoveredSymbols;
18use Exception;
19use League\Flysystem\FilesystemException;
20use Psr\Log\LoggerAwareTrait;
21use Psr\Log\LoggerInterface;
22use Psr\Log\NullLogger;
23use Seld\JsonLint\ParsingException;
24
25class Autoload
26{
27    use LoggerAwareTrait;
28
29    protected FileSystem $filesystem;
30
31    /**
32     * @var StraussConfig&AutoloadConfigInterface
33     */
34    protected StraussConfig $config;
35
36    /**
37     * The files autoloaders of packages that have been copied by Strauss.
38     * Keyed by package path.
39     *
40     * @var array<string, array<string>> $discoveredFilesAutoloaders Array of packagePath => array of relativeFilePaths.
41     */
42    protected array $discoveredFilesAutoloaders;
43
44    /**
45     * Autoload constructor.
46     *
47     * @param StraussConfig&AutoloadConfigInterface $config
48     * @param array<string, array<string>> $discoveredFilesAutoloaders
49     */
50    public function __construct(
51        StraussConfig $config,
52        array $discoveredFilesAutoloaders,
53        Filesystem $filesystem,
54        ?LoggerInterface $logger = null
55    ) {
56        $this->config = $config;
57        $this->discoveredFilesAutoloaders = $discoveredFilesAutoloaders;
58        $this->filesystem = $filesystem;
59        $this->setLogger($logger ?? new NullLogger());
60    }
61
62    /**
63     * @param array<string,ComposerPackage> $flatDependencyTree
64     * @throws FilesystemException
65     * @throws ParsingException
66     * @throws Exception
67     */
68    public function generate(array $flatDependencyTree, DiscoveredSymbols $discoveredSymbols): void
69    {
70        if (!$this->config->isClassmapOutput()) {
71            $this->logger->debug('Not generating autoload.php because classmap output is disabled.');
72            // TODO: warn about `files` autoloaders.
73            // TODO: list the files autoloaders that will be missed.
74            return;
75        }
76
77        $this->logger->info('Generating autoload files for ' . $this->config->getAbsoluteTargetDirectory());
78
79        if (!$this->config->isTargetDirectoryVendor()) {
80            $installedJson = new InstalledJson(
81                $this->config,
82                $this->filesystem,
83                $this->logger
84            );
85            $installedJson->cleanTargetDirInstalledJson($flatDependencyTree, $discoveredSymbols);
86        }
87
88        (new DumpAutoload(
89            $this->config,
90            $this->filesystem,
91            $this->logger,
92            new Prefixer(
93                $this->config,
94                $this->filesystem,
95                $this->logger
96            ),
97            new FileEnumerator(
98                $this->config,
99                $this->filesystem,
100                $this->logger
101            ),
102            new ComposerAutoloadGeneratorFactory()
103        ))->generatedPrefixedAutoloader();
104    }
105}