Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 22
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 / 22
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 / 18
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\Extra\StraussConfig;
11use BrianHenryIE\Strauss\Config\AutoloadConfigInterace;
12use BrianHenryIE\Strauss\Helpers\FileSystem;
13use BrianHenryIE\Strauss\Pipeline\Autoload\DumpAutoload;
14use BrianHenryIE\Strauss\Pipeline\Cleanup\InstalledJson;
15use BrianHenryIE\Strauss\Types\DiscoveredSymbols;
16use Psr\Log\LoggerAwareTrait;
17use Psr\Log\LoggerInterface;
18use Psr\Log\NullLogger;
19
20class Autoload
21{
22    use LoggerAwareTrait;
23
24    protected FileSystem $filesystem;
25
26    protected AutoloadConfigInterace $config;
27
28    /**
29     * The files autoloaders of packages that have been copied by Strauss.
30     * Keyed by package path.
31     *
32     * @var array<string, array<string>> $discoveredFilesAutoloaders Array of packagePath => array of relativeFilePaths.
33     */
34    protected array $discoveredFilesAutoloaders;
35
36    protected string $absoluteTargetDirectory;
37
38    /**
39     * Autoload constructor.
40     *
41     * @param StraussConfig $config
42     * @param array<string, array<string>> $discoveredFilesAutoloaders
43     */
44    public function __construct(
45        AutoloadConfigInterace $config,
46        array $discoveredFilesAutoloaders,
47        Filesystem $filesystem,
48        ?LoggerInterface $logger = null
49    ) {
50        $this->config = $config;
51        $this->discoveredFilesAutoloaders = $discoveredFilesAutoloaders;
52        $this->filesystem = $filesystem;
53        $this->setLogger($logger ?? new NullLogger());
54    }
55
56    public function generate(array $flatDependencyTree, DiscoveredSymbols $discoveredSymbols): void
57    {
58        // Use native Composer's `autoload.php` etc. when the target directory is the vendor directory.
59        if ($this->config->getTargetDirectory() === $this->config->getVendorDirectory()) {
60            $this->logger->debug('Not generating autoload.php because the target directory is the vendor directory.');
61            return;
62        }
63
64        if (!$this->config->isClassmapOutput()) {
65            $this->logger->debug('Not generating autoload.php because classmap output is disabled.');
66            return;
67        }
68
69        $this->logger->info('Generating autoload files for ' . $this->config->getTargetDirectory());
70
71        $installedJson = new InstalledJson(
72            $this->config,
73            $this->filesystem,
74            $this->logger
75        );
76        $installedJson->createAndCleanTargetDirInstalledJson($flatDependencyTree, $discoveredSymbols);
77
78        (new DumpAutoload(
79            $this->config,
80            $this->filesystem,
81            $this->logger
82        ))->generatedPrefixedAutoloader();
83    }
84}