12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- namespace Doctrine\Common\Annotations;
- use SplFileObject;
- final class PhpParser
- {
-
- 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());
- if (null === $content) {
- return array();
- }
- $namespace = preg_quote($class->getNamespaceName());
- $content = preg_replace('/^.*?(\bnamespace\s+' . $namespace . '\s*[;{].*)$/s', '\\1', $content);
- $tokenizer = new TokenParser('<?php ' . $content);
- $statements = $tokenizer->parseUseStatements($class->getNamespaceName());
- return $statements;
- }
-
- private function getFileContent($filename, $lineNumber)
- {
- if ( ! is_file($filename)) {
- return null;
- }
- $content = '';
- $lineCnt = 0;
- $file = new SplFileObject($filename);
- while (!$file->eof()) {
- if ($lineCnt++ == $lineNumber) {
- break;
- }
- $content .= $file->fgets();
- }
- return $content;
- }
- }
|