1
0
mirror of synced 2024-12-12 22:36:02 +03:00

[2.0][DDC-366] Fixed. Fully qualified names were borked in the lexer as well. Tests didnt catch it because of unfortunate namespace constellations. Fixed now.

This commit is contained in:
romanb 2010-02-24 14:33:12 +00:00
parent b274a69ec2
commit 7badced187
4 changed files with 45 additions and 6 deletions

View File

@ -84,6 +84,17 @@ class AnnotationReader
$this->_parser->setDefaultAnnotationNamespace($defaultNamespace);
}
/**
* Sets an alias for an annotation namespace.
*
* @param $namespace
* @param $alias
*/
public function setAnnotationNamespaceAlias($namespace, $alias)
{
$this->_parser->setAnnotationNamespaceAlias($namespace, $alias);
}
/**
* Gets the annotations applied to a class.
*

View File

@ -57,7 +57,7 @@ class Lexer extends \Doctrine\Common\Lexer
protected function getCatchablePatterns()
{
return array(
'[a-z_][a-z0-9_\\\]*',
'[a-z_][a-z0-9_:]*',
'(?:[0-9]+(?:[\.][0-9]+)*)(?:e[+-]?[0-9]+)?',
'"(?:[^"]|"")*"'
);

View File

@ -213,10 +213,12 @@ class Parser
/**
* Annotation ::= "@" AnnotationName ["(" [Values] ")"]
* AnnotationName ::= QualifiedName | SimpleName
* AnnotationName ::= QualifiedName | SimpleName | AliasedName
* QualifiedName ::= NameSpacePart "\" {NameSpacePart "\"}* SimpleName
* AliasedName ::= Alias ":" SimpleName
* NameSpacePart ::= identifier
* SimpleName ::= identifier
* Alias ::= identifier
*
* @return mixed False if it is not a valid Annotation; instance of Annotation subclass otherwise.
*/
@ -228,7 +230,7 @@ class Parser
$this->match(Lexer::T_AT);
$this->match(Lexer::T_IDENTIFIER);
$nameParts[] = $this->_lexer->token['value'];
while ($this->_lexer->isNextToken(Lexer::T_NAMESPACE_SEPARATOR)) {
$this->match(Lexer::T_NAMESPACE_SEPARATOR);
$this->match(Lexer::T_IDENTIFIER);
@ -237,9 +239,12 @@ class Parser
// Effectively pick the name of class (append default NS if none, grab from NS alias, etc)
if (count($nameParts) == 1) {
$name = $this->_defaultAnnotationNamespace . $nameParts[0];
} else if (count($nameParts) == 2 && isset($this->_namespaceAliases[$nameParts[0]])) {
$name = $this->_namespaceAliases[$nameParts[0]] . $nameParts[1];
if (strpos($nameParts[0], ':')) {
list ($alias, $simpleName) = explode(':', $nameParts[0]);
$name = $this->_namespaceAliases[$alias] . $simpleName;
} else {
$name = $this->_defaultAnnotationNamespace . $nameParts[0];
}
} else {
$name = implode('\\', $nameParts);
}

View File

@ -100,6 +100,29 @@ DOCBLOCK;
$parser = $this->createTestParser();
$parser->parse("@Name(foo='bar')");
}
/**
* @group parse
*/
public function testAnnotationNamespaceAlias()
{
$parser = new Parser;
$parser->setAnnotationNamespaceAlias('Doctrine\Tests\Common\Annotations\\', 'alias');
$docblock = <<<DOCBLOCK
/**
* Some nifty class.
*
* @author Mr.X
* @alias:Name(foo="stuff")
*/
DOCBLOCK;
$result = $parser->parse($docblock);
$this->assertEquals(1, count($result));
$annot = $result['Doctrine\Tests\Common\Annotations\Name'];
$this->assertTrue($annot instanceof Name);
$this->assertEquals("stuff", $annot->foo);
}
public function createTestParser()
{