-
-
Notifications
You must be signed in to change notification settings - Fork 163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added workaround for #327 #328
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected 1 newline after opening brace; 2 found |
||
$trait = new \ReflectionClass($trait); | ||
|
||
/** | ||
* @var \ReflectionProperty $property | ||
*/ | ||
foreach ($trait->getProperties() as $property) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected 1 newline after opening brace; 2 found |
||
$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)); | ||
} | ||
} |
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected 1 newline after opening brace; 2 found |
||
$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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Each class must be in a file by itself There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can not fix this, except to pull out to separate files - however, this class is used only for this test suite, so usual practice is to place it inline within test suite. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can ignore this, ok |
||
{ | ||
private $mappedField; | ||
} | ||
|
||
class Entity__AopProxied | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
use SimpleTrait; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected 1 blank line at end of file; 2 found |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expected 1 newline after opening brace; 2 found