Created
February 16, 2014 12:04
-
-
Save fgm/9033196 to your computer and use it in GitHub Desktop.
Demonstrates a Doctrine annotations loader not needing explicit paths.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Doctrine\Common\Annotations\AnnotationReader; | |
use Doctrine\Common\Annotations\AnnotationRegistry; | |
$src = __DIR__ . '/../src'; | |
$vendors = __DIR__ . '/../vendor'; | |
require_once "$vendors/autoload.php"; | |
class Loader { | |
/** | |
* @var array | |
*/ | |
protected $namespaces; | |
public function __construct(array $namespaces) { | |
$this->namespaces = $namespaces; | |
} | |
/** | |
* @param string $name | |
* | |
* @return bool | |
*/ | |
public function __invoke($name) { | |
foreach ($this->namespaces as $namespace) { | |
if (strpos($name, $namespace) === 0) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} | |
AnnotationRegistry::registerFile("$vendors/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php"); | |
AnnotationRegistry::registerLoader(new Loader(array( | |
'Symfony\Component\Validator\Constraints', | |
'MyProject\Annotations', | |
))); | |
$reader = new AnnotationReader(); | |
AnnotationReader::addGlobalIgnoredName('dummy'); | |
$rc = new ReflectionClass('MyProject\Entities\User'); | |
print_r($reader->getClassAnnotations($rc)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment