Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* text eol=lf

# Remove files for archives generated using `git archive`
.phive export-ignore
.editorconfig export-ignore
phpunit.xml.dist export-ignore
phpcs.xml.dist export-ignore
Expand Down
2 changes: 1 addition & 1 deletion .phive/phars.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
<phive xmlns="https://phar.io/phive">
<phar name="phpstan" version="1.10.14" installed="1.10.14" location="./tools/phpstan" copy="false"/>
<phar name="psalm" version="5.10.0" installed="5.10.0" location="./tools/psalm" copy="false"/>
</phive>
</phive>
46 changes: 46 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,55 @@ public static function getSubscribedEvents()
{
return [
'post-autoload-dump' => 'postAutoloadDump',
'pre-autoload-dump' => 'preAutoloadDump',
];
}

/**
* Add PSR-4 autoload paths for app plugins.
*
* @param \Composer\Script\Event $event
* @return void
*/
public function preAutoloadDump(Event $event): void
{
$package = $event->getComposer()->getPackage();
$autoload = $package->getAutoload();
$devAutoload = $package->getDevAutoload();

$extra = $package->getExtra();
if (empty($extra['plugin-paths'])) {
$extra['plugin-paths'] = ['plugins'];
}

$root = dirname(realpath($event->getComposer()->getConfig()->get('vendor-dir'))) . '/';
foreach ($extra['plugin-paths'] as $pluginsPath) {
foreach (new DirectoryIterator($root . $pluginsPath) as $fileInfo) {
if (!$fileInfo->isDir() || $fileInfo->isDot()) {
continue;
}

$folderName = $fileInfo->getFilename();
if ($folderName[0] === '.') {
continue;
}

$pluginNamespace = $folderName . '\\';
$pluginTestNamespace = $folderName . '\\Test\\';
$path = $pluginsPath . '/' . $folderName . '/';
if (!isset($autoload['psr-4'][$pluginNamespace]) && is_dir($root . $path . '/src')) {
$autoload['psr-4'][$pluginNamespace] = $path . 'src';
}
if (!isset($devAutoload['psr-4'][$pluginTestNamespace]) && is_dir($root . $path . '/tests')) {
$devAutoload['psr-4'][$pluginTestNamespace] = $path . 'tests';
}
}
}

$package->setAutoload($autoload);
$package->setDevAutoload($devAutoload);
}

/**
* Called whenever composer (re)generates the autoloader.
*
Expand Down
79 changes: 61 additions & 18 deletions tests/TestCase/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use Composer\Config;
use Composer\IO\IOInterface;
use Composer\Package\Package;
use Composer\Package\RootPackage;
use Composer\Repository\RepositoryManager;
use Composer\Script\Event;
use Composer\Util\HttpDownloader;
use PHPUnit\Framework\TestCase;

Expand All @@ -31,15 +33,14 @@ class PluginTest extends TestCase
* @var array<string>
*/
protected array $testDirs = [
'',
'vendor',
'plugins',
'plugins/Foo',
'plugins/Fee',
'plugins/Foe',
'plugins/Fee/src',
'plugins/Fee/tests',
'plugins/Foe/src',
'plugins/Fum',
'app_plugins',
'app_plugins/Bar',
'app_plugins/Bar/src',
'app_plugins/Bar/tests',
];

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

foreach ($this->testDirs as $dir) {
if (!is_dir($this->path . '/' . $dir)) {
mkdir($this->path . '/' . $dir);
mkdir($this->path . '/' . $dir, 0777, true);
}
}

$this->composer = new Composer();
$config = new Config();
$config->merge([
'vendor-dir' => $this->path . '/vendor',
'config' => [
'vendor-dir' => $this->path . '/vendor',
],
]);

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

$dirs = array_reverse($this->testDirs);

if (is_file($this->path . '/vendor/cakephp-plugins.php')) {
unlink($this->path . '/vendor/cakephp-plugins.php');
}

foreach ($dirs as $dir) {
if (is_dir($this->path . '/' . $dir)) {
rmdir($this->path . '/' . $dir);
}
if (PHP_OS === 'Windows') {
exec(sprintf('rd /s /q %s', escapeshellarg($this->path)));
} else {
exec(sprintf('rm -rf %s', escapeshellarg($this->path)));
}
}

public function testGetSubscribedEvents()
{
$expected = [
'post-autoload-dump' => 'postAutoloadDump',
'pre-autoload-dump' => 'preAutoloadDump',
];

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

public function testPreAutoloadDump()
{
$package = new RootPackage('App', '1.0.0', '1.0.0');
$package->setExtra([
'plugin-paths' => [
'app_plugins',
'plugins',
],
]);
$package->setAutoload([
'psr-4' => [
'Foo\\' => 'xyz/Foo/src',
],
]);
$package->setDevAutoload([
'psr-4' => [
'Foo\Test\\' => 'xyz/Foo/tests',
],
]);
$this->composer->setPackage($package);

$event = new Event('', $this->composer, $this->io);

$this->plugin->preAutoloadDump($event);

$expected = [
'psr-4' => [
'Foo\\' => 'xyz/Foo/src',
'Fee\\' => 'plugins/Fee/src',
'Foe\\' => 'plugins/Foe/src',
'Bar\\' => 'app_plugins/Bar/src',
],
];
$this->assertEquals($expected, $package->getAutoload());

$expected = [
'psr-4' => [
'Foo\Test\\' => 'xyz/Foo/tests',
'Fee\Test\\' => 'plugins/Fee/tests',
'Bar\Test\\' => 'app_plugins/Bar/tests',
],
];
$this->assertEquals($expected, $package->getDevAutoload());
}

public function testGetPrimaryNamespace()
{
$autoload = [
Expand Down