You are here

public function DebugClassLoader::loadClass in Zircon Profile 8

Same name in this branch
  1. 8 vendor/symfony/debug/DebugClassLoader.php \Symfony\Component\Debug\DebugClassLoader::loadClass()
  2. 8 vendor/symfony/class-loader/DebugClassLoader.php \Symfony\Component\ClassLoader\DebugClassLoader::loadClass()
Same name and namespace in other branches
  1. 8.0 vendor/symfony/debug/DebugClassLoader.php \Symfony\Component\Debug\DebugClassLoader::loadClass()

Loads the given class or interface.

Parameters

string $class The name of the class:

Return value

bool|null True, if loaded

Throws

\RuntimeException

File

vendor/symfony/debug/DebugClassLoader.php, line 143

Class

DebugClassLoader
Autoloader checking if the class is really defined in the file found.

Namespace

Symfony\Component\Debug

Code

public function loadClass($class) {
  ErrorHandler::stackErrors();
  try {
    if ($this->isFinder) {
      if ($file = $this->classLoader[0]
        ->findFile($class)) {
        require $file;
      }
    }
    else {
      call_user_func($this->classLoader, $class);
      $file = false;
    }
  } catch (\Exception $e) {
    ErrorHandler::unstackErrors();
    throw $e;
  }
  ErrorHandler::unstackErrors();
  $exists = class_exists($class, false) || interface_exists($class, false) || function_exists('trait_exists') && trait_exists($class, false);
  if ('\\' === $class[0]) {
    $class = substr($class, 1);
  }
  if ($exists) {
    $refl = new \ReflectionClass($class);
    $name = $refl
      ->getName();
    if ($name !== $class && 0 === strcasecmp($name, $class)) {
      throw new \RuntimeException(sprintf('Case mismatch between loaded and declared class names: %s vs %s', $class, $name));
    }
    if (in_array(strtolower($refl
      ->getShortName()), self::$php7Reserved)) {
      @trigger_error(sprintf('%s uses a reserved class name (%s) that will break on PHP 7 and higher', $name, $refl
        ->getShortName()), E_USER_DEPRECATED);
    }
    elseif (preg_match('#\\n \\* @deprecated (.*?)\\r?\\n \\*(?: @|/$)#s', $refl
      ->getDocComment(), $notice)) {
      self::$deprecated[$name] = preg_replace('#\\s*\\r?\\n \\* +#', ' ', $notice[1]);
    }
    else {
      if (2 > ($len = 1 + (strpos($name, '\\', 1 + strpos($name, '\\')) ?: strpos($name, '_')))) {
        $len = 0;
        $ns = '';
      }
      else {
        switch ($ns = substr($name, 0, $len)) {
          case 'Symfony\\Bridge\\':
          case 'Symfony\\Bundle\\':
          case 'Symfony\\Component\\':
            $ns = 'Symfony\\';
            $len = strlen($ns);
            break;
        }
      }
      $parent = $refl
        ->getParentClass();
      if (!$parent || strncmp($ns, $parent->name, $len)) {
        if ($parent && isset(self::$deprecated[$parent->name]) && strncmp($ns, $parent->name, $len)) {
          @trigger_error(sprintf('The %s class extends %s that is deprecated %s', $name, $parent->name, self::$deprecated[$parent->name]), E_USER_DEPRECATED);
        }
        foreach ($refl
          ->getInterfaceNames() as $interface) {
          if (isset(self::$deprecated[$interface]) && strncmp($ns, $interface, $len) && !($parent && $parent
            ->implementsInterface($interface))) {
            @trigger_error(sprintf('The %s %s %s that is deprecated %s', $name, $refl
              ->isInterface() ? 'interface extends' : 'class implements', $interface, self::$deprecated[$interface]), E_USER_DEPRECATED);
          }
        }
      }
    }
  }
  if ($file) {
    if (!$exists) {
      if (false !== strpos($class, '/')) {
        throw new \RuntimeException(sprintf('Trying to autoload a class with an invalid name "%s". Be careful that the namespace separator is "\\" in PHP, not "/".', $class));
      }
      throw new \RuntimeException(sprintf('The autoloader expected class "%s" to be defined in file "%s". The file was found but the class was not in it, the class name or namespace probably has a typo.', $class, $file));
    }
    if (self::$caseCheck) {
      $real = explode('\\', $class . strrchr($file, '.'));
      $tail = explode(DIRECTORY_SEPARATOR, str_replace('/', DIRECTORY_SEPARATOR, $file));
      $i = count($tail) - 1;
      $j = count($real) - 1;
      while (isset($tail[$i], $real[$j]) && $tail[$i] === $real[$j]) {
        --$i;
        --$j;
      }
      array_splice($tail, 0, $i + 1);
    }
    if (self::$caseCheck && $tail) {
      $tail = DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $tail);
      $tailLen = strlen($tail);
      $real = $refl
        ->getFileName();
      if (2 === self::$caseCheck) {

        // realpath() on MacOSX doesn't normalize the case of characters
        $i = 1 + strrpos($real, '/');
        $file = substr($real, $i);
        $real = substr($real, 0, $i);
        if (isset(self::$darwinCache[$real])) {
          $kDir = $real;
        }
        else {
          $kDir = strtolower($real);
          if (isset(self::$darwinCache[$kDir])) {
            $real = self::$darwinCache[$kDir][0];
          }
          else {
            $dir = getcwd();
            chdir($real);
            $real = getcwd() . '/';
            chdir($dir);
            $dir = $real;
            $k = $kDir;
            $i = strlen($dir) - 1;
            while (!isset(self::$darwinCache[$k])) {
              self::$darwinCache[$k] = array(
                $dir,
                array(),
              );
              self::$darwinCache[$dir] =& self::$darwinCache[$k];
              while ('/' !== $dir[--$i]) {
              }
              $k = substr($k, 0, ++$i);
              $dir = substr($dir, 0, $i--);
            }
          }
        }
        $dirFiles = self::$darwinCache[$kDir][1];
        if (isset($dirFiles[$file])) {
          $kFile = $file;
        }
        else {
          $kFile = strtolower($file);
          if (!isset($dirFiles[$kFile])) {
            foreach (scandir($real, 2) as $f) {
              if ('.' !== $f[0]) {
                $dirFiles[$f] = $f;
                if ($f === $file) {
                  $kFile = $k = $file;
                }
                elseif ($f !== ($k = strtolower($f))) {
                  $dirFiles[$k] = $f;
                }
              }
            }
            self::$darwinCache[$kDir][1] = $dirFiles;
          }
        }
        $real .= $dirFiles[$kFile];
      }
      if (0 === substr_compare($real, $tail, -$tailLen, $tailLen, true) && 0 !== substr_compare($real, $tail, -$tailLen, $tailLen, false)) {
        throw new \RuntimeException(sprintf('Case mismatch between class and real file names: %s vs %s in %s', substr($tail, -$tailLen + 1), substr($real, -$tailLen + 1), substr($real, 0, -$tailLen + 1)));
      }
    }
    return true;
  }
}