function drupal_classloader

Initializes and returns the class loader.

The class loader is responsible for lazy-loading all PSR-0 compatible classes, interfaces, and traits (PHP 5.4 and later). Its only dependencies are DRUPAL_ROOT and variable_get(). Otherwise it may be called as early as possible.

Return value

Symfony\Component\ClassLoader\UniversalClassLoader A UniversalClassLoader class instance (or extension thereof).

11 calls to drupal_classloader()
DrupalKernelTest::testCompileDIC in drupal/core/modules/system/lib/Drupal/system/Tests/DrupalKernel/DrupalKernelTest.php
Tests DIC compilation.
DrupalUnitTestBase::setUp in drupal/core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php
Sets up Drupal unit test environment.
drupal_classloader_register in drupal/core/includes/bootstrap.inc
Registers an additional namespace.
drupal_install_system in drupal/core/includes/install.inc
Installs the system module.
http.php in drupal/core/modules/system/tests/http.php
Fake an HTTP request, for use during testing.

... See full list

File

drupal/core/includes/bootstrap.inc, line 3062
Functions that need to be loaded on every Drupal request.

Code

function drupal_classloader() {

  // By default, use the UniversalClassLoader which is best for development,
  // as it does not break when code is moved on the file system. However, as it
  // is slow, allow to use the APC class loader in production.
  static $loader;
  if (!isset($loader)) {

    // Include the Symfony ClassLoader for loading PSR-0-compatible classes.
    require_once DRUPAL_ROOT . '/core/vendor/symfony/class-loader/Symfony/Component/ClassLoader/UniversalClassLoader.php';

    // @todo Use a cleaner way than variable_get() to switch autoloaders.
    switch (variable_get('autoloader_mode', 'default')) {
      case 'apc':
        if (function_exists('apc_store')) {
          require_once DRUPAL_ROOT . '/core/vendor/symfony/class-loader/Symfony/Component/ClassLoader/ApcUniversalClassLoader.php';
          $loader = new ApcUniversalClassLoader('drupal.' . drupal_get_hash_salt());
          break;
        }

      // Fall through to the default loader if APC was not loaded, so that the
      // site does not fail completely.
      case 'dev':
      case 'default':
      default:
        $loader = new UniversalClassLoader();
        break;
    }

    // Register explicit namespaces for Drupal core.
    // The majority of namespaces that need to be resolved are from Drupal core,
    // so registering/setting them before vendor libraries saves a few
    // additional cycles per class lookup.
    $loader
      ->registerNamespaces(array(
      'Drupal\\Core' => DRUPAL_ROOT . '/core/lib',
      'Drupal\\Component' => DRUPAL_ROOT . '/core/lib',
    ));

    // Register namespaces for vendor libraries managed by Composer.
    $namespaces = (require DRUPAL_ROOT . '/core/vendor/composer/autoload_namespaces.php');
    $prefixes = array();
    foreach ($namespaces as $namespace => $path) {

      // Composer combines libraries that use PHP 5.3 namespaces and ones that
      // use PEAR-style class prefixes in a single array, but the Symfony class
      // loader requires them to be registered separately. PSR-0 disallows
      // underscores in namespace names and requires at least one in a
      // PEAR-style class prefix.
      if (strpos($namespace, '_') !== FALSE) {
        $prefixes[$namespace] = $path;
        unset($namespaces[$namespace]);
      }
    }
    $loader
      ->registerPrefixes($prefixes);
    $loader
      ->registerNamespaces($namespaces);

    // Register the loader with PHP.
    $loader
      ->register();
  }
  return $loader;
}