public function createForNamespace($namespace, $fileContents)
    {
        $namespace = trim($namespace, '\\');
        $useStatements = [];
        $currentNamespace = '';
        $tokens = new \ArrayIterator(token_get_all($fileContents));

        while ($tokens->valid()) {
            switch ($tokens->current()[0]) {
                case T_NAMESPACE:
                    $currentNamespace = $this->parseNamespace($tokens);
                    break;
                case T_CLASS:
                    // Fast-forward the iterator through the class so that any
                    // T_USE tokens found within are skipped - these are not
                    // valid namespace use statements so should be ignored.
                    $braceLevel = 0;
                    $firstBraceFound = false;
                    while ($tokens->valid() && ($braceLevel > 0 || !$firstBraceFound)) {
                        if ($tokens->current() === '{'
                            || $tokens->current()[0] === T_CURLY_OPEN
                            || $tokens->current()[0] === T_DOLLAR_OPEN_CURLY_BRACES) {
                            if (!$firstBraceFound) {
                                $firstBraceFound = true;
                            }

                            ++$braceLevel;
                        }

                        if ($tokens->current() === '}') {
                            --$braceLevel;
                        }

                        $tokens->next();
                    }
                    break;
                case T_USE:
                    if ($currentNamespace === $namespace) {
                        $useStatements = array_merge($useStatements, $this->parseUseStatement($tokens));
                    }
                    break;
            }

            $tokens->next();
        }

        return new Context($namespace, $useStatements);
    }
public function createForNamespace($namespace, $fileContents)
    {
        $namespace = trim($namespace, '\\');
        $useStatements = [];
        $currentNamespace = '';
        $tokens = new \ArrayIterator(token_get_all($fileContents));

        while ($tokens->valid()) {
            switch ($tokens->current()[0]) {
                case T_NAMESPACE:
                    $currentNamespace = $this->parseNamespace($tokens);
                    break;
                case T_CLASS:
                    // Fast-forward the iterator through the class so that any
                    // T_USE tokens found within are skipped - these are not
                    // valid namespace use statements so should be ignored.
                    $braceLevel = 0;
                    $firstBraceFound = false;
                    while ($tokens->valid() && ($braceLevel > 0 || !$firstBraceFound)) {
                        if ($tokens->current() === '{'
                            || $tokens->current()[0] === T_CURLY_OPEN
                            || $tokens->current()[0] === T_DOLLAR_OPEN_CURLY_BRACES) {
                            if (!$firstBraceFound) {
                                $firstBraceFound = true;
                            }

                            ++$braceLevel;
                        }

                        if ($tokens->current() === '}') {
                            --$braceLevel;
                        }

                        $tokens->next();
                    }
                    break;
                case T_USE:
                    if ($currentNamespace === $namespace) {
                        $useStatements = array_merge($useStatements, $this->parseUseStatement($tokens));
                    }
                    break;
            }

            $tokens->next();
        }

        return new Context($namespace, $useStatements);
    }
public function createForNamespace ( $ namespace , $ fileContents ) { $ namespace = trim ( $ namespace , '\\' ) ; $ useStatements = [ ] ; $ currentNamespace = '' ; $ tokens = new \ ArrayIterator ( token_get_all ( $ fileContents ) ) ; while ( $ tokens -> valid ( ) ) { switch ( $ tokens -> current ( ) [ 0 ] ) { case T_NAMESPACE : $ currentNamespace = $ this -> parseNamespace ( $ tokens ) ; break ; case T_CLASS : // Fast-forward the iterator through the class so that any // T_USE tokens found within are skipped - these are not // valid namespace use statements so should be ignored. $ braceLevel = 0 ; $ firstBraceFound = false ; while ( $ tokens -> valid ( ) && ( $ braceLevel > 0 || ! $ firstBraceFound ) ) { if ( $ tokens -> current ( ) === '{' || $ tokens -> current ( ) [ 0 ] === T_CURLY_OPEN || $ tokens -> current ( ) [ 0 ] === T_DOLLAR_OPEN_CURLY_BRACES ) { if ( ! $ firstBraceFound ) { $ firstBraceFound = true ; } ++ $ braceLevel ; } if ( $ tokens -> current ( ) === '}' ) { -- $ braceLevel ; } $ tokens -> next ( ) ; } break ; case T_USE : if ( $ currentNamespace === $ namespace ) { $ useStatements = array_merge ( $ useStatements , $ this -> parseUseStatement ( $ tokens ) ) ; } break ; } $ tokens -> next ( ) ; } return new Context ( $ namespace , $ useStatements ) ; }
Build a Context for a namespace in the provided file contents.

@param string $namespace It does not matter if a `\` precedes the namespace name, this method first normalizes.
@param string $fileContents the file's contents to retrieve the aliases from with the given namespace.

@see Context for more information on Contexts.

@return Context
Build a Context for a namespace in the provided file contents .