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

3 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
@dataProvider getFixNamespaceDeclarationsData
ClassCollectionLoaderTest::testFixNamespaceDeclarationsWithoutTokenizer in drupal/core/vendor/symfony/class-loader/Symfony/Component/ClassLoader/Tests/ClassCollectionLoaderTest.php
@dataProvider getFixNamespaceDeclarationsDataWithoutTokenizer

File

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

Class

ClassCollectionLoader
ClassCollectionLoader.

Namespace

Symfony\Component\ClassLoader

Code

public static function fixNamespaceDeclarations($source) {
  if (!function_exists('token_get_all') || !self::$useTokenizer) {
    if (preg_match('/namespace(.*?)\\s*;/', $source)) {
      $source = preg_replace('/namespace(.*?)\\s*;/', "namespace\$1\n{", $source) . "}\n";
    }
    return $source;
  }
  $rawChunk = '';
  $output = '';
  $inNamespace = false;
  $tokens = token_get_all($source);
  for (reset($tokens); false !== ($token = current($tokens)); next($tokens)) {
    if (is_string($token)) {
      $rawChunk .= $token;
    }
    elseif (in_array($token[0], array(
      T_COMMENT,
      T_DOC_COMMENT,
    ))) {

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

      // namespace name and whitespaces
      while (($t = next($tokens)) && is_array($t) && in_array($t[0], array(
        T_WHITESPACE,
        T_NS_SEPARATOR,
        T_STRING,
      ))) {
        $rawChunk .= $t[1];
      }
      if ('{' === $t) {
        $inNamespace = false;
        prev($tokens);
      }
      else {
        $rawChunk = rtrim($rawChunk) . "\n{";
        $inNamespace = true;
      }
    }
    elseif (T_START_HEREDOC === $token[0]) {
      $output .= self::compressCode($rawChunk) . $token[1];
      do {
        $token = next($tokens);
        $output .= is_string($token) ? $token : $token[1];
      } while ($token[0] !== T_END_HEREDOC);
      $output .= "\n";
      $rawChunk = '';
    }
    elseif (T_CONSTANT_ENCAPSED_STRING === $token[0]) {
      $output .= self::compressCode($rawChunk) . $token[1];
      $rawChunk = '';
    }
    else {
      $rawChunk .= $token[1];
    }
  }
  if ($inNamespace) {
    $rawChunk .= "}\n";
  }
  return $output . self::compressCode($rawChunk);
}