summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.txt2
-rw-r--r--includes/bootstrap.inc21
-rw-r--r--includes/registry.inc2
-rw-r--r--modules/simpletest/tests/bootstrap.test4
-rw-r--r--modules/simpletest/tests/drupal_autoload_test/drupal_autoload_test.module16
-rw-r--r--modules/simpletest/tests/drupal_autoload_test/drupal_autoload_test_trait.sh16
6 files changed, 59 insertions, 2 deletions
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');
+ }
}
/**
@@ -3112,6 +3115,22 @@ function drupal_autoload_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.
*
* @param $type
@@ -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 <em>DrupalAutoloadTestInterface</em> in lower case.');
// Test class autoloader.
$this->assertTrue(drupal_autoload_class('drupalautoloadtestclass'), 'drupal_autoload_class() recognizes <em>DrupalAutoloadTestClass</em> in lower case.');
+ // Test trait autoloader.
+ if (version_compare(PHP_VERSION, '5.4') >= 0) {
+ $this->assertTrue(drupal_autoload_trait('drupalautoloadtesttrait'), 'drupal_autoload_trait() recognizes <em>DrupalAutoloadTestTrait</em> 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 @@
+<?php
+
+/**
+ * @file
+ * Test traits for code registry testing.
+ *
+ * This file has a non-standard extension to prevent PHP < 5.4 testbots from
+ * trying to run a syntax check on it.
+ * @todo Use a standard extension once the testbots allow it. See
+ * https://www.drupal.org/node/2589649.
+ */
+
+/**
+ * This trait is empty because we only care if Drupal can find it.
+ */
+trait DrupalAutoloadTestTrait {}