-
-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement workaround for Doctrine entities with metadata listener #327
- Loading branch information
1 parent
5d96ceb
commit 6e2a0fe
Showing
4 changed files
with
253 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
<?php | ||
/* | ||
* Go! AOP framework | ||
* | ||
* @copyright Copyright 2012, Lisachenko Alexander <[email protected]> | ||
* | ||
* This source file is subject to the license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
namespace Go\Bridge\Doctrine; | ||
|
||
use Doctrine\Common\EventSubscriber; | ||
use Doctrine\ORM\Event\LoadClassMetadataEventArgs; | ||
use Doctrine\ORM\Events; | ||
use Doctrine\ORM\Mapping\ClassMetadata; | ||
use Go\Core\AspectContainer; | ||
|
||
/** | ||
* Class MetadataLoadInterceptor | ||
* | ||
* Support for weaving Doctrine entities. | ||
*/ | ||
final class MetadataLoadInterceptor implements EventSubscriber | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getSubscribedEvents() | ||
{ | ||
return [ | ||
Events::loadClassMetadata | ||
]; | ||
} | ||
|
||
/** | ||
* Handles \Doctrine\ORM\Events::loadClassMetadata event by modifying metadata of Go! AOP proxied classes. | ||
* | ||
* This method intercepts loaded metadata of Doctrine's entities which are weaved by Go! AOP, | ||
* and denotes them as mapped superclass. If weaved entities uses mappings from traits | ||
* (such as Timestampable, Blameable, etc... from https://github.com/Atlantic18/DoctrineExtensions), | ||
* it will remove all mappings from proxied class for fields inherited from traits in order to prevent | ||
* collision with concrete subclass of weaved entity. Fields from trait will be present in concrete subclass | ||
* of weaved entitites. | ||
* | ||
* @see http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#mapped-superclasses | ||
* @see https://github.com/Atlantic18/DoctrineExtensions | ||
* | ||
* @param LoadClassMetadataEventArgs $args | ||
*/ | ||
public function loadClassMetadata(LoadClassMetadataEventArgs $args) | ||
{ | ||
/** | ||
* @var ClassMetadata $metadata | ||
*/ | ||
$metadata = $args->getClassMetadata(); | ||
|
||
if (1 === preg_match(sprintf('/.+(%s)$/', AspectContainer::AOP_PROXIED_SUFFIX), $metadata->name)) { | ||
$metadata->isMappedSuperclass = true; | ||
$metadata->isEmbeddedClass = false; | ||
$metadata->table = []; | ||
$metadata->customRepositoryClassName = null; | ||
|
||
$this->removeMappingsFromTraits($metadata); | ||
} | ||
} | ||
|
||
/** | ||
* Remove fields in Go! AOP proxied class metadata that are inherited | ||
* from traits. | ||
* | ||
* @param ClassMetadata $metadata | ||
*/ | ||
private function removeMappingsFromTraits(ClassMetadata $metadata) | ||
{ | ||
$traits = $this->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]; | ||
|
||
unset( | ||
$metadata->fieldMappings[$name], | ||
$metadata->fieldNames[$mapping['columnName']], | ||
$metadata->columnNames[$name] | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Get ALL traits used by one class. | ||
* | ||
* This method is copied from https://github.com/RunOpenCode/traitor-bundle/blob/master/src/RunOpenCode/Bundle/Traitor/Utils/ClassUtils.php | ||
* | ||
* @param object|string $objectOrClass Instance of class or FQCN | ||
* @param bool $autoload Weather to autoload class. | ||
* | ||
* @throws \InvalidArgumentException | ||
* @throws \RuntimeException | ||
* | ||
* @return array Used traits. | ||
*/ | ||
private function getTraits($objectOrClass, $autoload = true) | ||
{ | ||
if (is_object($objectOrClass)) { | ||
$objectOrClass = get_class($objectOrClass); | ||
} | ||
|
||
if (!is_string($objectOrClass)) { | ||
throw new \InvalidArgumentException(sprintf('Full qualified class name expected, got: "%s".', gettype($objectOrClass))); | ||
} | ||
|
||
if (!class_exists($objectOrClass)) { | ||
throw new \RuntimeException(sprintf('Class "%s" does not exists or it can not be autoloaded.', $objectOrClass)); | ||
} | ||
|
||
$traits = []; | ||
// Get traits of all parent classes | ||
do { | ||
$traits = array_merge(class_uses($objectOrClass, $autoload), $traits); | ||
} while ($objectOrClass = get_parent_class($objectOrClass)); | ||
|
||
$traitsToSearch = $traits; | ||
|
||
while (count($traitsToSearch) > 0) { | ||
$newTraits = class_uses(array_pop($traitsToSearch), $autoload); | ||
$traits = array_merge($newTraits, $traits); | ||
$traitsToSearch = array_merge($newTraits, $traitsToSearch); | ||
} | ||
|
||
foreach ($traits as $trait => $same) { | ||
$traits = array_merge(class_uses($trait, $autoload), $traits); | ||
} | ||
|
||
return array_unique(array_map(function ($fqcn) { | ||
return ltrim($fqcn, '\\'); | ||
}, $traits)); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
tests/Go/Aop/Bridge/Doctrine/MetadataLoadInterceptorTest.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
/* | ||
* Go! AOP framework | ||
* | ||
* @copyright Copyright 2014, Lisachenko Alexander <[email protected]> | ||
* | ||
* This source file is subject to the license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
namespace Go\Aop\Bridge\Doctrine; | ||
|
||
use Doctrine\ORM\EntityManager; | ||
use Doctrine\ORM\Event\LoadClassMetadataEventArgs; | ||
use Doctrine\ORM\Mapping\ClassMetadata; | ||
use Go\Bridge\Doctrine\MetadataLoadInterceptor; | ||
use Go\Core\AspectContainer; | ||
|
||
class MetadataLoadInterceptorTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testItWillNotModifyClassMetadataForNonProxiedClasses() | ||
{ | ||
$metadatas = [ | ||
new ClassMetadata('\\Some\\Class\\Name'), | ||
new ClassMetadata(sprintf('%s\\Some\\Class\\Name', AspectContainer::AOP_PROXIED_SUFFIX)), | ||
new ClassMetadata(AspectContainer::AOP_PROXIED_SUFFIX), | ||
]; | ||
|
||
$metadataInterceptor = new MetadataLoadInterceptor(); | ||
$entityManager = $this->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock(); | ||
|
||
/** | ||
* @var ClassMetadata $metadata | ||
*/ | ||
foreach ($metadatas as $metadata) { | ||
$metadata->isMappedSuperclass = false; | ||
$metadataInterceptor->loadClassMetadata(new LoadClassMetadataEventArgs($metadata, $entityManager)); | ||
|
||
$this->assertFalse($metadata->isMappedSuperclass); | ||
} | ||
} | ||
|
||
public function testItWillModifyClassMetadataForNonProxiedClasses() | ||
{ | ||
$metadata = new ClassMetadata(Entity__AopProxied::class); | ||
$metadataInterceptor = new MetadataLoadInterceptor(); | ||
$entityManager = $this->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock(); | ||
|
||
$metadata->isMappedSuperclass = false; | ||
$metadata->isEmbeddedClass = true; | ||
$metadata->table = ['table_name']; | ||
$metadata->customRepositoryClassName = 'CustomRepositoryClass'; | ||
|
||
$metadata->fieldMappings['mappedField'] = [ | ||
'columnName' => 'mapped_field', | ||
'fieldName' => 'mappedField' | ||
]; | ||
$metadata->fieldNames['mapped_field'] = 'mappedField'; | ||
$metadata->columnNames['mappedField'] = 'mapped_field'; | ||
|
||
$metadataInterceptor->loadClassMetadata(new LoadClassMetadataEventArgs($metadata, $entityManager)); | ||
|
||
$this->assertTrue($metadata->isMappedSuperclass); | ||
$this->assertFalse($metadata->isEmbeddedClass); | ||
$this->assertEquals(0, count($metadata->table)); | ||
$this->assertNull($metadata->customRepositoryClassName); | ||
|
||
$this->assertFalse(isset($metadata->fieldMappings['mappedField'])); | ||
$this->assertFalse(isset($metadata->fieldNames['mapped_field'])); | ||
$this->assertFalse(isset($metadata->columnNames['mappedField'])); | ||
} | ||
} | ||
|
||
trait SimpleTrait | ||
{ | ||
private $mappedField; | ||
} | ||
|
||
class Entity__AopProxied | ||
{ | ||
use SimpleTrait; | ||
} |