How is the class loader set to load the files containing classes? I was trying to understand how Drupal 8 initializes the class loader to look for files containing classes in core/lib and modules/*/src.

I found out that the class loader is set to an instance of ApcClassLoader in DrupalKernel::initializeSettings().

  // If the class loader is still the same, possibly upgrade to the APC class
  // loader.
  // ApcClassLoader does not support APCu without backwards compatibility
  // enabled.
  if ($class_loader_class == get_class($this-&gt;classLoader)
   &amp;&amp; Settings::get('class_loader_auto_detect', TRUE)
     &amp;&amp; extension_loaded('apc')) {
    $prefix = Settings::getApcuPrefix('class_loader', $this-&gt;root);
    $apc_loader = new \Symfony\Component\ClassLoader\ApcClassLoader($prefix, $this-&gt;classLoader);
    $this-&gt;classLoader-&gt;unregister();
    $apc_loader-&gt;register();
    $this-&gt;classLoader = $apc_loader;
  }


Following the methods that are called after that, I cannot find the exact method setting the class loader to correctly find the files containing Drupal classes. I expect it to be done from Drupal code, since the directory layout is very specific for Drupal.

Which method set the class loader to look in the right directories Drupal uses?
For modules with composer dependencies in 7.x, try https://www.drupal.org/project/composer_manager. If you want to support modules with PSR-4 autoloading, see https://www.drupal.org/project/registry_autoload (integrates with the standard 7.x class loading registry) or https://www.drupal.org/project/xautoload.

That said, the code you pasted is just the APCu wrapper for the class loader. The actually relevant line is this, in \Drupal\Core\DrupalKernel::discoverServiceProviders():

$this-&gt;classLoaderAddMultiplePsr4($this-&gt;getModuleNamespacesPsr4($module_filenames));


What it does is register a PSR-4 namespace for each module with composer. Below that, everything follows the standard rules.

But composer dependencies is something else, that's handled automatically by composer's autoload.php and all the files that that then loads. Which is basically what composer_manager does, load the generated autoload.php file.