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