Created
March 17, 2016 18:05
-
-
Save fritz-gerneth/10a706f6250b619f9440 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Created by PhpStorm. | |
* User: Fritz Gerneth | |
* Date: 17.03.2016 | |
* Time: 17:38 | |
*/ | |
namespace Application; | |
use Doctrine\Common\Annotations\AnnotationReader; | |
use Doctrine\Common\EventSubscriber; | |
use Doctrine\ORM\Event\LoadClassMetadataEventArgs; | |
use Doctrine\ORM\Events; | |
class EntityEventListener implements EventSubscriber | |
{ | |
/** | |
* @var array | |
*/ | |
protected $mapping; | |
public function __construct(array $mapping) | |
{ | |
$this->mapping = $mapping; | |
} | |
public function loadClassMetadata(LoadClassMetadataEventArgs $event) | |
{ | |
$metadata = $event->getClassMetadata(); | |
if (!array_key_exists($metadata->getName(), $this->mapping)) { | |
return; | |
} | |
$class = $metadata->getReflectionClass(); | |
if ($class === null) { | |
$class = new \ReflectionClass($metadata->getName()); | |
} | |
$discriminatorMap = $this->mapping[$metadata->getName()]; | |
$reader = new AnnotationReader(); | |
if (null !== $discriminatorMapAnnotation = $reader->getClassAnnotation($class, 'Doctrine\ORM\Mapping\DiscriminatorMap')) { | |
$discriminatorMap = array_merge( | |
$discriminatorMapAnnotation->value, | |
$discriminatorMap | |
); | |
} | |
$metadata->setDiscriminatorMap($discriminatorMap); | |
} | |
/** | |
* Returns an array of events this subscriber wants to listen to. | |
* | |
* @return array | |
*/ | |
public function getSubscribedEvents() | |
{ | |
return array(Events::loadClassMetadata); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment