Skip to content

Commit

Permalink
Major restructure: renamed to StaticReflection, moved under Reflectio…
Browse files Browse the repository at this point in the history
…n namespace, injecting the token parser.
  • Loading branch information
chx committed Jun 25, 2012
1 parent db12028 commit 9cf5ebd
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 82 deletions.
8 changes: 3 additions & 5 deletions lib/Doctrine/Common/Annotations/PhpParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @author Fabien Potencier <fabien@symfony.com>
* @author Christian Kaps <christian.kaps@mohiva.com>
*/
final class PhpParser extends TokenParser
final class PhpParser
{
/**
* Parses a class.
Expand All @@ -48,11 +48,9 @@ public function parseClass(\ReflectionClass $class)
$content = $this->getFileContent($filename, $class->getStartLine());
$namespace = str_replace('\\', '\\\\', $class->getNamespaceName());
$content = preg_replace('/^.*?(\bnamespace\s+' . $namespace . '\s*[;{].*)$/s', '\\1', $content);
$this->tokens = token_get_all('<?php ' . $content);
$this->numTokens = count($this->tokens);
$this->pointer = 0;
$tokenizer = new TokenParser('<?php ' . $content);

$statements = $this->parseUseStatements($class->getNamespaceName());
$statements = $tokenizer->parseUseStatements($class->getNamespaceName());

return $statements;
}
Expand Down
47 changes: 32 additions & 15 deletions lib/Doctrine/Common/Annotations/TokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,28 @@ class TokenParser
*/
protected $pointer = 0;

public function __construct($contents) {
$this->tokens = token_get_all($contents);
$this->numTokens = count($this->tokens);
$this->pointer = 0;
}

/**
* Gets the next non whitespace and non comment token.
*
* @param $skipDoxygen
* If TRUE then doxygen is considered a comment and skipped.
* If FALSE then only whitespace and normal comment is skipped.
*
* @return array The token if exists, null otherwise.
*/
protected function next($skipDoxygen = TRUE)
public function next($doxygenIsComment = TRUE)
{
for ($i = $this->pointer; $i < $this->numTokens; $i++) {
$this->pointer++;
if ($this->tokens[$i][0] === T_WHITESPACE ||
$this->tokens[$i][0] === T_COMMENT ||
($skipDoxygen && $this->tokens[$i][0] === T_DOC_COMMENT)) {
($doxygenIsComment && $this->tokens[$i][0] === T_DOC_COMMENT)) {

continue;
}
Expand All @@ -75,7 +85,7 @@ protected function next($skipDoxygen = TRUE)
*
* @return array A list with all found class names for a use statement.
*/
protected function parseUseStatement()
public function parseUseStatement()
{
$class = '';
$alias = '';
Expand Down Expand Up @@ -113,7 +123,7 @@ protected function parseUseStatement()
* @param string $namespaceName The namespace name of the reflected class.
* @return array A list with all found use statements.
*/
protected function parseUseStatements($namespaceName)
public function parseUseStatements($namespaceName)
{
$statements = array();
while (($token = $this->next())) {
Expand All @@ -134,22 +144,29 @@ protected function parseUseStatements($namespaceName)
}

/**
* Get the namespace name.
* Get the namespace.
*
* @return string The found namespace name.
* @return string The found namespace.
*/
protected function parseNamespace()
public function parseNamespace()
{
$namespace = '';
while (($token = $this->next())){
if ($token[0] === T_STRING || $token[0] === T_NS_SEPARATOR) {
$namespace .= $token[1];
} else {
break;
}
$name = '';
while (($token = $this->next()) && ($token[0] === T_STRING || $token[0] === T_NS_SEPARATOR)) {
$name .= $token[1];
}

return $namespace;
return $name;
}

/**
* Get the class name.
*
* @return string The foundclass name.
*/
public function parseClass() {
// Namespaces and class names are tokenized the same: T_STRINGs
// separated by T_NS_SEPARATOR so we can use one function to provide
// both.
return $this->parseNamespace();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace Doctrine\Common\Annotations;
namespace Doctrine\Common\Reflection;

use ReflectionClass;
use ReflectionException;

class Psr0ClassReflection extends ReflectionClass
class StaticReflectionClass extends ReflectionClass
{
public function __construct($psr0Parser)
{
Expand Down Expand Up @@ -33,11 +33,11 @@ public function getUseStatements()
}

public function getMethod($name) {
return $this->psr0Parser->getMethodReflection($name);
return $this->psr0Parser->getReflectionMethod($name);
}

public function getProperty($name) {
return $this->psr0Parser->getPropertyReflection($name);
return $this->psr0Parser->getReflectionProperty($name);
}

public static function export($argument, $return = false) { throw new ReflectionException('Method not implemented'); }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?php

namespace Doctrine\Common\Annotations;
namespace Doctrine\Common\Reflection;

use ReflectionMethod;
use ReflectionException;

class Psr0MethodReflection extends ReflectionMethod
class StaticReflectionMethod extends ReflectionMethod
{
/**
* The PSR-0 parser object.
*
* @var Psr0Parser
* @var StaticReflectionParser
*/
protected $psr0Parser;
protected $staticReflectionParser;

/**
* The name of the method.
Expand All @@ -21,34 +21,34 @@ class Psr0MethodReflection extends ReflectionMethod
*/
protected $methodName;

public function __construct($psr0Parser, $methodName)
public function __construct($StaticReflectionParser, $methodName)
{
$this->psr0Parser = $psr0Parser;
$this->staticReflectionParser = $StaticReflectionParser;
$this->methodName = $methodName;
}
public function getName()
{
return $this->methodName;
}
protected function getPsr0Parser()
protected function getStaticReflectionParser()
{
return $this->psr0Parser->getPsr0ParserForDeclaringClass('method', $this->methodName);
return $this->staticReflectionParser->getStaticReflectionParserForDeclaringClass('method', $this->methodName);
}
public function getDeclaringClass()
{
return $this->getPsr0Parser()->getClassReflection();
return $this->getStaticReflectionParser()->getReflectionClass();
}
public function getNamespaceName()
{
return $this->getPsr0Parser()->getNamespaceName();
return $this->getStaticReflectionParser()->getNamespaceName();
}
public function getDocComment()
{
return $this->getPsr0Parser()->getDoxygen('method', $this->methodName);
return $this->getStaticReflectionParser()->getDoxygen('method', $this->methodName);
}
public function getUseStatements()
{
return $this->getPsr0Parser()->getUseStatements();
return $this->getStaticReflectionParser()->getUseStatements();
}
public static function export($class, $name, $return = false) { throw new ReflectionException('Method not implemented'); }
public function getClosure($object) { throw new ReflectionException('Method not implemented'); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\Common\Annotations;
namespace Doctrine\Common\Reflection;

use ReflectionException;
use Doctrine\Common\Annotations\TokenParser;

/**
* Parses a file for namespaces/use/class declarations.
*
* @author Karoly Negyesi <karoly@negyesi.net>
*/
class Psr0Parser extends TokenParser
class StaticReflectionParser
{

/**
Expand Down Expand Up @@ -106,9 +107,9 @@ class Psr0Parser extends TokenParser
/**
* The parent PSR-0 Parser.
*
* @var \Doctrine\Common\Annotations\Psr0Parser
* @var \Doctrine\Common\Annotations\StaticReflectionParser
*/
protected $parentPsr0Parser;
protected $parentStaticReflectionParser;

/**
* Parses a class residing in a PSR-0 hierarchy.
Expand Down Expand Up @@ -145,17 +146,13 @@ protected function parse()
$contents = $matches[1];
}
}
$this->tokens = token_get_all($contents);
$this->numTokens = count($this->tokens);
$this->pointer = 0;
$annotations = array();
$statements = array();
$tokenParser = new TokenParser($contents);
$doxygen = '';
while ($token = $this->next(false)) {
while ($token = $tokenParser->next(false)) {
if (is_array($token)) {
switch ($token[0]) {
case T_USE:
$this->useStatements = array_merge($this->useStatements, $this->parseUseStatement());
$this->useStatements = array_merge($this->useStatements, $tokenParser->parseUseStatement());
break;
case T_DOC_COMMENT:
$doxygen = $token[1];
Expand All @@ -168,7 +165,7 @@ protected function parse()
case T_PRIVATE:
case T_PROTECTED:
case T_PUBLIC:
$token = $this->next();
$token = $tokenParser->next();
if ($token[0] === T_VARIABLE) {
$propertyName = substr($token[1], 1);
$this->doxygen['property'][$propertyName] = $doxygen;
Expand All @@ -183,16 +180,13 @@ protected function parse()
// The next string after function is the name, but
// there can be & before the function name so find the
// string.
while (($token = $this->next()) && $token[0] !== T_STRING);
while (($token = $tokenParser->next()) && $token[0] !== T_STRING);
$methodName = $token[1];
$this->doxygen['method'][$methodName] = $doxygen;
$doxygen = '';
break;
case T_EXTENDS:
$this->parentClassName = '';
while (($token = $this->next()) && ($token[0] === T_STRING || $token[0] === T_NS_SEPARATOR)) {
$this->parentClassName .= $token[1];
}
$this->parentClassName = $tokenParser->parseClass();
$nsPos = strpos($this->parentClassName, '\\');
$fullySpecified = false;
if ($nsPos === 0) {
Expand All @@ -219,8 +213,6 @@ protected function parse()
}
}
}
// Drop the tokens to save memory.
$this->tokens = array();
}

protected function findClassFile($includePaths, $namespace, $classShortName)
Expand All @@ -238,14 +230,14 @@ protected function findClassFile($includePaths, $namespace, $classShortName)
}
}

protected function getParentPsr0Parser()
protected function getParentStaticReflectionParser()
{
if (empty($this->parentPsr0Parser)) {
if (empty($this->parentStaticReflectionParser)) {
$class = get_class($this);
$this->parentPsr0Parser = new $class($this->parentClassName, $this->includePaths);
$this->parentStaticReflectionParser = new $class($this->parentClassName, $this->includePaths);
}

return $this->parentPsr0Parser;
return $this->parentStaticReflectionParser;
}

public function getClassName()
Expand All @@ -261,25 +253,25 @@ public function getNamespaceName()
/**
* Get the ReflectionClass equivalent for this file / class.
*/
public function getClassReflection()
public function getReflectionClass()
{
return new Psr0ClassReflection($this);
return new StaticReflectionClass($this);
}

/**
* Get the ReflectionMethod equivalent for the method of this file / class.
*/
public function getMethodReflection($methodName)
public function getReflectionMethod($methodName)
{
return new Psr0MethodReflection($this, $methodName);
return new StaticReflectionMethod($this, $methodName);
}

/**
* Get the ReflectionProperty equivalent for the method of this file / class.
*/
public function getPropertyReflection($propertyName)
public function getReflectionProperty($propertyName)
{
return new Psr0PropertyReflection($this, $propertyName);
return new StaticReflectionProperty($this, $propertyName);
}

/**
Expand Down Expand Up @@ -313,16 +305,16 @@ public function getDoxygen($type = 'class', $name = '')
* @param string $type property or method.
* @param string $name Name of the property or method.
*
* @return Psr0Parser A PSR-0 for the declaring class.
* @return StaticReflectionParser A static reflection parser for the declaring class.
*/
public function getPsr0ParserForDeclaringClass($type, $name)
public function getStaticReflectionParserForDeclaringClass($type, $name)
{
$this->parse();
if (isset($this->doxygen[$type][$name])) {
return $this;
}
if (!empty($this->parentClassName)) {
return $this->getParentPsr0Parser()->getPsr0ParserForDeclaringClass($type, $name);
return $this->getParentStaticReflectionParser()->getStaticReflectionParserForDeclaringClass($type, $name);
}
throw new ReflectionException('Invalid ' . $type . ' "' . $name . '"');
}
Expand Down
Loading

0 comments on commit 9cf5ebd

Please sign in to comment.