Skip to content

Instantly share code, notes, and snippets.

@fgm
Created February 16, 2014 12:04
Show Gist options
  • Save fgm/9033196 to your computer and use it in GitHub Desktop.
Save fgm/9033196 to your computer and use it in GitHub Desktop.
Demonstrates a Doctrine annotations loader not needing explicit paths.
<?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