Skip to content

Commit

Permalink
Add support for multiple autoloaders (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
webflo authored Jun 28, 2024
1 parent 1fa6548 commit 7304506
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 10 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
},
"require-dev": {
"phpunit/phpunit": "^10.4",
"mikey179/vfsstream": "^1.6"
"mikey179/vfsstream": "^1.6",
"symfony/process": "^6.4"
},
"config": {
"platform": {
Expand Down
17 changes: 13 additions & 4 deletions src/DrupalFinderComposerRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace DrupalFinder;

use Composer\InstalledVersions;
use Composer\Autoload\ClassLoader;

class DrupalFinderComposerRuntime
{
Expand All @@ -16,15 +17,20 @@ class DrupalFinderComposerRuntime
*/
public function getDrupalRoot(): ?string
{
$core = InstalledVersions::getInstallPath('drupal/core');
return $core ? realpath(dirname($core)) : null;
$core = InstalledVersions::getInstallPath('drupal/core');
return $core ? realpath(dirname($core)) : null;
}

/**
* Get the path to the Composer root directory.
*/
public function getComposerRoot(): ?string
{
foreach (InstalledVersions::getAllRawData() as $data) {
if (isset($data['versions']['drupal/core'])) {
return realpath($data['root']['install_path']);
}
}
$root = InstalledVersions::getRootPackage();
return realpath($root['install_path']);
}
Expand All @@ -34,8 +40,11 @@ public function getComposerRoot(): ?string
*/
public function getVendorDir(): ?string
{
$reflection = new \ReflectionClass(InstalledVersions::class);
return realpath(dirname(dirname($reflection->getFileName())));
foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) {
if ($loader->findFile(\Drupal::class)) {
return realpath($vendorDir);
}
}
}

}
2 changes: 2 additions & 0 deletions tests/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[composer.json]
indent_size = 2
32 changes: 28 additions & 4 deletions tests/DrupalFinderComposerRuntimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace DrupalFinder\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

class DrupalFinderComposerRuntimeTest extends TestCase {

Expand All @@ -12,11 +14,33 @@ class DrupalFinderComposerRuntimeTest extends TestCase {
* @runInSeparateProcess
*/
public function testDefault() {
$basePath = realpath( __DIR__ . '/fixtures/default');
$basePath = realpath(__DIR__ . '/fixtures/default');
$this->assertDirectoryExists($basePath . '/vendor', static::installFixtures);
$this->assertDirectoryExists($basePath . '/web', static::installFixtures);

$result = json_decode(require $basePath . '/drupal-finder.php', TRUE);
$result = json_decode(require $basePath . '/drupal-finder.php', TRUE);
$this->assertSame($result['getComposerRoot'], $basePath);
$this->assertSame($result['getVendorDir'], $basePath . '/vendor');
$this->assertSame($result['getDrupalRoot'], $basePath . '/web');
}

/**
* @runInSeparateProcess
*/
public function testDefaultComposerScript() {
$basePath = realpath(__DIR__ . '/fixtures/default');
$this->assertDirectoryExists($basePath . '/vendor', static::installFixtures);
$this->assertDirectoryExists($basePath . '/web', static::installFixtures);

$process = new Process(['composer', 'run-script', 'dump-drupal-finder'], $basePath);
$process->run();

// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}

$result = json_decode($process->getOutput(), TRUE);
$this->assertSame($result['getComposerRoot'], $basePath);
$this->assertSame($result['getVendorDir'], $basePath . '/vendor');
$this->assertSame($result['getDrupalRoot'], $basePath . '/web');
Expand All @@ -26,11 +50,11 @@ public function testDefault() {
* @runInSeparateProcess
*/
public function testCustomVendor() {
$basePath = realpath( __DIR__ . '/fixtures/custom-vendor');
$basePath = realpath(__DIR__ . '/fixtures/custom-vendor');
$this->assertDirectoryExists($basePath . '/foo/bar', static::installFixtures);
$this->assertDirectoryExists($basePath . '/foo/bar/drupal', static::installFixtures);

$result = json_decode(require $basePath . '/drupal-finder.php', TRUE);
$result = json_decode(require $basePath . '/drupal-finder.php', TRUE);
$this->assertSame($result['getComposerRoot'], $basePath);
$this->assertSame($result['getVendorDir'], $basePath . '/foo/bar');
$this->assertSame($result['getDrupalRoot'], $basePath . '/foo/bar/drupal');
Expand Down
19 changes: 19 additions & 0 deletions tests/fixtures/default/TestAsComposerScript.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace DrupalFinder\Tests\Fixtures\Default;

use Composer\Script\Event;
use DrupalFinder\DrupalFinderComposerRuntime;

class TestAsComposerScript {

public static function dumpDrupalFinder(Event $event) {
$finder = new DrupalFinderComposerRuntime();
$event->getIO()->writeRaw(json_encode([
'getComposerRoot' => $finder->getComposerRoot(),
'getVendorDir' => $finder->getVendorDir(),
'getDrupalRoot' => $finder->getDrupalRoot(),
]));
}

}
10 changes: 9 additions & 1 deletion tests/fixtures/default/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,13 @@
}
},
"minimum-stability": "dev",
"prefer-stable": true
"prefer-stable": true,
"autoload": {
"classmap": [
"TestAsComposerScript.php"
]
},
"scripts": {
"dump-drupal-finder": "\\DrupalFinder\\Tests\\Fixtures\\Default\\TestAsComposerScript::dumpDrupalFinder"
}
}

0 comments on commit 7304506

Please sign in to comment.