Issue #2083547 by donquixote: Implement PSR-4 for module-provided class files.

This affects class loading, plugin discovery and test discovery.

PSR-0 is still supported, and no module class files are moved yet.
8.0.x
Andreas Hennings 2014-01-04 04:50:45 +01:00 committed by webchick
parent 4f18d62272
commit 940e0dffb8
35 changed files with 187 additions and 72 deletions

View File

@ -26,10 +26,10 @@
"zendframework/zend-feed": "2.2.*" "zendframework/zend-feed": "2.2.*"
}, },
"autoload": { "autoload": {
"psr-0": { "psr-4": {
"Drupal\\Core": "core/lib/", "Drupal\\Core\\": "core/lib/Drupal/Core",
"Drupal\\Component": "core/lib/", "Drupal\\Component\\": "core/lib/Drupal/Component",
"Drupal\\Driver": "drivers/lib/" "Drupal\\Driver\\": "drivers/lib/Drupal/Driver"
}, },
"files": [ "files": [
"core/lib/Drupal.php" "core/lib/Drupal.php"

View File

@ -2124,7 +2124,10 @@ function drupal_classloader($class_loader = NULL) {
*/ */
function drupal_classloader_register($name, $path) { function drupal_classloader_register($name, $path) {
$loader = drupal_classloader(); $loader = drupal_classloader();
$loader->add('Drupal\\' . $name, DRUPAL_ROOT . '/' . $path . '/lib'); $loader->addPsr4('Drupal\\' . $name . '\\', array(
DRUPAL_ROOT . '/' . $path . '/lib/Drupal/' . $name,
DRUPAL_ROOT . '/' . $path . '/src',
));
} }
/** /**

View File

@ -99,7 +99,6 @@ class AnnotatedClassDiscovery implements DiscoveryInterface {
// Search for classes within all PSR-0 namespace locations. // Search for classes within all PSR-0 namespace locations.
foreach ($this->getPluginNamespaces() as $namespace => $dirs) { foreach ($this->getPluginNamespaces() as $namespace => $dirs) {
foreach ($dirs as $dir) { foreach ($dirs as $dir) {
$dir .= DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $namespace);
if (file_exists($dir)) { if (file_exists($dir)) {
$iterator = new \RecursiveIteratorIterator( $iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS) new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS)

View File

@ -218,7 +218,7 @@ class DrupalKernel implements DrupalKernelInterface, TerminableInterface {
$this->moduleList = isset($extensions['module']) ? $extensions['module'] : array(); $this->moduleList = isset($extensions['module']) ? $extensions['module'] : array();
} }
$module_filenames = $this->getModuleFileNames(); $module_filenames = $this->getModuleFileNames();
$this->registerNamespaces($this->getModuleNamespaces($module_filenames)); $this->registerNamespacesPsr4($this->getModuleNamespacesPsr4($module_filenames));
// Load each module's serviceProvider class. // Load each module's serviceProvider class.
foreach ($this->moduleList as $module => $weight) { foreach ($this->moduleList as $module => $weight) {
@ -408,8 +408,8 @@ class DrupalKernel implements DrupalKernelInterface, TerminableInterface {
// All namespaces must be registered before we attempt to use any service // All namespaces must be registered before we attempt to use any service
// from the container. // from the container.
$container_modules = $this->container->getParameter('container.modules'); $container_modules = $this->container->getParameter('container.modules');
$namespaces_before = $this->classLoader->getPrefixes(); $namespaces_before = $this->classLoader->getPrefixesPsr4();
$this->registerNamespaces($this->container->getParameter('container.namespaces')); $this->registerNamespacesPsr4($this->container->getParameter('container.namespaces'));
// If 'container.modules' is wrong, the container must be rebuilt. // If 'container.modules' is wrong, the container must be rebuilt.
if (!isset($this->moduleList)) { if (!isset($this->moduleList)) {
@ -422,9 +422,9 @@ class DrupalKernel implements DrupalKernelInterface, TerminableInterface {
// registerNamespaces() performs a merge rather than replace, so to // registerNamespaces() performs a merge rather than replace, so to
// effectively remove erroneous registrations, we must replace them with // effectively remove erroneous registrations, we must replace them with
// empty arrays. // empty arrays.
$namespaces_after = $this->classLoader->getPrefixes(); $namespaces_after = $this->classLoader->getPrefixesPsr4();
$namespaces_before += array_fill_keys(array_diff(array_keys($namespaces_after), array_keys($namespaces_before)), array()); $namespaces_before += array_fill_keys(array_diff(array_keys($namespaces_after), array_keys($namespaces_before)), array());
$this->registerNamespaces($namespaces_before); $this->registerNamespacesPsr4($namespaces_before);
} }
} }
@ -500,14 +500,15 @@ class DrupalKernel implements DrupalKernelInterface, TerminableInterface {
$container->setParameter('container.modules', $this->getModulesParameter()); $container->setParameter('container.modules', $this->getModulesParameter());
// Get a list of namespaces and put it onto the container. // Get a list of namespaces and put it onto the container.
$namespaces = $this->getModuleNamespaces($this->getModuleFileNames()); $namespaces = $this->getModuleNamespacesPsr4($this->getModuleFileNames());
// Add all components in \Drupal\Core and \Drupal\Component that have a // Add all components in \Drupal\Core and \Drupal\Component that have a
// Plugin directory. // Plugin directory.
foreach (array('Core', 'Component') as $parent_directory) { foreach (array('Core', 'Component') as $parent_directory) {
$path = DRUPAL_ROOT . '/core/lib/Drupal/' . $parent_directory; $path = DRUPAL_ROOT . '/core/lib/Drupal/' . $parent_directory;
$parent_namespace = 'Drupal\\' . $parent_directory;
foreach (new \DirectoryIterator($path) as $component) { foreach (new \DirectoryIterator($path) as $component) {
if (!$component->isDot() && $component->isDir() && is_dir($component->getPathname() . '/Plugin')) { if (!$component->isDot() && $component->isDir() && is_dir($component->getPathname() . '/Plugin')) {
$namespaces['Drupal\\' . $parent_directory . '\\' . $component->getFilename()] = DRUPAL_ROOT . '/core/lib'; $namespaces[$parent_namespace . '\\' . $component->getFilename()] = $path . '/' . $component->getFilename();
} }
} }
} }
@ -696,6 +697,28 @@ class DrupalKernel implements DrupalKernelInterface, TerminableInterface {
return $filenames; return $filenames;
} }
/**
* Gets the PSR-4 base directories for module namespaces.
*
* @param array $module_file_names
* Array where each key is a module name, and each value is a path to the
* respective *.module or *.profile file.
*
* @return array
* Array where each key is a module namespace like 'Drupal\system', and each
* value is an array of PSR-4 base directories associated with the module
* namespace.
*/
protected function getModuleNamespacesPsr4($module_file_names) {
$namespaces = array();
foreach ($module_file_names as $module => $filename) {
// @todo Remove lib/Drupal/$module, once the switch to PSR-4 is complete.
$namespaces["Drupal\\$module"][] = DRUPAL_ROOT . '/' . dirname($filename) . '/lib/Drupal/' . $module;
$namespaces["Drupal\\$module"][] = DRUPAL_ROOT . '/' . dirname($filename) . '/src';
}
return $namespaces;
}
/** /**
* Gets the PSR-0 base directories for module namespaces. * Gets the PSR-0 base directories for module namespaces.
* *
@ -715,6 +738,20 @@ class DrupalKernel implements DrupalKernelInterface, TerminableInterface {
return $namespaces; return $namespaces;
} }
/**
* Registers a list of namespaces with PSR-4 directories for class loading.
*
* @param array $namespaces
* Array where each key is a namespace like 'Drupal\system', and each value
* is either a PSR-4 base directory, or an array of PSR-4 base directories
* associated with this namespace.
*/
protected function registerNamespacesPsr4(array $namespaces = array()) {
foreach ($namespaces as $prefix => $paths) {
$this->classLoader->addPsr4($prefix . '\\', $paths);
}
}
/** /**
* Registers a list of namespaces with PSR-0 directories for class loading. * Registers a list of namespaces with PSR-0 directories for class loading.
* *

View File

@ -16,17 +16,23 @@ use Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery as Comp
class AnnotatedClassDiscovery extends ComponentAnnotatedClassDiscovery { class AnnotatedClassDiscovery extends ComponentAnnotatedClassDiscovery {
/** /**
* The subdirectory within a namespace to look for plugins. * A suffix to append to each PSR-4 directory associated with a base
* * namespace, to form the directories where plugins are found.
* If the plugins are in the top level of the namespace and not within a
* subdirectory, set this to an empty string.
* *
* @var string * @var string
*/ */
protected $subdir = ''; protected $directorySuffix = '';
/** /**
* An object containing the namespaces to look for plugin implementations. * A suffix to append to each base namespace, to obtain the namespaces where
* plugins are found.
*
* @var string
*/
protected $namespaceSuffix = '';
/**
* A list of base namespaces with their PSR-4 directories.
* *
* @var \Traversable * @var \Traversable
*/ */
@ -48,7 +54,13 @@ class AnnotatedClassDiscovery extends ComponentAnnotatedClassDiscovery {
*/ */
function __construct($subdir, \Traversable $root_namespaces, $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin') { function __construct($subdir, \Traversable $root_namespaces, $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin') {
if ($subdir) { if ($subdir) {
$this->subdir = str_replace('/', '\\', $subdir); // Prepend a directory separator to $subdir,
// if it does not already have one.
if ('/' !== $subdir[0]) {
$subdir = '/' . $subdir;
}
$this->directorySuffix = $subdir;
$this->namespaceSuffix = str_replace('/', '\\', $subdir);
} }
$this->rootNamespacesIterator = $root_namespaces; $this->rootNamespacesIterator = $root_namespaces;
$plugin_namespaces = array(); $plugin_namespaces = array();
@ -104,11 +116,28 @@ class AnnotatedClassDiscovery extends ComponentAnnotatedClassDiscovery {
*/ */
protected function getPluginNamespaces() { protected function getPluginNamespaces() {
$plugin_namespaces = array(); $plugin_namespaces = array();
foreach ($this->rootNamespacesIterator as $namespace => $dir) { if ($this->namespaceSuffix) {
if ($this->subdir) { foreach ($this->rootNamespacesIterator as $namespace => $dirs) {
$namespace .= "\\{$this->subdir}"; // Append the namespace suffix to the base namespace, to obtain the
// plugin namespace. E.g. 'Drupal\Views' may become
// 'Drupal\Views\Plugin\Block'.
$namespace .= $this->namespaceSuffix;
foreach ((array) $dirs as $dir) {
// Append the directory suffix to the PSR-4 base directory, to obtain
// the directory where plugins are found.
// E.g. DRUPAL_ROOT . '/core/modules/views/src' may become
// DRUPAL_ROOT . '/core/modules/views/src/Plugin/Block'.
$plugin_namespaces[$namespace][] = $dir . $this->directorySuffix;
}
}
}
else {
// Both the namespace suffix and the directory suffix are empty,
// so the plugin namespaces and directories are the same as the base
// directories.
foreach ($this->rootNamespacesIterator as $namespace => $dirs) {
$plugin_namespaces[$namespace] = (array) $dirs;
} }
$plugin_namespaces[$namespace] = array($dir);
} }
return $plugin_namespaces; return $plugin_namespaces;

View File

@ -40,7 +40,7 @@ class MigrateActionConfigsTest extends MigrateDrupalTestBase {
parent::setUp(); parent::setUp();
$migration = entity_load('migration', 'd6_action_settings'); $migration = entity_load('migration', 'd6_action_settings');
$dumps = array( $dumps = array(
drupal_get_path('module', 'migrate_drupal') . '/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6ActionSettings.php', dirname(__DIR__) . '/Dump/Drupal6ActionSettings.php',
); );
$this->prepare($migration, $dumps); $this->prepare($migration, $dumps);
$executable = new MigrateExecutable($migration, $this); $executable = new MigrateExecutable($migration, $this);

View File

@ -40,7 +40,7 @@ class MigrateAggregatorConfigsTest extends MigrateDrupalTestBase {
parent::setUp(); parent::setUp();
$migration = entity_load('migration', 'd6_aggregator_settings'); $migration = entity_load('migration', 'd6_aggregator_settings');
$dumps = array( $dumps = array(
drupal_get_path('module', 'migrate_drupal') . '/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6AggregatorSettings.php', dirname(__DIR__) . '/Dump/Drupal6AggregatorSettings.php',
); );
$this->prepare($migration, $dumps); $this->prepare($migration, $dumps);
$executable = new MigrateExecutable($migration, $this); $executable = new MigrateExecutable($migration, $this);

View File

@ -41,7 +41,7 @@ class MigrateBookConfigsTest extends MigrateDrupalTestBase {
parent::setUp(); parent::setUp();
$migration = entity_load('migration', 'd6_book_settings'); $migration = entity_load('migration', 'd6_book_settings');
$dumps = array( $dumps = array(
drupal_get_path('module', 'migrate_drupal') . '/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6BookSettings.php', dirname(__DIR__) . '/Dump/Drupal6BookSettings.php',
); );
$this->prepare($migration, $dumps); $this->prepare($migration, $dumps);
$executable = new MigrateExecutable($migration, new MigrateMessage()); $executable = new MigrateExecutable($migration, new MigrateMessage());

View File

@ -41,7 +41,7 @@ class MigrateContactConfigsTest extends MigrateDrupalTestBase {
parent::setUp(); parent::setUp();
$migration = entity_load('migration', 'd6_contact_settings'); $migration = entity_load('migration', 'd6_contact_settings');
$dumps = array( $dumps = array(
drupal_get_path('module', 'migrate_drupal') . '/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6ContactSettings.php', dirname(__DIR__) . '/Dump/Drupal6ContactSettings.php',
); );
$this->prepare($migration, $dumps); $this->prepare($migration, $dumps);
$executable = new MigrateExecutable($migration, new MigrateMessage()); $executable = new MigrateExecutable($migration, new MigrateMessage());

View File

@ -41,7 +41,7 @@ class MigrateDblogConfigsTest extends MigrateDrupalTestBase {
parent::setUp(); parent::setUp();
$migration = entity_load('migration', 'd6_dblog_settings'); $migration = entity_load('migration', 'd6_dblog_settings');
$dumps = array( $dumps = array(
drupal_get_path('module', 'migrate_drupal') . '/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6DblogSettings.php', dirname(__DIR__) . '/Dump/Drupal6DblogSettings.php',
); );
$this->prepare($migration, $dumps); $this->prepare($migration, $dumps);
$executable = new MigrateExecutable($migration, new MigrateMessage()); $executable = new MigrateExecutable($migration, new MigrateMessage());

View File

@ -33,7 +33,7 @@ class MigrateFieldConfigsTest extends MigrateDrupalTestBase {
parent::setUp(); parent::setUp();
$migration = entity_load('migration', 'd6_field_settings'); $migration = entity_load('migration', 'd6_field_settings');
$dumps = array( $dumps = array(
drupal_get_path('module', 'migrate_drupal') . '/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6FieldSettings.php', dirname(__DIR__) . '/Dump/Drupal6FieldSettings.php',
); );
$this->prepare($migration, $dumps); $this->prepare($migration, $dumps);
$executable = new MigrateExecutable($migration, $this); $executable = new MigrateExecutable($migration, $this);

View File

@ -41,7 +41,7 @@ class MigrateFileConfigsTest extends MigrateDrupalTestBase {
parent::setUp(); parent::setUp();
$migration = entity_load('migration', 'd6_file_settings'); $migration = entity_load('migration', 'd6_file_settings');
$dumps = array( $dumps = array(
drupal_get_path('module', 'migrate_drupal') . '/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6FileSettings.php', dirname(__DIR__) . '/Dump/Drupal6FileSettings.php',
); );
$this->prepare($migration, $dumps); $this->prepare($migration, $dumps);
$executable = new MigrateExecutable($migration, new MigrateMessage()); $executable = new MigrateExecutable($migration, new MigrateMessage());

View File

@ -40,7 +40,7 @@ class MigrateForumConfigsTest extends MigrateDrupalTestBase {
parent::setUp(); parent::setUp();
$migration = entity_load('migration', 'd6_forum_settings'); $migration = entity_load('migration', 'd6_forum_settings');
$dumps = array( $dumps = array(
drupal_get_path('module', 'migrate_drupal') . '/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6ForumSettings.php', dirname(__DIR__) . '/Dump/Drupal6ForumSettings.php',
); );
$this->prepare($migration, $dumps); $this->prepare($migration, $dumps);
$executable = new MigrateExecutable($migration, $this); $executable = new MigrateExecutable($migration, $this);

View File

@ -40,7 +40,7 @@ class MigrateLocaleConfigsTest extends MigrateDrupalTestBase {
parent::setUp(); parent::setUp();
$migration = entity_load('migration', 'd6_locale_settings'); $migration = entity_load('migration', 'd6_locale_settings');
$dumps = array( $dumps = array(
drupal_get_path('module', 'migrate_drupal') . '/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6LocaleSettings.php', dirname(__DIR__) . '/Dump/Drupal6LocaleSettings.php',
); );
$this->prepare($migration, $dumps); $this->prepare($migration, $dumps);
$executable = new MigrateExecutable($migration, $this); $executable = new MigrateExecutable($migration, $this);

View File

@ -40,7 +40,7 @@ class MigrateMenuConfigsTest extends MigrateDrupalTestBase {
parent::setUp(); parent::setUp();
$migration = entity_load('migration', 'd6_menu_settings'); $migration = entity_load('migration', 'd6_menu_settings');
$dumps = array( $dumps = array(
drupal_get_path('module', 'migrate_drupal') . '/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6MenuSettings.php', dirname(__DIR__) . '/Dump/Drupal6MenuSettings.php',
); );
$this->prepare($migration, $dumps); $this->prepare($migration, $dumps);
$executable = new MigrateExecutable($migration, $this); $executable = new MigrateExecutable($migration, $this);

View File

@ -41,7 +41,7 @@ class MigrateNodeConfigsTest extends MigrateDrupalTestBase {
parent::setUp(); parent::setUp();
$migration = entity_load('migration', 'd6_node_settings'); $migration = entity_load('migration', 'd6_node_settings');
$dumps = array( $dumps = array(
drupal_get_path('module', 'migrate_drupal') . '/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6NodeSettings.php', dirname(__DIR__) . '/Dump/Drupal6NodeSettings.php',
); );
$this->prepare($migration, $dumps); $this->prepare($migration, $dumps);
$executable = new MigrateExecutable($migration, new MigrateMessage); $executable = new MigrateExecutable($migration, new MigrateMessage);

View File

@ -41,7 +41,7 @@ class MigrateSearchConfigsTest extends MigrateDrupalTestBase {
parent::setUp(); parent::setUp();
$migration = entity_load('migration', 'd6_search_settings'); $migration = entity_load('migration', 'd6_search_settings');
$dumps = array( $dumps = array(
drupal_get_path('module', 'migrate_drupal') . '/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6SearchSettings.php', dirname(__DIR__) . '/Dump/Drupal6SearchSettings.php',
); );
$this->prepare($migration, $dumps); $this->prepare($migration, $dumps);
$executable = new MigrateExecutable($migration, new MigrateMessage()); $executable = new MigrateExecutable($migration, new MigrateMessage());

View File

@ -41,7 +41,7 @@ class MigrateSimpletestConfigsTest extends MigrateDrupalTestBase {
parent::setUp(); parent::setUp();
$migration = entity_load('migration', 'd6_simpletest_settings'); $migration = entity_load('migration', 'd6_simpletest_settings');
$dumps = array( $dumps = array(
drupal_get_path('module', 'migrate_drupal') . '/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6SimpletestSettings.php', dirname(__DIR__) . '/Dump/Drupal6SimpletestSettings.php',
); );
$this->prepare($migration, $dumps); $this->prepare($migration, $dumps);
$executable = new MigrateExecutable($migration, new MigrateMessage()); $executable = new MigrateExecutable($migration, new MigrateMessage());

View File

@ -41,7 +41,7 @@ class MigrateStatisticsConfigsTest extends MigrateDrupalTestBase {
parent::setUp(); parent::setUp();
$migration = entity_load('migration', 'd6_statistics_settings'); $migration = entity_load('migration', 'd6_statistics_settings');
$dumps = array( $dumps = array(
drupal_get_path('module', 'migrate_drupal') . '/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6StatisticsSettings.php', dirname(__DIR__) . '/Dump/Drupal6StatisticsSettings.php',
); );
$this->prepare($migration, $dumps); $this->prepare($migration, $dumps);
$executable = new MigrateExecutable($migration, new MigrateMessage()); $executable = new MigrateExecutable($migration, new MigrateMessage());

View File

@ -40,7 +40,7 @@ class MigrateSyslogConfigsTest extends MigrateDrupalTestBase {
parent::setUp(); parent::setUp();
$migration = entity_load('migration', 'd6_syslog_settings'); $migration = entity_load('migration', 'd6_syslog_settings');
$dumps = array( $dumps = array(
drupal_get_path('module', 'migrate_drupal') . '/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6SyslogSettings.php', dirname(__DIR__) . '/Dump/Drupal6SyslogSettings.php',
); );
$this->prepare($migration, $dumps); $this->prepare($migration, $dumps);
$executable = new MigrateExecutable($migration, $this); $executable = new MigrateExecutable($migration, $this);

View File

@ -40,7 +40,7 @@ class MigrateTaxonomyConfigsTest extends MigrateDrupalTestBase {
parent::setUp(); parent::setUp();
$migration = entity_load('migration', 'd6_taxonomy_settings'); $migration = entity_load('migration', 'd6_taxonomy_settings');
$dumps = array( $dumps = array(
drupal_get_path('module', 'migrate_drupal') . '/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6TaxonomySettings.php', dirname(__DIR__) . '/Dump/Drupal6TaxonomySettings.php',
); );
$this->prepare($migration, $dumps); $this->prepare($migration, $dumps);
$executable = new MigrateExecutable($migration, $this); $executable = new MigrateExecutable($migration, $this);

View File

@ -40,7 +40,7 @@ class MigrateTextConfigsTest extends MigrateDrupalTestBase {
parent::setUp(); parent::setUp();
$migration = entity_load('migration', 'd6_text_settings'); $migration = entity_load('migration', 'd6_text_settings');
$dumps = array( $dumps = array(
drupal_get_path('module', 'migrate_drupal') . '/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6TextSettings.php', dirname(__DIR__) . '/Dump/Drupal6TextSettings.php',
); );
$this->prepare($migration, $dumps); $this->prepare($migration, $dumps);
$executable = new MigrateExecutable($migration, $this); $executable = new MigrateExecutable($migration, $this);

View File

@ -40,7 +40,7 @@ class MigrateUpdateConfigsTest extends MigrateDrupalTestBase {
parent::setUp(); parent::setUp();
$migration = entity_load('migration', 'd6_update_settings'); $migration = entity_load('migration', 'd6_update_settings');
$dumps = array( $dumps = array(
drupal_get_path('module', 'migrate_drupal') . '/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6UpdateSettings.php', dirname(__DIR__) . '/Dump/Drupal6UpdateSettings.php',
); );
$this->prepare($migration, $dumps); $this->prepare($migration, $dumps);
$executable = new MigrateExecutable($migration, $this); $executable = new MigrateExecutable($migration, $this);

View File

@ -33,7 +33,7 @@ class MigrateUserConfigsTest extends MigrateDrupalTestBase {
parent::setUp(); parent::setUp();
$migration = entity_load('migration', 'd6_user_mail'); $migration = entity_load('migration', 'd6_user_mail');
$dumps = array( $dumps = array(
drupal_get_path('module', 'migrate_drupal') . '/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6UserMail.php', dirname(__DIR__) . '/Dump/Drupal6UserMail.php',
); );
$this->prepare($migration, $dumps); $this->prepare($migration, $dumps);
$executable = new MigrateExecutable($migration, $this); $executable = new MigrateExecutable($migration, $this);

View File

@ -49,10 +49,9 @@ class MigrateUserRoleTest extends MigrateDrupalTestBase {
/** @var \Drupal\migrate\entity\Migration $migration */ /** @var \Drupal\migrate\entity\Migration $migration */
$migration = entity_load('migration', 'd6_user_role'); $migration = entity_load('migration', 'd6_user_role');
$path = drupal_get_path('module', 'migrate_drupal');
$dumps = array( $dumps = array(
$path . '/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6UserRole.php', dirname(__DIR__) . '/Dump/Drupal6UserRole.php',
$path . '/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6FilterFormat.php', dirname(__DIR__) . '/Dump/Drupal6FilterFormat.php',
); );
$this->prepare($migration, $dumps); $this->prepare($migration, $dumps);
$executable = new MigrateExecutable($migration, $this); $executable = new MigrateExecutable($migration, $this);

View File

@ -466,21 +466,25 @@ function simpletest_test_get_all($module = NULL) {
} }
$classes = array(); $classes = array();
foreach ($all_data as $name => $data) { foreach ($all_data as $name => $data) {
// Build directory in which the test files would reside. $extension_dir = DRUPAL_ROOT . '/' . $data->getPath();
$tests_dir = DRUPAL_ROOT . '/' . $data->getPath() . '/lib/Drupal/' . $name . '/Tests';
// Build directories in which the test files would reside.
$tests_dirs = array(
$extension_dir . '/lib/Drupal/' . $name . '/Tests',
$extension_dir . '/src/Tests',
);
$namespace = 'Drupal\\' . $name . '\Tests\\';
// Scan it for test files if it exists. // Scan it for test files if it exists.
if (is_dir($tests_dir)) { foreach ($tests_dirs as $tests_dir) {
$files = file_scan_directory($tests_dir, '/\.php$/'); if (is_dir($tests_dir)) {
if (!empty($files)) { $files = file_scan_directory($tests_dir, '/\.php$/');
$basedir = DRUPAL_ROOT . '/' . $data->getPath() . '/lib/'; if (!empty($files)) {
foreach ($files as $file) { $strlen = strlen($tests_dir) + 1;
// Convert the file name into the namespaced class name. // Convert the file names into the namespaced class names.
$replacements = array( foreach ($files as $file) {
'/' => '\\', $classes[] = $namespace . str_replace('/', '\\', substr($file->uri, $strlen, -4));
$basedir => '', }
'.php' => '',
);
$classes[] = strtr($file->uri, $replacements);
} }
} }
} }
@ -569,7 +573,10 @@ function simpletest_classloader_register() {
foreach ($types as $type) { foreach ($types as $type) {
foreach ($extensions[$type] as $name => $uri) { foreach ($extensions[$type] as $name => $uri) {
drupal_classloader_register($name, dirname($uri)); drupal_classloader_register($name, dirname($uri));
$classloader->add('Drupal\\' . $name . '\\Tests', DRUPAL_ROOT . '/' . dirname($uri) . '/tests'); $classloader->addPsr4('Drupal\\' . $name . '\\Tests\\', array(
DRUPAL_ROOT . '/' . dirname($uri) . '/tests/Drupal/' . $name . '/Tests',
DRUPAL_ROOT . '/' . dirname($uri) . '/tests/src',
));
// While being there, prime drupal_get_filename(). // While being there, prime drupal_get_filename().
drupal_get_filename($type, $name, $uri); drupal_get_filename($type, $name, $uri);
} }

View File

@ -22,7 +22,14 @@ class PhpUnitErrorTest extends UnitTestCase {
* Test errors reported. * Test errors reported.
*/ */
public function testPhpUnitXmlParsing() { public function testPhpUnitXmlParsing() {
require_once __DIR__ . '/../../../../simpletest.module'; // This test class could be either in tests/Drupal/simpletest/Tests/, or in
// tests/src/, after the PSR-4 transition.
if (file_exists(__DIR__ . '/../../simpletest.module')) {
require_once __DIR__ . '/../../simpletest.module';
}
else {
require_once __DIR__ . '/../../../../simpletest.module';
}
$phpunit_error_xml = __DIR__ . '/phpunit_error.xml'; $phpunit_error_xml = __DIR__ . '/phpunit_error.xml';
$res = simpletest_phpunit_xml_to_rows(1, $phpunit_error_xml); $res = simpletest_phpunit_xml_to_rows(1, $phpunit_error_xml);
$this->assertEquals(count($res), 4, 'All testcases got extracted'); $this->assertEquals(count($res), 4, 'All testcases got extracted');

View File

@ -57,7 +57,13 @@ class AnnotatedClassDiscoveryTest extends DiscoveryTestBase {
'provider' => 'plugin_test', 'provider' => 'plugin_test',
), ),
); );
$namespaces = new \ArrayObject(array('Drupal\plugin_test' => DRUPAL_ROOT . '/core/modules/system/tests/modules/plugin_test/lib')); $namespaces = new \ArrayObject(array(
'Drupal\plugin_test' => array(
// @todo Remove lib/Drupal/$module, once the switch to PSR-4 is complete.
DRUPAL_ROOT . '/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test',
DRUPAL_ROOT . '/core/modules/system/tests/modules/plugin_test/src',
),
));
$this->discovery = new AnnotatedClassDiscovery('Plugin/plugin_test/fruit', $namespaces); $this->discovery = new AnnotatedClassDiscovery('Plugin/plugin_test/fruit', $namespaces);
$this->emptyDiscovery = new AnnotatedClassDiscovery('Plugin/non_existing_module/non_existing_plugin_type', $namespaces); $this->emptyDiscovery = new AnnotatedClassDiscovery('Plugin/non_existing_module/non_existing_plugin_type', $namespaces);
} }

View File

@ -41,7 +41,13 @@ class CustomAnnotationClassDiscoveryTest extends DiscoveryTestBase {
'provider' => 'plugin_test', 'provider' => 'plugin_test',
), ),
); );
$root_namespaces = new \ArrayObject(array('Drupal\plugin_test' => DRUPAL_ROOT . '/core/modules/system/tests/modules/plugin_test/lib')); $root_namespaces = new \ArrayObject(array(
'Drupal\plugin_test' => array(
// @todo Remove lib/Drupal/$module, once the switch to PSR-4 is complete.
DRUPAL_ROOT . '/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test',
DRUPAL_ROOT . '/core/modules/system/tests/modules/plugin_test/src',
),
));
$this->discovery = new AnnotatedClassDiscovery('Plugin/plugin_test/custom_annotation', $root_namespaces, 'Drupal\plugin_test\Plugin\Annotation\PluginExample'); $this->discovery = new AnnotatedClassDiscovery('Plugin/plugin_test/custom_annotation', $root_namespaces, 'Drupal\plugin_test\Plugin\Annotation\PluginExample');
$this->emptyDiscovery = new AnnotatedClassDiscovery('Plugin/non_existing_module/non_existing_plugin_type', $root_namespaces, 'Drupal\plugin_test\Plugin\Annotation\PluginExample'); $this->emptyDiscovery = new AnnotatedClassDiscovery('Plugin/non_existing_module/non_existing_plugin_type', $root_namespaces, 'Drupal\plugin_test\Plugin\Annotation\PluginExample');

View File

@ -70,7 +70,21 @@ class CustomDirectoryAnnotatedClassDiscoveryTest extends DiscoveryTestBase {
'provider' => 'plugin_test', 'provider' => 'plugin_test',
), ),
); );
$namespaces = new \ArrayObject(array('Drupal\plugin_test' => DRUPAL_ROOT . '/core/modules/system/tests/modules/plugin_test/lib')); // Due to the transition from PSR-0 to PSR-4, plugin classes can be in
// either one of
// - core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/
// - core/modules/system/tests/modules/plugin_test/src/
// To avoid false positives with "Drupal\plugin_test\Drupal\plugin_test\..",
// only one of them can be registered.
// Note: This precaution is only needed if the plugin namespace is identical
// with the module namespace. Usually this is not the case, because every
// plugin namespace is like "Drupal\$module\Plugin\..".
// @todo Clean this up, once the transition to PSR-4 is complete.
$extension_dir = DRUPAL_ROOT . '/core/modules/system/tests/modules/plugin_test';
$base_directory = is_dir($extension_dir . '/lib/Drupal/plugin_test')
? $extension_dir . '/lib/Drupal/plugin_test'
: $extension_dir . '/src';
$namespaces = new \ArrayObject(array('Drupal\plugin_test' => $base_directory));
$this->discovery = new AnnotatedClassDiscovery('', $namespaces); $this->discovery = new AnnotatedClassDiscovery('', $namespaces);
$empty_namespaces = new \ArrayObject(); $empty_namespaces = new \ArrayObject();
$this->emptyDiscovery = new AnnotatedClassDiscovery('', $empty_namespaces); $this->emptyDiscovery = new AnnotatedClassDiscovery('', $empty_namespaces);

View File

@ -30,10 +30,7 @@ class ScriptTest extends UnitTestBase {
*/ */
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
$path_parts = explode(DIRECTORY_SEPARATOR, __DIR__); chdir(DRUPAL_ROOT);
// This file is 8 levels below the Drupal root.
$root = implode(DIRECTORY_SEPARATOR, array_slice($path_parts, 0, -8));
chdir($root);
} }
/** /**

View File

@ -18,7 +18,9 @@
<!-- Exclude Drush tests. --> <!-- Exclude Drush tests. -->
<exclude>./drush/tests</exclude> <exclude>./drush/tests</exclude>
<!-- Exclude special-case files from config's test modules. --> <!-- Exclude special-case files from config's test modules. -->
<!-- @todo Remove /lib/Drupal/config_test after the transition to PSR-4. -->
<exclude>./modules/config/tests/config_test/lib/Drupal/config_test</exclude> <exclude>./modules/config/tests/config_test/lib/Drupal/config_test</exclude>
<exclude>./modules/config/tests/config_test/src</exclude>
</testsuite> </testsuite>
</testsuites> </testsuites>
<!-- Filter for coverage reports. --> <!-- Filter for coverage reports. -->

View File

@ -56,6 +56,8 @@ function drupal_phpunit_contrib_extension_directory_roots() {
*/ */
function drupal_phpunit_register_extension_dirs(Composer\Autoload\ClassLoader $loader, $dirs) { function drupal_phpunit_register_extension_dirs(Composer\Autoload\ClassLoader $loader, $dirs) {
foreach ($dirs as $extension => $dir) { foreach ($dirs as $extension => $dir) {
// Register PSR-0 test directories.
// @todo Remove this, when the transition to PSR-4 is complete.
$lib_path = $dir . '/lib'; $lib_path = $dir . '/lib';
if (is_dir($lib_path)) { if (is_dir($lib_path)) {
$loader->add('Drupal\\' . $extension, $lib_path); $loader->add('Drupal\\' . $extension, $lib_path);
@ -64,6 +66,13 @@ function drupal_phpunit_register_extension_dirs(Composer\Autoload\ClassLoader $l
if (is_dir($tests_path)) { if (is_dir($tests_path)) {
$loader->add('Drupal\\' . $extension, $tests_path); $loader->add('Drupal\\' . $extension, $tests_path);
} }
// Register PSR-4 test directories.
if (is_dir($dir . '/src')) {
$loader->addPsr4('Drupal\\' . $extension . '\\', $dir . '/src');
}
if (is_dir($dir . '/tests/src')) {
$loader->addPsr4('Drupal\\' . $extension . '\Tests\\', $dir . '/tests/src');
}
} }
} }

View File

@ -27,9 +27,6 @@ return array(
'Psr\\Log\\' => array($vendorDir . '/psr/log'), 'Psr\\Log\\' => array($vendorDir . '/psr/log'),
'Gliph' => array($vendorDir . '/sdboyer/gliph/src'), 'Gliph' => array($vendorDir . '/sdboyer/gliph/src'),
'EasyRdf_' => array($vendorDir . '/easyrdf/easyrdf/lib'), 'EasyRdf_' => array($vendorDir . '/easyrdf/easyrdf/lib'),
'Drupal\\Driver' => array($baseDir . '/drivers/lib'),
'Drupal\\Core' => array($baseDir . '/core/lib'),
'Drupal\\Component' => array($baseDir . '/core/lib'),
'Doctrine\\Common\\Lexer\\' => array($vendorDir . '/doctrine/lexer/lib'), 'Doctrine\\Common\\Lexer\\' => array($vendorDir . '/doctrine/lexer/lib'),
'Doctrine\\Common\\Inflector\\' => array($vendorDir . '/doctrine/inflector/lib'), 'Doctrine\\Common\\Inflector\\' => array($vendorDir . '/doctrine/inflector/lib'),
'Doctrine\\Common\\Collections\\' => array($vendorDir . '/doctrine/collections/lib'), 'Doctrine\\Common\\Collections\\' => array($vendorDir . '/doctrine/collections/lib'),

View File

@ -8,4 +8,7 @@ $baseDir = dirname(dirname($vendorDir));
return array( return array(
'GuzzleHttp\\Stream\\' => array($vendorDir . '/guzzlehttp/streams/src'), 'GuzzleHttp\\Stream\\' => array($vendorDir . '/guzzlehttp/streams/src'),
'GuzzleHttp\\' => array($vendorDir . '/guzzlehttp/guzzle/src'), 'GuzzleHttp\\' => array($vendorDir . '/guzzlehttp/guzzle/src'),
'Drupal\\Driver\\' => array($baseDir . '/drivers/lib/Drupal/Driver'),
'Drupal\\Core\\' => array($baseDir . '/core/lib/Drupal/Core'),
'Drupal\\Component\\' => array($baseDir . '/core/lib/Drupal/Component'),
); );