Skip to content

Commit e4e4129

Browse files
committed
[#16] Use webflo/drupal-finder to find docroot.
Signed-off-by: Jibran Ijaz <[email protected]>
1 parent 410ea5a commit e4e4129

File tree

3 files changed

+11
-58
lines changed

3 files changed

+11
-58
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"require": {
1212
"php": "^7.1",
1313
"phpstan/phpstan": "^0.10.6",
14-
"symfony/yaml": "~3.4.5|^4.2"
14+
"symfony/yaml": "~3.4.5|^4.2",
15+
"webflo/drupal-finder": "^1.1"
1516
},
1617
"autoload": {
1718
"psr-4": {

src/DependencyInjection/DrupalExtension.php

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\DependencyInjection;
44

5+
use DrupalFinder\DrupalFinder;
56
use Nette\DI\CompilerExtension;
67
use Nette\DI\Config\Helpers;
78
use PHPStan\Drupal\ExtensionDiscovery;
@@ -19,11 +20,6 @@ class DrupalExtension extends CompilerExtension
1920
'themes' => [],
2021
];
2122

22-
/**
23-
* @var ?string
24-
*/
25-
private $autoloaderPath;
26-
2723
/**
2824
* @var string
2925
*/
@@ -55,27 +51,9 @@ class DrupalExtension extends CompilerExtension
5551

5652
public function loadConfiguration(): void
5753
{
58-
59-
$this->autoloaderPath = $GLOBALS['autoloaderInWorkingDirectory'];
60-
$realpath = realpath($this->autoloaderPath);
61-
if ($realpath === false) {
62-
throw new \InvalidArgumentException('Cannot determine the realpath of the autoloader.');
63-
}
64-
$project_root = dirname($realpath, 2);
65-
$drupalRoot = null;
66-
if (is_dir($project_root . '/core')) {
67-
$drupalRoot = $project_root;
68-
}
69-
foreach (['web', 'docroot'] as $possible_docroot) {
70-
if (is_dir("$project_root/$possible_docroot/core")) {
71-
$drupalRoot = "$project_root/$possible_docroot";
72-
}
73-
}
74-
if ($drupalRoot === null) {
75-
throw new \InvalidArgumentException('Unable to determine the Drupal root');
76-
}
77-
78-
$this->drupalRoot = $drupalRoot;
54+
$finder = new DrupalFinder();
55+
$finder->locateRoot(getcwd());
56+
$this->drupalRoot = $finder->getDrupalRoot();
7957

8058
$builder = $this->getContainerBuilder();
8159
$builder->parameters['drupalRoot'] = $this->drupalRoot;

src/Drupal/Bootstrap.php

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace PHPStan\Drupal;
44

5-
use Composer\Autoload\ClassLoader;
5+
use DrupalFinder\DrupalFinder;
66
use Nette\Utils\Finder;
77

88
class Bootstrap
@@ -15,11 +15,6 @@ class Bootstrap
1515
'themes' => [],
1616
];
1717

18-
/**
19-
* @var ?string
20-
*/
21-
private $autoloaderPath;
22-
2318
/**
2419
* @var \Composer\Autoload\ClassLoader
2520
*/
@@ -66,31 +61,10 @@ class Bootstrap
6661

6762
public function register(): void
6863
{
69-
70-
$this->autoloaderPath = $GLOBALS['autoloaderInWorkingDirectory'];
71-
/** @noinspection PhpIncludeInspection */
72-
$this->autoloader = require $this->autoloaderPath;
73-
if (!$this->autoloader instanceof ClassLoader) {
74-
throw new \InvalidArgumentException('Unable to determine the Composer class loader for Drupal');
75-
}
76-
$realpath = realpath($this->autoloaderPath);
77-
if ($realpath === false) {
78-
throw new \InvalidArgumentException('Cannot determine the realpath of the autoloader.');
79-
}
80-
$project_root = dirname($realpath, 2);
81-
if (is_dir($project_root . '/core')) {
82-
$this->drupalRoot = $project_root;
83-
}
84-
$drupalRoot = null;
85-
foreach (['web', 'docroot'] as $possible_docroot) {
86-
if (is_dir("$project_root/$possible_docroot/core")) {
87-
$drupalRoot = "$project_root/$possible_docroot";
88-
}
89-
}
90-
if ($drupalRoot === null) {
91-
throw new \InvalidArgumentException('Unable to determine the Drupal root');
92-
}
93-
$this->drupalRoot = $drupalRoot;
64+
$finder = new DrupalFinder();
65+
$finder->locateRoot(getcwd());
66+
$this->autoloader = include $finder->getVendorDir() . '/autoload.php';
67+
$this->drupalRoot = $finder->getDrupalRoot();
9468

9569
$this->extensionDiscovery = new ExtensionDiscovery($this->drupalRoot);
9670
$this->extensionDiscovery->setProfileDirectories([]);

0 commit comments

Comments
 (0)