diff --git a/lib/Doctrine/Common/Annotations/AnnotationReader.php b/lib/Doctrine/Common/Annotations/AnnotationReader.php index bf9b488f2..f2b53b5b5 100644 --- a/lib/Doctrine/Common/Annotations/AnnotationReader.php +++ b/lib/Doctrine/Common/Annotations/AnnotationReader.php @@ -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. * diff --git a/lib/Doctrine/Common/Annotations/Lexer.php b/lib/Doctrine/Common/Annotations/Lexer.php index d624a1bd8..cb32f0cc2 100644 --- a/lib/Doctrine/Common/Annotations/Lexer.php +++ b/lib/Doctrine/Common/Annotations/Lexer.php @@ -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]+)?', '"(?:[^"]|"")*"' ); diff --git a/lib/Doctrine/Common/Annotations/Parser.php b/lib/Doctrine/Common/Annotations/Parser.php index b54e49ab8..4b91a8ab9 100644 --- a/lib/Doctrine/Common/Annotations/Parser.php +++ b/lib/Doctrine/Common/Annotations/Parser.php @@ -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); } diff --git a/tests/Doctrine/Tests/Common/Annotations/ParserTest.php b/tests/Doctrine/Tests/Common/Annotations/ParserTest.php index 270517d55..c9709ba07 100644 --- a/tests/Doctrine/Tests/Common/Annotations/ParserTest.php +++ b/tests/Doctrine/Tests/Common/Annotations/ParserTest.php @@ -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 = <<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() {