PhpParser.php

Namespace

Doctrine\Common\Annotations

File

drupal/core/vendor/doctrine/common/lib/Doctrine/Common/Annotations/PhpParser.php
View source
<?php

/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the MIT license. For more information, see
 * <http://www.doctrine-project.org>.
 */
namespace Doctrine\Common\Annotations;

use SplFileObject;

/**
 * Parses a file for namespaces/use/class declarations.
 *
 * @author Fabien Potencier <fabien@symfony.com>
 * @author Christian Kaps <christian.kaps@mohiva.com>
 */
final class PhpParser {

  /**
   * Parses a class.
   *
   * @param  \ReflectionClass $class A <code>ReflectionClass</code> object.
   * @return array            A list with use statements in the form (Alias => FQN).
   */
  public function parseClass(\ReflectionClass $class) {
    if (method_exists($class, 'getUseStatements')) {
      return $class
        ->getUseStatements();
    }
    if (false === ($filename = $class
      ->getFilename())) {
      return array();
    }
    $content = $this
      ->getFileContent($filename, $class
      ->getStartLine());
    $namespace = str_replace('\\', '\\\\', $class
      ->getNamespaceName());
    $content = preg_replace('/^.*?(\\bnamespace\\s+' . $namespace . '\\s*[;{].*)$/s', '\\1', $content);
    $tokenizer = new TokenParser('<?php ' . $content);
    $statements = $tokenizer
      ->parseUseStatements($class
      ->getNamespaceName());
    return $statements;
  }

  /**
   * Get the content of the file right up to the given line number.
   *
   * @param  string $filename   The name of the file to load.
   * @param  int    $lineNumber The number of lines to read from file.
   * @return string The content of the file.
   */
  private function getFileContent($filename, $lineNumber) {
    $content = '';
    $lineCnt = 0;
    $file = new SplFileObject($filename);
    while (!$file
      ->eof()) {
      if ($lineCnt++ == $lineNumber) {
        break;
      }
      $content .= $file
        ->fgets();
    }
    return $content;
  }

}

Classes

Namesort descending Description
PhpParser Parses a file for namespaces/use/class declarations.