Skip to content

Commit

Permalink
Reverted annotation autoloading functionality, reintroducing a config…
Browse files Browse the repository at this point in the history
…uration switch, defaulting to false. Too much trouble in paradise otherwise.
  • Loading branch information
romanb committed Jun 16, 2010
1 parent f34d581 commit 30eac01
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
25 changes: 25 additions & 0 deletions lib/Doctrine/Common/Annotations/AnnotationReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,31 @@ public function setAnnotationNamespaceAlias($namespace, $alias)
$this->parser->setAnnotationNamespaceAlias($namespace, $alias);
}

/**
* Sets a flag whether to try to autoload annotation classes, as well as to distinguish
* between what is an annotation and what not by triggering autoloading.
*
* NOTE: Autoloading of annotation classes is inefficient and requires silently failing
* autoloaders. In particular, setting this option to TRUE renders this AnnotationReader
* incompatible with a {@link ClassLoader}.
* @param boolean $bool Boolean flag.
*/
public function setAutoloadAnnotations($bool)
{
$this->parser->setAutoloadAnnotations($bool);
}

/**
* Gets a flag whether to try to autoload annotation classes.
*
* @see setAutoloadAnnotations
* @return boolean
*/
public function getAutoloadAnnotations()
{
return $this->parser->getAutoloadAnnotations();
}

/**
* Gets the annotations applied to a class.
*
Expand Down
34 changes: 32 additions & 2 deletions lib/Doctrine/Common/Annotations/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ class Parser
*/
private $context = '';

/**
* @var boolean Whether to try to autoload annotations that are not yet defined.
*/
private $autoloadAnnotations = false;

/**
* Constructs a new AnnotationParser.
*/
Expand All @@ -83,6 +88,31 @@ public function __construct()
$this->lexer = new Lexer;
}

/**
* Sets a flag whether to try to autoload annotation classes, as well as to distinguish
* between what is an annotation and what not by triggering autoloading.
*
* NOTE: Autoloading of annotation classes is inefficient and requires silently failing
* autoloaders. In particular, setting this option to TRUE renders the Parser
* incompatible with a {@link ClassLoader}.
* @param boolean $bool Boolean flag.
*/
public function setAutoloadAnnotations($bool)
{
$this->autoloadAnnotations = $bool;
}

/**
* Gets a flag whether to try to autoload annotation classes.
*
* @see setAutoloadAnnotations
* @return boolean
*/
public function getAutoloadAnnotations()
{
return $this->autoloadAnnotations;
}

/**
* Sets the default namespace that is assumed for an annotation that does not
* define a namespace prefix.
Expand Down Expand Up @@ -244,10 +274,10 @@ public function Annotation()
}

// Is it really an annotation?
// If the lookahead is "(" it surely is, otherwise classExists decides.
// If the lookahead is "(" it surely is, otherwise class_exists decides.
if (
($this->lexer->lookahead == null || ! $this->lexer->isNextToken(Lexer::T_OPEN_PARENTHESIS)) &&
! ClassLoader::classExists($name)
! class_exists($name, $this->autoloadAnnotations)
) {
$this->lexer->skipUntil(Lexer::T_AT);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ public function testAnnotations()
{
$reader = new AnnotationReader(new \Doctrine\Common\Cache\ArrayCache);
$reader->setDefaultAnnotationNamespace('Doctrine\Tests\Common\Annotations\\');

$this->assertFalse($reader->getAutoloadAnnotations());
$reader->setAutoloadAnnotations(true);
$this->assertTrue($reader->getAutoloadAnnotations());
$reader->setAutoloadAnnotations(false);
$this->assertFalse($reader->getAutoloadAnnotations());

$class = new \ReflectionClass('Doctrine\Tests\Common\Annotations\DummyClass');
$classAnnots = $reader->getClassAnnotations($class);
Expand Down
6 changes: 5 additions & 1 deletion tests/Doctrine/Tests/Common/Annotations/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public function testBasicAnnotations()
{
$parser = $this->createTestParser();

$this->assertFalse($parser->getAutoloadAnnotations());

// Marker annotation
$result = $parser->parse("@Name");
$annot = $result['Doctrine\Tests\Common\Annotations\Name'];
Expand Down Expand Up @@ -77,7 +79,9 @@ public function testNamespacedAnnotations()
/**
* Some nifty class.
*
* @author Mr.X
* @package foo
* @subpackage bar
* @author Mr.X <mr@x.com>
* @Doctrine\Tests\Common\Annotations\Name(foo="bar")
* @ignore
*/
Expand Down

0 comments on commit 30eac01

Please sign in to comment.