Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
26.23% covered (danger)
26.23%
16 / 61
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
FileEnumerator
26.23% covered (danger)
26.23%
16 / 61
25.00% covered (danger)
25.00%
1 / 4
80.85
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
 compileFileListForDependencies
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
3.02
 compileFileListForPaths
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 addFile
9.09% covered (danger)
9.09%
4 / 44
0.00% covered (danger)
0.00%
0 / 1
43.81
1<?php
2/**
3 * Build a list of files for the Composer packages.
4 */
5
6namespace BrianHenryIE\Strauss\Pipeline;
7
8use BrianHenryIE\Strauss\Composer\ComposerPackage;
9use BrianHenryIE\Strauss\Composer\DependenciesCollection;
10use BrianHenryIE\Strauss\Config\FileEnumeratorConfig;
11use BrianHenryIE\Strauss\Files\DiscoveredFiles;
12use BrianHenryIE\Strauss\Files\File;
13use BrianHenryIE\Strauss\Files\FileWithDependency;
14use BrianHenryIE\Strauss\Helpers\Flysystem\FileSystem;
15use League\Flysystem\FilesystemException;
16use Psr\Log\LoggerAwareTrait;
17use Psr\Log\LoggerInterface;
18
19class FileEnumerator
20{
21    use LoggerAwareTrait;
22
23    protected FileEnumeratorConfig $config;
24
25    protected FileSystem $filesystem;
26
27    protected DiscoveredFiles $discoveredFiles;
28
29    /**
30     * Copier constructor.
31     */
32    public function __construct(
33        FileEnumeratorConfig $config,
34        FileSystem $filesystem,
35        LoggerInterface $logger
36    ) {
37        $this->discoveredFiles = new DiscoveredFiles();
38
39        $this->config = $config;
40
41        $this->filesystem = $filesystem;
42
43        $this->logger = $logger;
44    }
45
46    /**
47     * @param DependenciesCollection $flatDependencies
48     *
49     * @throws FilesystemException
50     */
51    public function compileFileListForDependencies(DependenciesCollection $flatDependencies): DiscoveredFiles
52    {
53        /** @var ComposerPackage $dependency */
54        foreach ($flatDependencies as $dependency) {
55            $this->logger->info("Scanning for files for package {packageName}", ['packageName' => $dependency->getPackageName()]);
56            $dependencyPackageAbsolutePath = $dependency->getPackageAbsolutePath();
57            // Meta packages.
58            if (is_null($dependencyPackageAbsolutePath)) {
59                continue;
60            }
61            $this->compileFileListForPaths([$dependencyPackageAbsolutePath], $dependency);
62//            $absoluteFilePaths = $this->filesystem->findAllFilesAbsolutePaths([$dependencyPackageAbsolutePath]);
63//
64//            foreach ($absoluteFilePaths as $sourceAbsolutePath) {
65//                $this->addFile($sourceAbsolutePath, $dependency);
66//            }
67        }
68
69        $this->discoveredFiles->sort();
70        return $this->discoveredFiles;
71    }
72
73    /**
74     * @param string[] $paths
75     * @throws FilesystemException
76     */
77    public function compileFileListForPaths(array $paths, ?ComposerPackage $dependency = null): DiscoveredFiles
78    {
79        $absoluteFilePaths = $this->filesystem->findAllFilesAbsolutePaths($paths);
80
81        foreach ($absoluteFilePaths as $sourceAbsolutePath) {
82            $this->addFile($sourceAbsolutePath, $dependency);
83        }
84
85        $this->discoveredFiles->sort();
86        return $this->discoveredFiles;
87    }
88
89    /**
90     * @param string $sourceAbsoluteFilepath
91     * @param ?ComposerPackage $dependency
92     * @param ?string $autoloaderType
93     *
94     * @throws FilesystemException
95     * @uses DiscoveredFiles::add
96     *
97     */
98    protected function addFile(
99        string $sourceAbsoluteFilepath,
100        ?ComposerPackage $dependency = null,
101        ?string $autoloaderType = null
102    ): void {
103
104        if ($this->filesystem->directoryExists($sourceAbsoluteFilepath)) {
105            $this->logger->debug("Skipping directory at {sourcePath}", ['sourcePath' => $sourceAbsoluteFilepath]);
106            return;
107        }
108
109        // Do not add a file if its source does not exist!
110        if (!$this->filesystem->fileExists($sourceAbsoluteFilepath)) {
111            $this->logger->warning("File does not exist: {sourcePath}", ['sourcePath' => $sourceAbsoluteFilepath]);
112            return;
113        }
114
115        if ($dependency) {
116            $vendorRelativePath = $this->filesystem->getRelativePath(
117                $this->config->getAbsoluteVendorDirectory(),
118                $sourceAbsoluteFilepath
119            );
120
121            /** @var string $dependencyPackageAbsolutePath */
122            $dependencyPackageAbsolutePath = $dependency->getPackageAbsolutePath();
123            if ($vendorRelativePath === $sourceAbsoluteFilepath) {
124                $vendorRelativePath = $dependency->getRelativePath() . str_replace(
125                    FileSystem::normalizeDirSeparator($dependencyPackageAbsolutePath),
126                    '',
127                    FileSystem::normalizeDirSeparator($sourceAbsoluteFilepath)
128                );
129            }
130
131            /** @var FileWithDependency $f */
132            $f = $this->discoveredFiles->getFile($sourceAbsoluteFilepath)
133                ?? new FileWithDependency(
134                    $dependency,
135                    FileSystem::normalizeDirSeparator($vendorRelativePath),
136                    $this->filesystem->normalizePath($sourceAbsoluteFilepath),
137                    $this->config->getAbsoluteTargetDirectory(). '/' . $vendorRelativePath
138                );
139
140//            $f->setTargetAbsolutePath($this->config->getAbsoluteTargetDirectory() . '/' . $vendorRelativePath);
141
142            $autoloaderType && $f->addAutoloader($autoloaderType);
143
144//            if ($isOutsideProjectDir) {
145//                $f->setDoDelete(false);
146//            }
147        } else {
148            $vendorRelativePath = $this->filesystem->getRelativePath(
149                str_starts_with($sourceAbsoluteFilepath, $this->config->getAbsoluteVendorDirectory()) ? $this->config->getAbsoluteVendorDirectory() : $this->config->getAbsoluteTargetDirectory(),
150                $sourceAbsoluteFilepath,
151            );
152
153            $targetAbsolutePath = $this->config->getAbsoluteTargetDirectory() . '/' . $vendorRelativePath;
154
155            $f = $this->discoveredFiles->getFile($sourceAbsoluteFilepath)
156                 ?? new File(
157                     FileSystem::normalizeDirSeparator($sourceAbsoluteFilepath),
158                     $vendorRelativePath,
159                     $targetAbsolutePath
160                 );
161        }
162
163        $this->discoveredFiles->add($f);
164
165        $vendorRelativeFilePath =
166            $this->filesystem->getRelativePath(
167                $this->config->getProjectAbsolutePath(),
168                $f->getSourcePath()
169            );
170        $this->logger->info("Found file " . $vendorRelativeFilePath);
171    }
172}