diff --git a/libraries.module b/libraries.module index d843bcd..d4ee004 100644 --- a/libraries.module +++ b/libraries.module @@ -732,6 +732,23 @@ function libraries_load_files($library) { } /** + * Wrapper function for require_once. + * + * A library file could set a $path variable in file scope. Requiring such a + * file directly in libraries_load_files() would lead to the local $path + * variable being overridden after the require_once statement. This would + * break loading further files. Therefore we use this trivial wrapper which has + * no local state that can be tampered with. + * + * @param $file_path + * The file path of the file to require. + */ +function _libraries_require_once($file_path) { + require_once $file_path; +} + + +/** * Gets the version information from an arbitrary library. * * @param $library diff --git a/tests/example/example_1.php b/tests/example/example_1.php index 92e6711..a55cbf0 100644 --- a/tests/example/example_1.php +++ b/tests/example/example_1.php @@ -5,6 +5,9 @@ * Test PHP file for Libraries loading. */ +// @see _libraries_require_once() +$path = 'abc'; + /** * Dummy function to see if this file was loaded. */ diff --git a/tests/example/example_2.php b/tests/example/example_2.php index ddded40..4f06edf 100644 --- a/tests/example/example_2.php +++ b/tests/example/example_2.php @@ -5,6 +5,9 @@ * Test PHP file for Libraries loading. */ +// @see _libraries_require_once() +$path = 'abc'; + /** * Dummy function to see if this file was loaded. */ diff --git a/tests/libraries.test b/tests/libraries.test index 1770162..eba4077 100644 --- a/tests/libraries.test +++ b/tests/libraries.test @@ -303,6 +303,12 @@ class LibrariesTestCase extends DrupalWebTestCase { $loaded = &drupal_static('libraries_load'); $this->verbose('
' . var_export($loaded, TRUE) . '
'); $this->assertEqual($loaded['example_dependency']['loaded'], 1, 'Dependency library is also loaded'); + + // Test that PHP files that have a local $path variable do not break library + // loading. + // @see _libraries_require_once() + $library = libraries_load('example_path_variable_override'); + $this->assertEqual($library['loaded'], 2, 'PHP files cannot break library loading.'); } /** diff --git a/tests/libraries_test.module b/tests/libraries_test.module index faae509..dfc13ff 100644 --- a/tests/libraries_test.module +++ b/tests/libraries_test.module @@ -287,6 +287,15 @@ function libraries_test_libraries_info() { 'post-load callback' => 'not applied', ); + $libraries['example_path_variable_override'] = array( + 'name' => 'Example path variable override', + 'library path' => drupal_get_path('module', 'libraries') . '/tests/example', + 'version' => '1', + 'files' => array( + 'php' => array('example_1.php', 'example_2.php'), + ), + ); + return $libraries; }