Skip to content

Commit

Permalink
Issue #3144354 by alexpott, vijaycs85, andypost: ModuleInstaller load…
Browse files Browse the repository at this point in the history
…s .module and .install before allowing classes to autoloaded

(cherry picked from commit 1a48485eded95e8ece29e4ed872fc11eacc05e8d)
  • Loading branch information
catch committed Jun 29, 2020
1 parent 9e846da commit d29df8a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
13 changes: 6 additions & 7 deletions lib/Drupal/Core/Extension/ModuleInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,9 @@ public function install(array $module_list, $enable_dependencies = TRUE) {
}
}

// Update the module handler in order to load the module's code.
// This allows the module to participate in hooks and its existence to
// be discovered by other modules.
// The current ModuleHandler instance is obsolete with the kernel
// rebuild below.
// Update the module handler in order to have the correct module list
// for the kernel update.
$this->moduleHandler->setModuleList($module_filenames);
$this->moduleHandler->load($module);
module_load_install($module);

// Clear the static cache of the "extension.list.module" service to pick
// up the new module, since it merges the installation status of modules
Expand All @@ -210,6 +205,10 @@ public function install(array $module_list, $enable_dependencies = TRUE) {
// Update the kernel to include it.
$this->updateKernel($module_filenames);

// Load the module's .module and .install files.
$this->moduleHandler->load($module);
module_load_install($module);

// Replace the route provider service with a version that will rebuild
// if routes used during installation. This ensures that a module's
// routes are available during installation. This has to occur before
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

/**
* @file
* Test module.
*/

use Drupal\module_autoload_test\SomeClass;

define('MODULE_AUTOLOAD_TEST_CONSTANT', SomeClass::TEST);
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class SomeClass {

const TEST = '\Drupal\module_autoload_test\SomeClass::TEST';

public function testMethod() {
return 'Drupal\\module_autoload_test\\SomeClass::testMethod() was invoked.';
}
Expand Down
16 changes: 16 additions & 0 deletions modules/system/tests/src/Functional/Module/ClassLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\Tests\system\Functional\Module;

use Drupal\module_autoload_test\SomeClass;
use Drupal\Tests\BrowserTestBase;

/**
Expand Down Expand Up @@ -96,4 +97,19 @@ public function testMultipleModules() {
$this->assertTrue(\Drupal::moduleHandler()->moduleExists('module_install_class_loader_test2'), 'The module_install_class_loader_test2 module has been installed.');
}

/**
* Tests that .module files can use class constants in main section.
*/
public function testAutoloadFromModuleFile() {
$this->assertFalse(defined('MODULE_AUTOLOAD_TEST_CONSTANT'));
$this->drupalLogin($this->rootUser);
$edit = [
"modules[module_autoload_test][enable]" => TRUE,
];
$this->drupalPostForm('admin/modules', $edit, t('Install'));
$this->assertSession()->statusCodeEquals(200);
$this->resetAll();
$this->assertSame(SomeClass::TEST, MODULE_AUTOLOAD_TEST_CONSTANT);
}

}

0 comments on commit d29df8a

Please sign in to comment.