Skip to content

Commit 63ac7e6

Browse files
authored
Merge pull request #64 from cakephp/2.0-pre-autoload-dump
Add autoload paths for app plugins.
2 parents be18d9d + e89cd22 commit 63ac7e6

4 files changed

Lines changed: 109 additions & 19 deletions

File tree

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* text eol=lf
55

66
# Remove files for archives generated using `git archive`
7+
.phive export-ignore
78
.editorconfig export-ignore
89
phpunit.xml.dist export-ignore
910
phpcs.xml.dist export-ignore

.phive/phars.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
<phive xmlns="https://phar.io/phive">
33
<phar name="phpstan" version="1.10.14" installed="1.10.14" location="./tools/phpstan" copy="false"/>
44
<phar name="psalm" version="5.10.0" installed="5.10.0" location="./tools/psalm" copy="false"/>
5-
</phive>
5+
</phive>

src/Plugin.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,55 @@ public static function getSubscribedEvents()
4242
{
4343
return [
4444
'post-autoload-dump' => 'postAutoloadDump',
45+
'pre-autoload-dump' => 'preAutoloadDump',
4546
];
4647
}
4748

49+
/**
50+
* Add PSR-4 autoload paths for app plugins.
51+
*
52+
* @param \Composer\Script\Event $event
53+
* @return void
54+
*/
55+
public function preAutoloadDump(Event $event): void
56+
{
57+
$package = $event->getComposer()->getPackage();
58+
$autoload = $package->getAutoload();
59+
$devAutoload = $package->getDevAutoload();
60+
61+
$extra = $package->getExtra();
62+
if (empty($extra['plugin-paths'])) {
63+
$extra['plugin-paths'] = ['plugins'];
64+
}
65+
66+
$root = dirname(realpath($event->getComposer()->getConfig()->get('vendor-dir'))) . '/';
67+
foreach ($extra['plugin-paths'] as $pluginsPath) {
68+
foreach (new DirectoryIterator($root . $pluginsPath) as $fileInfo) {
69+
if (!$fileInfo->isDir() || $fileInfo->isDot()) {
70+
continue;
71+
}
72+
73+
$folderName = $fileInfo->getFilename();
74+
if ($folderName[0] === '.') {
75+
continue;
76+
}
77+
78+
$pluginNamespace = $folderName . '\\';
79+
$pluginTestNamespace = $folderName . '\\Test\\';
80+
$path = $pluginsPath . '/' . $folderName . '/';
81+
if (!isset($autoload['psr-4'][$pluginNamespace]) && is_dir($root . $path . '/src')) {
82+
$autoload['psr-4'][$pluginNamespace] = $path . 'src';
83+
}
84+
if (!isset($devAutoload['psr-4'][$pluginTestNamespace]) && is_dir($root . $path . '/tests')) {
85+
$devAutoload['psr-4'][$pluginTestNamespace] = $path . 'tests';
86+
}
87+
}
88+
}
89+
90+
$package->setAutoload($autoload);
91+
$package->setDevAutoload($devAutoload);
92+
}
93+
4894
/**
4995
* Called whenever composer (re)generates the autoloader.
5096
*

tests/TestCase/PluginTest.php

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
use Composer\Config;
99
use Composer\IO\IOInterface;
1010
use Composer\Package\Package;
11+
use Composer\Package\RootPackage;
1112
use Composer\Repository\RepositoryManager;
13+
use Composer\Script\Event;
1214
use Composer\Util\HttpDownloader;
1315
use PHPUnit\Framework\TestCase;
1416

@@ -31,15 +33,14 @@ class PluginTest extends TestCase
3133
* @var array<string>
3234
*/
3335
protected array $testDirs = [
34-
'',
3536
'vendor',
36-
'plugins',
3737
'plugins/Foo',
38-
'plugins/Fee',
39-
'plugins/Foe',
38+
'plugins/Fee/src',
39+
'plugins/Fee/tests',
40+
'plugins/Foe/src',
4041
'plugins/Fum',
41-
'app_plugins',
42-
'app_plugins/Bar',
42+
'app_plugins/Bar/src',
43+
'app_plugins/Bar/tests',
4344
];
4445

