AbstractDoctrineDecorator.php 1.32 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
<?php

/*
 * © 2016 IRSTEA
 * Guillaume Perréal <guillaume.perreal@irstea.fr>
 * Tous droits réservés.
 */

namespace Irstea\PlantUmlBundle\Doctrine;

11
use Doctrine\ORM\EntityManagerInterface;
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadataFactory;
use Irstea\PlantUmlBundle\Model\ClassVisitorInterface;
use Irstea\PlantUmlBundle\Model\DecoratorInterface;
use Irstea\PlantUmlBundle\Model\NodeInterface;
use ReflectionClass;

/**
 * Description of AbstractDoctrineDecorator
 *
 * @author Guillaume Perréal <guillaume.perreal@irstea.fr>
 */
abstract class AbstractDoctrineDecorator implements DecoratorInterface
{
    /**
     * @var ClassMetadataFactory
     */
    protected $metadataFactory;

31
    public function __construct(EntityManagerInterface $manager)
32
    {
33
        $this->metadataFactory = $manager->getMetadataFactory();
34
35
    }

36
37
38
39
40
41
    /**
     * @param callable $callback
     * @param ReflectionClass $class
     * @return mixed
     */
    protected function withMetadata($callback, ReflectionClass $class = null)
42
    {
43
44
45
46
        if (!$class) {
            return null;
        }

47
        $className = $class->getName();
48
49
        if (!$this->metadataFactory->hasMetadataFor($className)) {
            return null;
50
        }
51
52
    
        return $callback($this->metadataFactory->getMetadataFor($className));
53
    }
54
}