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
36 changes: 31 additions & 5 deletions src/Drupal/ExtensionDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use mglaman\PHPStanDrupal\Drupal\Extension;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Symfony\Component\Finder\Finder;
use function array_filter;
use function array_flip;
use function array_multisort;
Expand Down Expand Up @@ -72,11 +73,11 @@ class ExtensionDiscovery
protected $root;

/**
* The site path.
* The site paths.
*
* @var string
* @var string[]
*/
protected $sitePath;
protected $sitePaths;

/**
* Constructs a new ExtensionDiscovery object.
Expand All @@ -90,7 +91,27 @@ public function __construct($root)
$this->profileDirectories = [
$root . '/core/profiles/standard'
];
$this->sitePath = 'sites/default';
$this->sitePaths = $this->discoverSitePaths();
}

/**
* Discovers all site-specific directories under sites/.
*
* @return string[]
* An array of site paths relative to the root (e.g. 'sites/default').
*/
private function discoverSitePaths(): array
{
$sitesDir = $this->root . '/sites';
if (!is_dir($sitesDir)) {
return [];
}
$finder = Finder::create()->directories()->in($sitesDir)->depth(0)->exclude(['all', 'default', 'simpletest']);
$paths = [];
foreach ($finder as $dir) {
$paths[] = 'sites/' . $dir->getFilename();
}
return $paths;
}

/**
Expand Down Expand Up @@ -152,7 +173,12 @@ public function scan($type)
// type specific directory names only.
$searchdirs[self::ORIGIN_ROOT] = '';

$searchdirs[self::ORIGIN_SITE] = $this->sitePath;
// Search the default site-specific directory, plus any additional site
// directories discovered for multisite setups.
$searchdirs[self::ORIGIN_SITE] = 'sites/default';
foreach ($this->sitePaths as $sitePath) {
$searchdirs[] = $sitePath;
}

$files = [];
foreach ($searchdirs as $dir) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

/**
* @file
* Include file for the site specific module fixture.
*/

function site_specific_module_helper(): string {
return 'helper';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: Acme site specific module
type: module
core: 8.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

/**
* @file
* Module file for the site specific module fixture.
*/
5 changes: 5 additions & 0 deletions tests/src/Rules/LoadIncludesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public static function cases(): \Generator
],
]
];

yield 'multisite module in sites/acme/' => [
[__DIR__.'/data/multisite-load-include.php'],
[]
];
}

}
14 changes: 14 additions & 0 deletions tests/src/Rules/data/multisite-load-include.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace MultisiteLoadInclude;

/**
* Tests that loadInclude works for modules in non-default site directories.
*
* This demonstrates a bug where ExtensionDiscovery only scanned sites/default,
* causing modules under other site directories (e.g. sites/acme/)
* to not be found.
*/
function test_multisite_load_include(): void {
\Drupal::moduleHandler()->loadInclude('acme_module', 'inc');
}
Loading