4546
protected string $path;
@@ -60,14 +61,16 @@ public function setUp(): void
6061

6162
foreach ($this->testDirs as $dir) {
6263
if (!is_dir($this->path . '/' . $dir)) {
63-
mkdir($this->path . '/' . $dir);
64+
mkdir($this->path . '/' . $dir, 0777, true);
6465
}
6566
}
6667

6768
$this->composer = new Composer();
6869
$config = new Config();
6970
$config->merge([
70-
'vendor-dir' => $this->path . '/vendor',
71+
'config' => [
72+
'vendor-dir' => $this->path . '/vendor',
73+
],
7174
]);
7275

7376
$this->composer->setConfig($config);
@@ -92,23 +95,18 @@ public function tearDown(): void
9295
{
9396
parent::tearDown();
9497

95-
$dirs = array_reverse($this->testDirs);
96-
97-
if (is_file($this->path . '/vendor/cakephp-plugins.php')) {
98-
unlink($this->path . '/vendor/cakephp-plugins.php');
99-
}
100-
101-
foreach ($dirs as $dir) {
102-
if (is_dir($this->path . '/' . $dir)) {
103-
rmdir($this->path . '/' . $dir);
104-
}
98+
if (PHP_OS === 'Windows') {
99+
exec(sprintf('rd /s /q %s', escapeshellarg($this->path)));
100+
} else {
101+
exec(sprintf('rm -rf %s', escapeshellarg($this->path)));
105102
}
106103
}
107104

108105
public function testGetSubscribedEvents()
109106
{
110107
$expected = [
111108
'post-autoload-dump' => 'postAutoloadDump',
109+
'pre-autoload-dump' => 'preAutoloadDump',
112110
];
113111

114112
$this->assertSame($expected, $this->plugin->getSubscribedEvents());
@@ -120,6 +118,51 @@ public function testGetConfigFilePath()
120118
$this->assertFileExists(dirname($path));
121119
}
122120

121+
public function testPreAutoloadDump()
122+
{
123+
$package = new RootPackage('App', '1.0.0', '1.0.0');
124+
$package->setExtra([
125+
'plugin-paths' => [
126+
'app_plugins',
127+
'plugins',
128+
],
129+
]);
130+
$package->setAutoload([
131+
'psr-4' => [
132+
'Foo\\' => 'xyz/Foo/src',
133+
],
134+
]);
135+
$package->setDevAutoload([
136+
'psr-4' => [
137+
'Foo\Test\\' => 'xyz/Foo/tests',
138+
],
139+
]);
140+
$this->composer->setPackage($package);
141+
142+
$event = new Event('', $this->composer, $this->io);
143+
144+
$this->plugin->preAutoloadDump($event);
145+
146+
$expected = [
147+
'psr-4' => [
148+
'Foo\\' => 'xyz/Foo/src',
149+
'Fee\\' => 'plugins/Fee/src',
150+
'Foe\\' => 'plugins/Foe/src',
151+
'Bar\\' => 'app_plugins/Bar/src',
152+
],
153+
];
154+
$this->assertEquals($expected, $package->getAutoload());
155+
156+
$expected = [
157+
'psr-4' => [
158+
'Foo\Test\\' => 'xyz/Foo/tests',
159+
'Fee\Test\\' => 'plugins/Fee/tests',
160+
'Bar\Test\\' => 'app_plugins/Bar/tests',
161+
],
162+
];
163+
$this->assertEquals($expected, $package->getDevAutoload());
164+
}
165+
123166
public function testGetPrimaryNamespace()
124167
{
125168
$autoload = [

0 commit comments

Comments
 (0)