forked from coenjacobs/mozart
-
Notifications
You must be signed in to change notification settings - Fork 23
/
FileEnumerator.php
167 lines (132 loc) · 5.38 KB
/
FileEnumerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* Build a list of files from the composer autoloaders.
*/
namespace BrianHenryIE\Strauss;
use BrianHenryIE\Strauss\Composer\ComposerPackage;
use BrianHenryIE\Strauss\Composer\Extra\StraussConfig;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Symfony\Component\Finder\Finder;
class FileEnumerator
{
/**
* The only path variable with a leading slash.
* All directories in project end with a slash.
*
* @var string
*/
protected string $workingDir;
/** @var ComposerPackage[] */
protected array $dependencies;
protected array $excludePackageNames = array();
protected array $excludeNamespaces = array();
protected array $excludeFilePatterns = array();
/** @var Filesystem */
protected Filesystem $filesystem;
/**
* Complete list of files specified in packages autoloaders.
*
* Relative filepaths as key, with their dependency as the value.
*
* Relative from vendor/
*
* @var array<string, ComposerPackage>
*/
protected array $filesWithDependencies = [];
/**
* Copier constructor.
* @param ComposerPackage[] $dependencies
* @param string $workingDir
*/
public function __construct(
array $dependencies,
string $workingDir,
StraussConfig $config
) {
$this->workingDir = $workingDir;
$this->dependencies = $dependencies;
$this->excludeNamespaces = $config->getExcludeNamespacesFromCopy();
$this->excludePackageNames = $config->getExcludePackagesFromCopy();
$this->excludeFilePatterns = $config->getExcludeFilePatternsFromCopy();
$this->filesystem = new Filesystem(new Local($this->workingDir));
}
/**
* Read the autoload keys of the dependencies and generate a list of the files referenced.
*/
public function compileFileList()
{
// TODO: read 'vendor' from composer.json.
$prefixToRemove = $this->workingDir .'vendor'. DIRECTORY_SEPARATOR;
foreach ($this->dependencies as $dependency) {
if (in_array($dependency->getName(), $this->excludePackageNames)) {
continue;
}
$packagePath = $this->workingDir . 'vendor' . DIRECTORY_SEPARATOR
. $dependency->getPath() . DIRECTORY_SEPARATOR;
/**
* Where $dependency->autoload is ~
*
* [ "psr-4" => [ "BrianHenryIE\Strauss" => "src" ] ]
*/
$autoloaders = $dependency->getAutoload();
foreach ($autoloaders as $_type => $value) {
// Might have to switch/case here.
foreach ($value as $namespace => $namespace_relative_path) {
if (!empty($namespace) && in_array($namespace, $this->excludeNamespaces)) {
continue;
}
if (is_file($packagePath . $namespace_relative_path)) {
// $finder->files()->name($file)->in($source_path);
$relativeFilepath = str_replace($prefixToRemove, '', $packagePath . $namespace_relative_path);
$this->filesWithDependencies[$relativeFilepath] = $dependency;
continue;
}
// else it is a directory.
// trailingslashit().
$namespace_relative_path = rtrim($namespace_relative_path, DIRECTORY_SEPARATOR)
. DIRECTORY_SEPARATOR;
$sourcePath = $packagePath . $namespace_relative_path;
// trailingslashit(). (to remove duplicates).
$sourcePath = rtrim($sourcePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
$finder = new Finder();
$finder->files()->in($sourcePath)->followLinks();
foreach ($finder as $foundFile) {
$filePath = $foundFile->getPathname();
$relativeFilepath = str_replace($prefixToRemove, '', $filePath);
// TODO: Is this needed here?! If anything, it's the prefix that needs to be normalised a few
// lines above before being used.
// Replace multiple \ and/or / with OS native DIRECTORY_SEPARATOR.
$relativeFilepath = preg_replace('#[\\\/]+#', DIRECTORY_SEPARATOR, $relativeFilepath);
foreach ($this->excludeFilePatterns as $excludePattern) {
if (1 === preg_match($excludePattern, $relativeFilepath)) {
continue 2;
}
}
$this->filesWithDependencies[$relativeFilepath] = $dependency;
}
}
}
}
}
/**
* Returns all found files.
*
* @return array<string, ComposerPackage>
*/
public function getAllFilesAndDependencyList(): array
{
return $this->filesWithDependencies;
}
/**
* Returns found PHP files.
*
* @return array<string, ComposerPackage>
*/
public function getPhpFilesAndDependencyList(): array
{
return array_filter($this->filesWithDependencies, function ($value, $key) {
return false !== strpos($key, '.php');
}, ARRAY_FILTER_USE_BOTH);
}
}