You are here

public static function ClassCollectionLoader::load in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/symfony/class-loader/ClassCollectionLoader.php \Symfony\Component\ClassLoader\ClassCollectionLoader::load()

Loads a list of classes and caches them in one big file.

Parameters

array $classes An array of classes to load:

string $cacheDir A cache directory:

string $name The cache name prefix:

bool $autoReload Whether to flush the cache when the cache is stale or not:

bool $adaptive Whether to remove already declared classes or not:

string $extension File extension of the resulting file:

Throws

\InvalidArgumentException When class can't be loaded

3 calls to ClassCollectionLoader::load()
ClassCollectionLoaderTest::testCommentStripping in vendor/symfony/class-loader/Tests/ClassCollectionLoaderTest.php
ClassCollectionLoaderTest::testUnableToLoadClassException in vendor/symfony/class-loader/Tests/ClassCollectionLoaderTest.php
@expectedException \InvalidArgumentException
Kernel::doLoadClassCache in vendor/symfony/http-kernel/Kernel.php

File

vendor/symfony/class-loader/ClassCollectionLoader.php, line 37

Class

ClassCollectionLoader
ClassCollectionLoader.

Namespace

Symfony\Component\ClassLoader

Code

public static function load($classes, $cacheDir, $name, $autoReload, $adaptive = false, $extension = '.php') {

  // each $name can only be loaded once per PHP process
  if (isset(self::$loaded[$name])) {
    return;
  }
  self::$loaded[$name] = true;
  $declared = array_merge(get_declared_classes(), get_declared_interfaces());
  if (function_exists('get_declared_traits')) {
    $declared = array_merge($declared, get_declared_traits());
  }
  if ($adaptive) {

    // don't include already declared classes
    $classes = array_diff($classes, $declared);

    // the cache is different depending on which classes are already declared
    $name = $name . '-' . substr(hash('sha256', implode('|', $classes)), 0, 5);
  }
  $classes = array_unique($classes);
  $cache = $cacheDir . '/' . $name . $extension;

  // auto-reload
  $reload = false;
  if ($autoReload) {
    $metadata = $cache . '.meta';
    if (!is_file($metadata) || !is_file($cache)) {
      $reload = true;
    }
    else {
      $time = filemtime($cache);
      $meta = unserialize(file_get_contents($metadata));
      sort($meta[1]);
      sort($classes);
      if ($meta[1] != $classes) {
        $reload = true;
      }
      else {
        foreach ($meta[0] as $resource) {
          if (!is_file($resource) || filemtime($resource) > $time) {
            $reload = true;
            break;
          }
        }
      }
    }
  }
  if (!$reload && is_file($cache)) {
    require_once $cache;
    return;
  }
  $files = array();
  $content = '';
  foreach (self::getOrderedClasses($classes) as $class) {
    if (in_array($class
      ->getName(), $declared)) {
      continue;
    }
    $files[] = $class
      ->getFileName();
    $c = preg_replace(array(
      '/^\\s*<\\?php/',
      '/\\?>\\s*$/',
    ), '', file_get_contents($class
      ->getFileName()));

    // fakes namespace declaration for global code
    if (!$class
      ->inNamespace()) {
      $c = "\nnamespace\n{\n" . $c . "\n}\n";
    }
    $c = self::fixNamespaceDeclarations('<?php ' . $c);
    $c = preg_replace('/^\\s*<\\?php/', '', $c);
    $content .= $c;
  }

  // cache the core classes
  if (!is_dir(dirname($cache))) {
    mkdir(dirname($cache), 0777, true);
  }
  self::writeCacheFile($cache, '<?php ' . $content);
  if ($autoReload) {

    // save the resources
    self::writeCacheFile($metadata, serialize(array(
      $files,
      $classes,
    )));
  }
}