Skip to content
Snippets Groups Projects
Commit 6d4a007b authored by Andreas Hennings's avatar Andreas Hennings
Browse files

correct implementation of D8-style LibPSR0. Added tests to confirm that it works.

parent d8ca8f45
No related branches found
No related tags found
No related merge requests found
<?php
class xautoload_Mock_DrupalExtensionSystem {
protected $extensions;
function module_exists($module) {
return isset($this->extensions[$module]);
}
function drupal_get_path($type, $name) {
$info = @$this->extensions[$name];
if ($info && $info['type'] === $type) {
return $info['path'];
}
}
function addExtension($type, $name, $path) {
$this->extensions[$name] = array(
'type' => $type,
'path' => $path,
);
}
function addModule($name, $path) {
$this->addExtension('module', $name, $path);
}
function addTheme($name, $path) {
$this->addExtension('theme', $name, $path);
}
}
<?php
class xautoload_NamespaceHandler_DrupalExtensionLibPSR0 extends xautoload_NamespaceHandler_DrupalModuleLib {
class xautoload_NamespaceHandler_DrupalExtensionLibPSR0 extends xautoload_NamespaceHandler_DrupalExtensionLib {
protected function _moduleClassesDir($module, $module_dir, $path_prefix_symbolic) {
return $module_dir . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . $path_prefix_symbolic;
return $module_dir . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR .
$path_prefix_symbolic . $module . DIRECTORY_SEPARATOR;
}
}
......@@ -4,6 +4,7 @@
class xautoload_NamespaceHandler_DrupalExtensionPSR0 extends xautoload_NamespaceHandler_DrupalExtensionLib {
protected function _moduleClassesDir($module, $module_dir, $path_prefix_symbolic) {
return $module_dir . DIRECTORY_SEPARATOR . $path_prefix_symbolic;
return $module_dir . DIRECTORY_SEPARATOR .
$path_prefix_symbolic . $module . DIRECTORY_SEPARATOR;
}
}
......@@ -92,7 +92,7 @@ function _xautoload_finder_create() {
$system = new xautoload_DrupalExtensionSystem();
$handler = new xautoload_NamespaceHandler_DrupalExtensionLib($system);
$handler_psr0 = new xautoload_NamespaceHandler_DrupalExtensionPSR0($system);
$handler_psr0 = new xautoload_NamespaceHandler_DrupalExtensionLibPSR0($system);
if (version_compare(PHP_VERSION, '5.3') >= 0) {
$finder = new xautoload_ClassFinder_NamespaceOrPrefix();
......
......@@ -36,7 +36,7 @@ class XAutoloadUnitTestCase extends DrupalUnitTestCase {
$this->assert(print_r($finder, TRUE) === $finder_str,
"Finder configuration may not change during the process.");
$this->assert(TRUE, '<pre>' . print_r($finder, TRUE) . '</pre>');
// $this->assert(TRUE, '<pre>' . print_r($finder, TRUE) . '</pre>');
}
function testNsReg() {
......@@ -59,7 +59,81 @@ class XAutoloadUnitTestCase extends DrupalUnitTestCase {
$this->assert(print_r($finder, TRUE) === $finder_str,
"Finder configuration may not change during the process.");
$this->assert(TRUE, '<pre>' . print_r($finder, TRUE) . '</pre>');
// $this->assert(TRUE, '<pre>' . print_r($finder, TRUE) . '</pre>');
}
/**
* This was suggested once for D8, but discarded.
*/
function testLibPseudoPSR0() {
$system = new xautoload_Mock_DrupalExtensionSystem();
$system->addModule('menu_block', 'sites/all/modules/contrib/menu_block');
// PSR0-style without the /lib/ folder.
$handler_psr0 = new xautoload_NamespaceHandler_DrupalExtensionLib($system);
$finder = new xautoload_ClassFinder_NamespaceOrPrefix();
$finder->registerNamespaceHandler('Drupal', $handler_psr0);
// We mix in some underscores just to make sure they work as designed.
$this->_findClass($finder, 'Drupal\\menu_block\\Abc_Def\\G_Hi', array(
'sites/all/modules/contrib/menu_block/lib/Abc_Def/G/Hi.php',
));
}
/**
* This was suggested once for D8, but discarded.
*/
function testPSR0() {
$system = new xautoload_Mock_DrupalExtensionSystem();
$system->addModule('menu_block', 'sites/all/modules/contrib/menu_block');
// PSR0-style without the /lib/ folder.
$handler_psr0 = new xautoload_NamespaceHandler_DrupalExtensionPSR0($system);
$finder = new xautoload_ClassFinder_NamespaceOrPrefix();
$finder->registerNamespaceHandler('Drupal', $handler_psr0);
// We mix in some underscores just to make sure they work as designed.
$this->_findClass($finder, 'Drupal\\menu_block\\Abc_Def\\G_Hi', array(
'sites/all/modules/contrib/menu_block/Drupal/menu_block/Abc_Def/G/Hi.php',
));
}
function testLibPSR0() {
$system = new xautoload_Mock_DrupalExtensionSystem();
$system->addModule('menu_block', 'sites/all/modules/contrib/menu_block');
$this->assert(
$system->module_exists('menu_block'),
'menu_block should "exist".'
);
$this->assert(
!$system->module_exists('foo'),
'foo should not "exist".'
);
$this->assert(
$system->drupal_get_path('module', 'menu_block') === 'sites/all/modules/contrib/menu_block',
'menu_block should be located in sites/all/modules/contrib/menu_block'
);
$handler = new xautoload_NamespaceHandler_DrupalExtensionLib($system);
$handler_psr0 = new xautoload_NamespaceHandler_DrupalExtensionLibPSR0($system);
$finder = new xautoload_ClassFinder_NamespaceOrPrefix();
$finder->registerNamespaceHandler('Drupal', $handler_psr0);
$finder->registerPrefixHandler('', $handler);
// PSR0-style (D8, PHP 5.3)
// We mix in some underscores just to make sure they work as designed.
$this->_findClass($finder, 'Drupal\\menu_block\\Abc_Def\\G_Hi', array(
'sites/all/modules/contrib/menu_block/lib/Drupal/menu_block/Abc_Def/G/Hi.php',
));
// xautoload-style (D6, D7, PHP 5.2)
// We mix in some lowercase characters, only to make sure they work as designed.
$this->_findClass($finder, 'menu_block_Abc_Def_g_hi', array(
'sites/all/modules/contrib/menu_block/lib/Abc/Def/g/hi.php',
));
}
protected function _findClass($finder, $class, array $expectedSuggestions) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment