Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace AppBundle\Listener\Aop;
- use AppBundle\Utils\ClassUtils;
- use Doctrine\Common\EventSubscriber;
- use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
- use Doctrine\ORM\Events;
- use Doctrine\ORM\Mapping\ClassMetadata;
- use Go\Core\AspectContainer;
- class MetadataLoadInterceptor implements EventSubscriber
- {
- /**
- * {@inheritdoc}
- */
- public function getSubscribedEvents()
- {
- return [
- Events::loadClassMetadata
- ];
- }
- public function loadClassMetadata(LoadClassMetadataEventArgs $args)
- {
- /**
- * @var ClassMetadata $metadata
- */
- $metadata = $args->getClassMetadata();
- // First, I will check if entity class is AOP proxy class (instead of strpos, string->endsWith() should be used, TODO)
- if (false !== strpos($metadata->name, AspectContainer::AOP_PROXIED_SUFFIX)) {
- // I will fix entity data first, it is a mapped superclass, it is not embedded, it does not have table, nor repo
- $metadata->isMappedSuperclass = true;
- $metadata->isEmbeddedClass = false;
- $metadata->table = [];
- $metadata->customRepositoryClassName = null;
- // Last thing I have to do is to remove all mappings got from traits,
- // All of those will be mapped again in subclass (a concrete class)
- // That is how I can support Gedmo extensions, like TimestampableEntity or BlameableEntity, etc...
- // This class utils is taken from https://github.com/RunOpenCode/traitor-bundle/blob/master/src/RunOpenCode/Bundle/Traitor/Utils/ClassUtils.php
- $traits = ClassUtils::getTraits($metadata->name);
- foreach ($traits as $trait) {
- $trait = new \ReflectionClass($trait);
- /**
- * @var \ReflectionProperty $property
- */
- foreach ($trait->getProperties() as $property) {
- $name = $property->getName();
- if (isset($metadata->fieldMappings[$name])) {
- $mapping = $metadata->fieldMappings[$name];
- // Each property from trait, found in mapping will be removed, it will be
- // remapped in concrete class again
- unset(
- $metadata->fieldMappings[$name],
- $metadata->fieldNames[$mapping['columnName']],
- $metadata->columnNames[$name]
- );
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment