Skip to content

Commit 3a2ba26

Browse files
committed
Default drupal_root to current working directory
This defaults the drupal.drupal_root container parameter to the current working directory variable, and marks it as a required value in the schema.
1 parent 39a7947 commit 3a2ba26

File tree

5 files changed

+11
-21
lines changed

5 files changed

+11
-21
lines changed

extension.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ parameters:
1212
- profile
1313
- engine
1414
drupal:
15-
drupal_root: null
15+
drupal_root: '%currentWorkingDirectory%'
1616
entityTypeStorageMapping:
1717
node: Drupal\node\NodeStorage
1818
taxonomy_term: Drupal\taxonomy\TermStorage
1919
user: Drupal\user\UserStorage
2020
parametersSchema:
2121
drupal: structure([
22-
drupal_root: schema(string(), nullable())
22+
drupal_root: string()
2323
entityTypeStorageMapping: arrayOf(string())
2424
])
2525
rules:

src/Drupal/DrupalAutoloader.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,15 @@ class DrupalAutoloader
6262

6363
public function register(Container $container): void
6464
{
65-
$startPath = null;
6665
$drupalParams = $container->getParameter('drupal');
67-
$drupalRoot = $drupalParams['drupal_root'] ?? null;
68-
if ($drupalRoot !== null && realpath($drupalRoot) !== false && is_dir($drupalRoot)) {
69-
$startPath = $drupalRoot;
70-
} else {
71-
$startPath = dirname($GLOBALS['autoloaderInWorkingDirectory']);
72-
}
66+
$drupalRoot = $drupalParams['drupal_root'];
7367
$finder = new DrupalFinder();
74-
$finder->locateRoot($startPath);
68+
$finder->locateRoot($drupalRoot);
7569

7670
$drupalRoot = $finder->getDrupalRoot();
7771
$drupalVendorRoot = $finder->getVendorDir();
7872
if (! (bool) $drupalRoot || ! (bool) $drupalVendorRoot) {
79-
throw new \RuntimeException("Unable to detect Drupal at $startPath");
73+
throw new \RuntimeException("Unable to detect Drupal at $drupalRoot");
8074
}
8175

8276
$this->drupalRoot = $drupalRoot;

tests/fixtures/config/drupal-phpstan.neon

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,5 @@ parameters:
33
level: 0
44
ignoreErrors:
55
- '#\Drupal calls should be avoided in classes, use dependency injection instead#'
6-
drupal:
7-
drupal_root: %currentWorkingDirectory%
86
includes:
97
- vendor/mglaman/phpstan-drupal/extension.neon
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
parameters:
22
reportUnmatchedIgnoredErrors: false
3-
level: 7
3+
level: 2
44
# Ignore functions for the PHPUnit bootstrap process.
55
ignoreErrors:
66
- '#Function drupal_phpunit_[a-zA-Z0-9\\_]+ not found#'
77
- '#Unsafe usage of new static().#'
8-
drupal:
9-
drupal_root: %currentWorkingDirectory%
108
includes:
119
- ../../../extension.neon
1210
- ../../../vendor/phpstan/phpstan-deprecation-rules/rules.neon

tests/src/BootstrapTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ private function doDrupalBootstrap()
4747
$rootDir = __DIR__ . '/../fixtures/drupal';
4848
$tmpDir = sys_get_temp_dir() . '/' . time() . 'phpstan';
4949
$containerFactory = new ContainerFactory($rootDir);
50-
$container = $containerFactory->create(
51-
$tmpDir,
52-
[__DIR__ . '/../fixtures/config/phpunit-drupal-phpstan.neon'],
53-
[]
54-
);
50+
$additionalConfigFiles = [
51+
\sprintf('%s/config.level%s.neon', $containerFactory->getConfigDirectory(), 2),
52+
__DIR__ . '/../fixtures/config/phpunit-drupal-phpstan.neon',
53+
];
54+
$container = $containerFactory->create($tmpDir, $additionalConfigFiles, []);
5555
$fileHelper = $container->getByType(FileHelper::class);
5656
assert($fileHelper !== null);
5757

0 commit comments

Comments
 (0)