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). It's only dependency is DRUPAL_ROOT. Otherwise it may be called as early as possible.

Parameters

$class_loader: The name of class loader to use. This can be used to change the class loader class when calling drupal_classloader() from settings.php. It is ignored otherwise.

Return value

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

12 calls to drupal_classloader()
DrupalKernelTest::testCompileDIC in drupal/core/modules/system/lib/Drupal/system/Tests/DrupalKernel/DrupalKernelTest.php
Tests DIC compilation.
DrupalKernelTest::testSerialization in drupal/core/modules/system/lib/Drupal/system/Tests/DrupalKernel/DrupalKernelTest.php
Tests kernel serialization/unserialization.
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_handle_request in drupal/core/includes/bootstrap.inc
Handles an entire PHP request.

... See full list

File

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

Code

function drupal_classloader($class_loader = NULL) {

  // By default, use the ClassLoader 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/ClassLoader.php';
    $loader = new ClassLoader();

    // Register the class loader.
    // When configured to use APC, the ApcClassLoader is registered instead.
    // Note that ApcClassLoader decorates ClassLoader and only provides the
    // findFile() method, but none of the others. The actual registry is still
    // in ClassLoader.
    if (!isset($class_loader)) {
      $class_loader = settings()
        ->get('class_loader', 'default');
    }
    if ($class_loader === 'apc') {
      require_once DRUPAL_ROOT . '/core/vendor/symfony/class-loader/Symfony/Component/ClassLoader/ApcClassLoader.php';
      $apc_loader = new ApcClassLoader('drupal.' . drupal_get_hash_salt(), $loader);
      $apc_loader
        ->register();
    }
    else {
      $loader
        ->register();
    }

    // Register namespaces for vendor libraries managed by Composer.
    $prefixes_and_namespaces = (require DRUPAL_ROOT . '/core/vendor/composer/autoload_namespaces.php');
    $loader
      ->addPrefixes($prefixes_and_namespaces);

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