Issue #1467126 by Rob Loach, sun, effulgentsia, amateescu, EclipseGc: Provider based PSR-0 Classes.

8.0.x
catch 2012-03-14 17:55:55 +09:00
parent 1de01b0025
commit 9ab046c976
4 changed files with 70 additions and 3 deletions

View File

@ -2865,15 +2865,15 @@ function drupal_get_complete_schema($rebuild = FALSE) {
* A UniversalClassLoader class instance (or extension thereof).
*/
function drupal_classloader() {
// Include the Symfony ClassLoader for loading PSR-0-compatible classes.
require_once DRUPAL_ROOT . '/core/vendor/Symfony/Component/ClassLoader/UniversalClassLoader.php';
// By default, use the UniversalClassLoader which is best for development,
// as it does not break when code is moved on the file system. However, as it
// is slow, allow to use the APC class loader in production.
static $loader;
if (!isset($loader)) {
// Include the Symfony ClassLoader for loading PSR-0-compatible classes.
require_once DRUPAL_ROOT . '/core/vendor/Symfony/Component/ClassLoader/UniversalClassLoader.php';
// @todo Use a cleaner way than variable_get() to switch autoloaders.
switch (variable_get('autoloader_mode', 'default')) {
case 'apc':
@ -2895,6 +2895,19 @@ function drupal_classloader() {
return $loader;
}
/**
* Registers an additional namespace.
*
* @param string $name
* The namespace component to register; e.g., 'node'.
* @param string $path
* The relative path to the Drupal component in the filesystem.
*/
function drupal_classloader_register($name, $path) {
$loader = drupal_classloader();
$loader->registerNamespace('Drupal\\' . $name, DRUPAL_ROOT . '/' . $path . '/lib');
}
/**
* Confirms that an interface is available.
*

View File

@ -140,6 +140,7 @@ function system_list($type) {
// drupal_get_filename() static cache for bootstrap modules only.
// The rest is stored separately to keep the bootstrap module cache small.
foreach ($bootstrap_list as $module) {
drupal_classloader_register($module->name, dirname($module->filename));
drupal_get_filename('module', $module->name, $module->filename);
}
// We only return the module names here since module_list() doesn't need
@ -175,6 +176,7 @@ function system_list($type) {
}
// Build a list of filenames so drupal_get_filename can use it.
if ($record->status) {
drupal_classloader_register($record->name, dirname($record->filename));
$lists['filepaths'][] = array('type' => $record->type, 'name' => $record->name, 'filepath' => $record->filename);
}
}

View File

@ -238,6 +238,38 @@ class ModuleUnitTest extends DrupalWebTestCase {
}
}
/**
* Tests class loading.
*/
class ModuleClassLoaderTestCase extends DrupalWebTestCase {
protected $profile = 'testing';
public static function getInfo() {
return array(
'name' => 'Module class loader',
'description' => 'Tests class loading for modules.',
'group' => 'Module',
);
}
/**
* Tests that module-provided classes can be loaded when a module is enabled.
*/
function testClassLoading() {
$expected = 'Drupal\\module_autoload_test\\SomeClass::testMethod() was invoked.';
module_enable(array('module_test', 'module_autoload_test'), FALSE);
$this->resetAll();
$this->drupalGet('module-test/class-loading');
$this->assertText($expected, t('Autoloader loads classes from an enabled module.'));
module_disable(array('module_autoload_test'), FALSE);
$this->resetAll();
$this->drupalGet('module-test/class-loading');
$this->assertNoText($expected, t('Autoloader does not load classes from a disabled module.'));
}
}
/**
* Unit tests for module installation.
*/

View File

@ -78,6 +78,11 @@ function module_test_menu() {
'page callback' => 'module_test_hook_dynamic_loading_invoke_all',
'access arguments' => array('access content'),
);
$items['module-test/class-loading'] = array(
'title' => 'Test loading a class from another module',
'page callback' => 'module_test_class_loading',
'access callback' => TRUE,
);
return $items;
}
@ -103,6 +108,21 @@ function module_test_hook_dynamic_loading_invoke_all() {
return $result['module_test'];
}
/**
* Page callback for 'class loading' test.
*
* This module does not have a dependency on module_autoload_test.module. If
* that module is enabled, this function should return the string
* 'Drupal\\module_autoload_test\\SomeClass::testMethod() was invoked.'. If
* that module is not enabled, this function should return nothing.
*/
function module_test_class_loading() {
if (class_exists('Drupal\module_autoload_test\SomeClass')) {
$obj = new Drupal\module_autoload_test\SomeClass();
return $obj->testMethod();
}
}
/**
* Implements hook_modules_enabled().
*/