Skip to content

Commit 9911430

Browse files
committed
Allow turning off src/dev matches
Signed-off-by: RJ Garcia <[email protected]>
1 parent 988b027 commit 9911430

File tree

2 files changed

+76
-33
lines changed

2 files changed

+76
-33
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Here's an example of the default configuration that is applied:
2525
"extra": {
2626
"php-inc": {
2727
"src-path": "src",
28-
"test-path": "test",
28+
"test-path": "tests",
2929
"matches": {
3030
"type": "and",
3131
"matches": [

src/PhpIncPlugin.php

Lines changed: 75 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -49,50 +49,93 @@ public function onPreAutoloadDump(Event $event) {
4949
$package = $event->getComposer()->getPackage();
5050

5151
$rootDir = dirname($event->getComposer()->getConfig()->get('vendor-dir'));
52-
$phpIncConfig = $package->getExtra()['php-inc'] ?? [];
53-
54-
$sourceDir = $rootDir . '/' . ($phpIncConfig['src-path'] ?? 'src');
55-
$testDir = $rootDir . '/' . ($phpIncConfig['test-path'] ?? 'tests');
56-
$standardMatches = [
57-
['type' => 'ext', 'exts' => ['php']],
58-
['type' => 'lowerCase'],
59-
];
60-
$matches = $phpIncConfig['matches'] ?? [
61-
'type' => 'and',
62-
'matches' => \array_merge($standardMatches, [
63-
['type' => 'excludePath', 'path' => '@.*/(Resources|Tests)/.*@']
64-
])
65-
];
66-
$devSrcMatches = $phpIncConfig['matches-dev-src'] ?? [
67-
'type' => 'and',
68-
'matches' => \array_merge($standardMatches, [
69-
['type' => 'includePath', 'path' => '@.*/Tests/.*@'],
70-
['type' => 'excludePath', 'path' => '@.*/Tests/.*/Fixtures/.*@'],
71-
]),
72-
];
73-
$devTestMatches = $phpIncConfig['matches-dev-test'] ?? [
74-
'type' => 'and',
75-
'matches' => \array_merge($standardMatches, [
76-
['type' => 'excludePath', 'path' => '@.*/Fixtures/.*@'],
77-
]),
78-
];
52+
$phpIncConfig = $package->getExtra()['php-inc'] ?? $this->createDefaultConfig();
7953

8054
$rootPrefix = $rootDir . '/';
81-
$additionalAutoloadFiles = _toArray(scannedFilesToIncludePaths($rootPrefix, scanSrc(astMatchFactory($matches))($sourceDir)));
82-
$additionalDevSrcAutoloadFiles = _toArray(scannedFilesToIncludePaths($rootPrefix, scanSrc(astMatchFactory($devSrcMatches))($sourceDir)));
83-
$additionalDevTestAutoloadFiles = _toArray(scannedFilesToIncludePaths($rootPrefix, scanSrc(astMatchFactory($devTestMatches))($testDir)));
55+
56+
$srcPath = $phpIncConfig['src-path'] ?? null;
57+
$testPath = $phpIncConfig['test-path'] ?? null;
58+
$matches = $phpIncConfig['matches'] ?? null;
59+
$devSrcMatches = $phpIncConfig['matches-dev-src'] ?? null;
60+
$devTestMatches = $phpIncConfig['matches-dev-test'] ?? null;
61+
62+
$additionalAutoloadFiles = [];
63+
$additionalAutoloadDevFiles = [];
64+
65+
if ($srcPath) {
66+
$sourceDir = $rootDir . '/' . $srcPath;
67+
$additionalAutoloadFiles = $this->matchFiles($rootPrefix, $matches, $sourceDir);
68+
$additionalAutoloadDevFiles = $this->matchFiles($rootPrefix, $devSrcMatches, $sourceDir);
69+
}
70+
if ($testPath) {
71+
$testDir = $rootDir . '/' . $testPath;
72+
$additionalAutoloadDevFiles = \array_merge($additionalAutoloadDevFiles, $this->matchFiles($rootPrefix, $devTestMatches, $testDir));
73+
}
8474

8575
if ($event->getIO()->isVerbose()) {
8676
$event->getIO()->write('<info>php-inc generating autoload files.</info>');
8777
$event->getIO()->write('Autoload Files:' . "\n" . \implode("\n", $additionalAutoloadFiles));
88-
$event->getIO()->write('Autoload Dev Files:' . "\n" . \implode("\n", \array_merge($additionalDevSrcAutoloadFiles, $additionalDevTestAutoloadFiles)));
78+
$event->getIO()->write('Autoload Dev Files:' . "\n" . \implode("\n", $additionalAutoloadDevFiles));
8979
}
9080

9181
$autoload = $package->getAutoload();
9282
$autoload['files'] = \array_unique(\array_merge($autoload['files'] ?? [], $additionalAutoloadFiles));
9383
$package->setAutoload($autoload);
9484
$autoloadDev = $package->getDevAutoload();
95-
$autoloadDev['files'] = \array_unique(\array_merge($autoload['files'] ?? [], $additionalDevSrcAutoloadFiles, $additionalDevTestAutoloadFiles));
85+
$autoloadDev['files'] = \array_unique(\array_merge($autoload['files'] ?? [], $additionalAutoloadDevFiles));
9686
$package->setDevAutoload($autoloadDev);
9787
}
88+
89+
private function matchFiles(string $rootPrefix, ?array $matches, string $dir): array {
90+
if (!$matches) {
91+
return [];
92+
}
93+
94+
return _toArray(scannedFilesToIncludePaths($rootPrefix, scanSrc(astMatchFactory($matches))($dir)));
95+
}
96+
97+
private function writeVerbose(IOInterface $io, string $line) {
98+
if (!$io->isVerbose()) {
99+
return;
100+
}
101+
102+
$io->write($line);
103+
}
104+
105+
/** @return mixed[] */
106+
private function createDefaultConfig(): array {
107+
// this is pulled directly from the README config
108+
return json_decode(<<<JSON
109+
{
110+
"src-path": "src",
111+
"test-path": "tests",
112+
"matches": {
113+
"type": "and",
114+
"matches": [
115+
{"type": "ext", "exts": ["php"]},
116+
{"type": "lowerCase"},
117+
{"type": "excludePath", "path": "@.*/(Resources|Tests)/.*@"}
118+
]
119+
},
120+
"matches-dev-src": {
121+
"type": "and",
122+
"matches": [
123+
{"type": "ext", "exts": ["php"]},
124+
{"type": "lowerCase"},
125+
{"type": "includePath", "path": "@.*/Tests/.*@"},
126+
{"type": "excludePath", "path": "@.*/Tests/.*/Fixtures/.*@"}
127+
]
128+
},
129+
"matches-dev-test": {
130+
"type": "and",
131+
"matches": [
132+
{"type": "ext", "exts": ["php"]},
133+
{"type": "lowerCase"},
134+
{"type": "excludePath", "path": "@.*/Fixtures/.*@"}
135+
]
136+
}
137+
}
138+
JSON
139+
);
140+
}
98141
}

0 commit comments

Comments
 (0)