Skip to content

Commit cae8441

Browse files
committed
Add graceful handling of container not being initialized
1 parent ebf69ca commit cae8441

11 files changed

+61
-4
lines changed

src/Drupal/Bootstrap.php

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

33
namespace PHPStan\Drupal;
44

5+
use Drupal\Core\DependencyInjection\ContainerNotInitializedException;
56
use DrupalFinder\DrupalFinder;
67
use Nette\Utils\Finder;
78

@@ -99,12 +100,12 @@ public function register(): void
99100
if (file_exists($module_dir . '/' . $module_name . '.install')) {
100101
$ignored_install_files = ['entity_test', 'entity_test_update', 'update_test_schema'];
101102
if (!in_array($module_name, $ignored_install_files, true)) {
102-
require $module_dir . '/' . $module_name . '.install';
103+
$this->loadAndCatchErrors($module_dir . '/' . $module_name . '.install');
103104
}
104105
}
105106
// Add .post_update.php
106107
if (file_exists($module_dir . '/' . $module_name . '.post_update.php')) {
107-
require $module_dir . '/' . $module_name . '.post_update.php';
108+
$this->loadAndCatchErrors($module_dir . '/' . $module_name . '.post_update.php');
108109
}
109110
// Add misc .inc that are magically allowed via hook_hook_info.
110111
$magic_hook_info_includes = [
@@ -116,7 +117,7 @@ public function register(): void
116117
];
117118
foreach ($magic_hook_info_includes as $hook_info_include) {
118119
if (file_exists($module_dir . "/$module_name.$hook_info_include.inc")) {
119-
require $module_dir . "/$module_name.$hook_info_include.inc";
120+
$this->loadAndCatchErrors($module_dir . "/$module_name.$hook_info_include.inc");
120121
}
121122
}
122123
}
@@ -223,4 +224,17 @@ protected function loadExtension(Extension $extension): void
223224
// This can happen when drupal_get_path or drupal_get_filename are used outside of the scope of a function.
224225
}
225226
}
227+
228+
protected function loadAndCatchErrors(string $path): void
229+
{
230+
try {
231+
require $path;
232+
} catch (ContainerNotInitializedException $e) {
233+
// This can happen when drupal_get_path or drupal_get_filename are used outside of the scope of a function.
234+
@trigger_error("$path invoked the Drupal container outside of the scope of a function or class method. It was skipped for analysis.", E_USER_WARNING);
235+
} catch (\Throwable $e) {
236+
// Something prevented the extension file from loading.
237+
@trigger_error("$path failed loading due to {$e->getMessage()}", E_USER_WARNING);
238+
}
239+
}
226240
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name: contained_not_initialized
2+
type: module
3+
core: 8.x
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
// drupal_get_path calls drupal_get_filename which invokes \Drupal::service("extension.list.$type");
4+
// this will cause the bootstrap process to always die.
5+
drupal_get_path('module', 'contained_not_initialized');
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
// drupal_get_path calls drupal_get_filename which invokes \Drupal::service("extension.list.$type");
4+
// this will cause the bootstrap process to always die.
5+
drupal_get_path('module', 'contained_not_initialized');
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
// drupal_get_path calls drupal_get_filename which invokes \Drupal::service("extension.list.$type");
4+
// this will cause the bootstrap process to always die.
5+
drupal_get_path('module', 'contained_not_initialized');
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
// drupal_get_path calls drupal_get_filename which invokes \Drupal::service("extension.list.$type");
4+
// this will cause the bootstrap process to always die.
5+
drupal_get_path('module', 'contained_not_initialized');
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
// drupal_get_path calls drupal_get_filename which invokes \Drupal::service("extension.list.$type");
4+
// this will cause the bootstrap process to always die.
5+
drupal_get_path('module', 'contained_not_initialized');
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
// drupal_get_path calls drupal_get_filename which invokes \Drupal::service("extension.list.$type");
4+
// this will cause the bootstrap process to always die.
5+
drupal_get_path('module', 'contained_not_initialized');
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
// drupal_get_path calls drupal_get_filename which invokes \Drupal::service("extension.list.$type");
4+
// this will cause the bootstrap process to always die.
5+
drupal_get_path('module', 'contained_not_initialized');
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
// drupal_get_path calls drupal_get_filename which invokes \Drupal::service("extension.list.$type");
4+
// this will cause the bootstrap process to always die.
5+
drupal_get_path('module', 'contained_not_initialized');

0 commit comments

Comments
 (0)