Search code examples
phpsymfonysymfony-3.3

Use custom annotations in Symfony User Provider


I'm trying to make a custom User Provider which uses a custom Doctrine-like bundle. This bundles uses entities, pretty much like Doctrine does :

/**
 * @EntityMeta(table="MY_TABLE")
*/
class MyTable extends AbstractEntity
{
/**
 * @var int
 * @EntityColumnMeta(column="Code", isKey=true)
 */
protected $code;

/**
 * @var int
 * @EntityColumnMeta(column="Name")
 */
protected $name;

Those annotations work well when I use the doctrine-like manager provided by my bundle. This code works well :

public function indexAction(DoctrineLikeManager $manager)
{
    $lines = $manager->getRepository('MyTable')->findBy(array(
        'email' => '[email protected]'
    ));
    // do something with these
}

So I know annotations work. But when I use the same code, with the same entity, in the User Provider Class, I get the following error :

[Semantical Error] The annotation "@NameSpace\DoctrineLikeBundle\EntityColumnMeta" in property AppBundle\MyTable::$code does not exist, or could not be auto-loaded.

The UserProvider :

class HanaUserProvider implements UserProviderInterface
{
     private $manager;

     public function __construct(DoctrineLikeManager $manager)
     {
         $this->manager = $manager;
     }

     public function loadUserByUsername($username)
     {
         // this is where it fails :(
         $lines = $this->manager->getRepository('MyTable')->findBy(array(
    'email' => '[email protected]'
         ));
         // return user or throw UsernameNotFoundException
     }
}

Is it possible to use custom annotations in that context ? Maybe I should do something in particular so custom annotations can be successfully loaded ?

Thanks in advance !


Solution

  • Ok, I found a solution, which might not be the best but works.

    The thing is that annotations don't seem to use classic autoloading, as explained here.

    I had to register a loader in my User Provider :

    public function loadUserByUsername($username)
     {
         AnnotationRegistry::registerLoader('class_exists'); // THIS LINE HERE
         $lines = $this->manager->getRepository('MyTable')->findBy(array(
    'email' => '[email protected]'
         ));
         // return user or throw UsernameNotFoundException
     }
    

    However, the problem is that this method is deprecated and will be removed in doctrine/annotations 2.0.