Chris@0: . Chris@0: */ Chris@0: Chris@0: namespace Doctrine\Common\Annotations; Chris@0: Chris@0: use SplFileObject; Chris@0: Chris@0: /** Chris@0: * Parses a file for namespaces/use/class declarations. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: * @author Christian Kaps Chris@0: */ Chris@0: final class PhpParser Chris@0: { Chris@0: /** Chris@0: * Parses a class. Chris@0: * Chris@0: * @param \ReflectionClass $class A ReflectionClass object. Chris@0: * Chris@0: * @return array A list with use statements in the form (Alias => FQN). Chris@0: */ Chris@0: public function parseClass(\ReflectionClass $class) Chris@0: { Chris@0: if (method_exists($class, 'getUseStatements')) { Chris@0: return $class->getUseStatements(); Chris@0: } Chris@0: Chris@12: if (false === $filename = $class->getFileName()) { Chris@0: return array(); Chris@0: } Chris@0: Chris@0: $content = $this->getFileContent($filename, $class->getStartLine()); Chris@0: Chris@0: if (null === $content) { Chris@0: return array(); Chris@0: } Chris@0: Chris@0: $namespace = preg_quote($class->getNamespaceName()); Chris@0: $content = preg_replace('/^.*?(\bnamespace\s+' . $namespace . '\s*[;{].*)$/s', '\\1', $content); Chris@0: $tokenizer = new TokenParser('parseUseStatements($class->getNamespaceName()); Chris@0: Chris@0: return $statements; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the content of the file right up to the given line number. Chris@0: * Chris@0: * @param string $filename The name of the file to load. Chris@0: * @param integer $lineNumber The number of lines to read from file. Chris@0: * Chris@12: * @return string|null The content of the file or null if the file does not exist. Chris@0: */ Chris@0: private function getFileContent($filename, $lineNumber) Chris@0: { Chris@0: if ( ! is_file($filename)) { Chris@0: return null; Chris@0: } Chris@0: Chris@0: $content = ''; Chris@0: $lineCnt = 0; Chris@0: $file = new SplFileObject($filename); Chris@0: while (!$file->eof()) { Chris@0: if ($lineCnt++ == $lineNumber) { Chris@0: break; Chris@0: } Chris@0: Chris@0: $content .= $file->fgets(); Chris@0: } Chris@0: Chris@0: return $content; Chris@0: } Chris@0: }