From 183a425c551969d4f56b04766fca82819a2a7b15 Mon Sep 17 00:00:00 2001 From: David Rothstein Date: Mon, 12 Oct 2015 23:32:29 -0400 Subject: Issue #2508055 by Dave Reid, David_Rothstein, hussainweb: Add support for autoloading Traits --- CHANGELOG.txt | 2 ++ includes/bootstrap.inc | 21 ++++++++++++++++++++- includes/registry.inc | 2 +- modules/simpletest/tests/bootstrap.test | 4 ++++ .../drupal_autoload_test.module | 16 ++++++++++++++++ .../drupal_autoload_test_trait.sh | 16 ++++++++++++++++ 6 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 modules/simpletest/tests/drupal_autoload_test/drupal_autoload_test_trait.sh diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 8602daa6f..6101a1e3c 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,6 +1,8 @@ Drupal 7.40, xxxx-xx-xx (development version) ----------------------- +- Added support for autoloading traits via the registry on sites running PHP + 5.4 or higher. - Allowed the user-picture.tpl.php theme template to have HTML classes besides the default "user-picture" class printed in it (markup change). - Fixed the URL text filter to convert e-mail addresses with plus signs into diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc index 086cef0a9..70d426b4a 100644 --- a/includes/bootstrap.inc +++ b/includes/bootstrap.inc @@ -2468,6 +2468,9 @@ function _drupal_bootstrap_database() { // the install or upgrade process. spl_autoload_register('drupal_autoload_class'); spl_autoload_register('drupal_autoload_interface'); + if (version_compare(PHP_VERSION, '5.4') >= 0) { + spl_autoload_register('drupal_autoload_trait'); + } } /** @@ -3111,6 +3114,22 @@ function drupal_autoload_class($class) { return _registry_check_code('class', $class); } +/** + * Confirms that a trait is available. + * + * This function is rarely called directly. Instead, it is registered as an + * spl_autoload() handler, and PHP calls it for us when necessary. + * + * @param string $trait + * The name of the trait to check or load. + * + * @return bool + * TRUE if the trait is currently available, FALSE otherwise. + */ +function drupal_autoload_trait($trait) { + return _registry_check_code('trait', $trait); +} + /** * Checks for a resource in the registry. * @@ -3129,7 +3148,7 @@ function drupal_autoload_class($class) { function _registry_check_code($type, $name = NULL) { static $lookup_cache, $cache_update_needed; - if ($type == 'class' && class_exists($name) || $type == 'interface' && interface_exists($name)) { + if ($type == 'class' && class_exists($name) || $type == 'interface' && interface_exists($name) || $type == 'trait' && trait_exists($name)) { return TRUE; } diff --git a/includes/registry.inc b/includes/registry.inc index 5fc767487..29a1fca8c 100644 --- a/includes/registry.inc +++ b/includes/registry.inc @@ -164,7 +164,7 @@ function _registry_parse_files($files) { * (optional) Weight of the module. */ function _registry_parse_file($filename, $contents, $module = '', $weight = 0) { - if (preg_match_all('/^\s*(?:abstract|final)?\s*(class|interface)\s+([a-zA-Z0-9_]+)/m', $contents, $matches)) { + if (preg_match_all('/^\s*(?:abstract|final)?\s*(class|interface|trait)\s+([a-zA-Z0-9_]+)/m', $contents, $matches)) { foreach ($matches[2] as $key => $name) { db_merge('registry') ->key(array( diff --git a/modules/simpletest/tests/bootstrap.test b/modules/simpletest/tests/bootstrap.test index ece1cd9e9..d46c6ec8f 100644 --- a/modules/simpletest/tests/bootstrap.test +++ b/modules/simpletest/tests/bootstrap.test @@ -313,6 +313,10 @@ class BootstrapAutoloadTestCase extends DrupalWebTestCase { $this->assertTrue(drupal_autoload_interface('drupalautoloadtestinterface'), 'drupal_autoload_interface() recognizes DrupalAutoloadTestInterface in lower case.'); // Test class autoloader. $this->assertTrue(drupal_autoload_class('drupalautoloadtestclass'), 'drupal_autoload_class() recognizes DrupalAutoloadTestClass in lower case.'); + // Test trait autoloader. + if (version_compare(PHP_VERSION, '5.4') >= 0) { + $this->assertTrue(drupal_autoload_trait('drupalautoloadtesttrait'), 'drupal_autoload_trait() recognizes DrupalAutoloadTestTrait in lower case.'); + } } } diff --git a/modules/simpletest/tests/drupal_autoload_test/drupal_autoload_test.module b/modules/simpletest/tests/drupal_autoload_test/drupal_autoload_test.module index 37aa94eb8..edd5d77cb 100644 --- a/modules/simpletest/tests/drupal_autoload_test/drupal_autoload_test.module +++ b/modules/simpletest/tests/drupal_autoload_test/drupal_autoload_test.module @@ -4,3 +4,19 @@ * @file * Test module to check code registry. */ + +/** + * Implements hook_registry_files_alter(). + */ +function drupal_autoload_test_registry_files_alter(&$files, $modules) { + foreach ($modules as $module) { + // Add the drupal_autoload_test_trait.sh file to the registry when PHP 5.4+ + // is being used. + if ($module->name == 'drupal_autoload_test' && version_compare(PHP_VERSION, '5.4') >= 0) { + $files["$module->dir/drupal_autoload_test_trait.sh"] = array( + 'module' => $module->name, + 'weight' => $module->weight, + ); + } + } +} diff --git a/modules/simpletest/tests/drupal_autoload_test/drupal_autoload_test_trait.sh b/modules/simpletest/tests/drupal_autoload_test/drupal_autoload_test_trait.sh new file mode 100644 index 000000000..69ce9ec08 --- /dev/null +++ b/modules/simpletest/tests/drupal_autoload_test/drupal_autoload_test_trait.sh @@ -0,0 +1,16 @@ +