function _simpletest_autoload_psr0

7.x simpletest.module _simpletest_autoload_psr0($class)

Autoload callback to find PSR-0 test classes.

This will only work on classes where the namespace is of the pattern "Drupal\$extension\Tests\.."

1 string reference to '_simpletest_autoload_psr0'
simpletest_classloader_register in drupal-7.x/modules/simpletest/simpletest.module

File

drupal-7.x/modules/simpletest/simpletest.module, line 419
Provides testing functionality.

Code

function _simpletest_autoload_psr0($class) {

  // Static cache for extension paths.
  // This cache is lazily filled as soon as it is needed.
  static $extensions;

  // Check that the first namespace fragment is "Drupal\"
  if (substr($class, 0, 7) === 'Drupal\\') {
    // Find the position of the second namespace separator.
    $pos = strpos($class, '\\', 7);
    // Check that the third namespace fragment is "\Tests\".
    if (substr($class, $pos, 7) === '\\Tests\\') {

      // Extract the second namespace fragment, which we expect to be the
      // extension name.
      $extension = substr($class, 7, $pos - 7);

      // Lazy-load the extension paths, both enabled and disabled.
      if (!isset($extensions)) {
        $extensions = db_query("SELECT name, filename FROM {system}")->fetchAllKeyed();
      }

      // Check if the second namespace fragment is a known extension name.
      if (isset($extensions[$extension])) {

        // Split the class into namespace and classname.
        $nspos = strrpos($class, '\\');
        $namespace = substr($class, 0, $nspos);
        $classname = substr($class, $nspos + 1);

        // Build the filepath where we expect the class to be defined.
        $path = dirname($extensions[$extension]) . '/lib/' .
          str_replace('\\', '/', $namespace) . '/' .
          str_replace('_', '/', $classname) . '.php';

        // Include the file, if it does exist.
        if (file_exists($path)) {
          include $path;
        }
      }
    }
  }
}