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