public static function ClassCollectionLoader::fixNamespaceDeclarations

Adds brackets around each namespace if it's not already the case.

Parameters

string $source Namespace string:

Return value

string Namespaces with brackets

2 calls to ClassCollectionLoader::fixNamespaceDeclarations()
ClassCollectionLoader::load in drupal/core/vendor/symfony/class-loader/Symfony/Component/ClassLoader/ClassCollectionLoader.php
Loads a list of classes and caches them in one big file.
ClassCollectionLoaderTest::testFixNamespaceDeclarations in drupal/core/vendor/symfony/class-loader/Symfony/Component/ClassLoader/Tests/ClassCollectionLoaderTest.php

File

drupal/core/vendor/symfony/class-loader/Symfony/Component/ClassLoader/ClassCollectionLoader.php, line 136

Class

ClassCollectionLoader
ClassCollectionLoader.

Namespace

Symfony\Component\ClassLoader

Code

public static function fixNamespaceDeclarations($source) {
  if (!function_exists('token_get_all')) {
    return $source;
  }
  $output = '';
  $inNamespace = false;
  $tokens = token_get_all($source);
  for ($i = 0, $max = count($tokens); $i < $max; $i++) {
    $token = $tokens[$i];
    if (is_string($token)) {
      $output .= $token;
    }
    elseif (in_array($token[0], array(
      T_COMMENT,
      T_DOC_COMMENT,
    ))) {

      // strip comments
      continue;
    }
    elseif (T_NAMESPACE === $token[0]) {
      if ($inNamespace) {
        $output .= "}\n";
      }
      $output .= $token[1];

      // namespace name and whitespaces
      while (($t = $tokens[++$i]) && is_array($t) && in_array($t[0], array(
        T_WHITESPACE,
        T_NS_SEPARATOR,
        T_STRING,
      ))) {
        $output .= $t[1];
      }
      if (is_string($t) && '{' === $t) {
        $inNamespace = false;
        --$i;
      }
      else {
        $output = rtrim($output);
        $output .= "\n{";
        $inNamespace = true;
      }
    }
    else {
      $output .= $token[1];
    }
  }
  if ($inNamespace) {
    $output .= "}\n";
  }
  return $output;
}