Created
April 17, 2019 10:41
-
-
Save r8or0pz/e8d49c116b532c1da70e620cd9860c16 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| namespace BackendLib\EventListener; | |
| use Doctrine\ORM\Event\LifecycleEventArgs; | |
| use Doctrine\ORM\Event\PreUpdateEventArgs; | |
| use BackendLib\Entity\iEmbeddedEntityRelationFixable; | |
| use BackendLib\Annotation\EmbeddedEntityRelationFixer; | |
| use Doctrine\Common\Annotations\Reader; | |
| use Doctrine\Common\Inflector\Inflector; | |
| class EmbeddedEntityRelationFixerListener | |
| { | |
| private $annotationReader; | |
| private $em; | |
| private $entity; | |
| private $parentMethodName; | |
| private $ann; | |
| public function __construct(Reader $annotationReader) | |
| { | |
| $this->annotationReader = $annotationReader; | |
| } | |
| public function postLoad(LifecycleEventArgs $args) | |
| { | |
| $this->main( | |
| $args, | |
| function () { | |
| $id = $this->entity->{$this->parentMethodName}()->{$this->ann->methodName}(); | |
| if ($id && is_integer($id)) { | |
| $result = $this->em->find( | |
| $this->ann->relatedClass, | |
| $id | |
| ); | |
| if ($result) { | |
| $this->entity->{$this->parentMethodName}() | |
| ->{$this->getSetterName($this->ann->methodName)}($result); | |
| } | |
| } | |
| } | |
| ); | |
| } | |
| public function prePersist(LifecycleEventArgs $args) | |
| { | |
| $this->main( | |
| $args, | |
| function () { | |
| $result = $this->entity->{$this->parentMethodName}()->{$this->ann->methodName}(); | |
| if ($result && is_object($result)) { | |
| $this->entity->{$this->parentMethodName}() | |
| ->{$this->getSetterName($this->ann->methodName)}($result->getId()); | |
| } | |
| } | |
| ); | |
| } | |
| public function main(LifecycleEventArgs $args, callable $callback) | |
| { | |
| if ($args instanceof LifecycleEventArgs || $args instanceof PreUpdateEventArgs) { | |
| $entity = $args->getEntity(); | |
| if (!$entity instanceof iEmbeddedEntityRelationFixable) { | |
| return; | |
| } | |
| $this->em = $args->getObjectManager(); | |
| $this->entity = $args->getEntity(); | |
| $metadata = $this->em->getClassMetadata(get_class($this->entity)); | |
| if (!empty($metadata->embeddedClasses)) { | |
| foreach ($metadata->embeddedClasses as $parent => $embeddable) { | |
| $methods = get_class_methods($embeddable['class']); | |
| if (!empty($methods)) { | |
| $this->parentMethodName = $this->getParentMethodName($parent); | |
| foreach ($methods as $method) { | |
| $this->ann = $this->annotationReader->getMethodAnnotation( | |
| new \ReflectionMethod($embeddable['class'], $method), | |
| EmbeddedEntityRelationFixer::class | |
| ); | |
| if ($this->ann) { | |
| $callback(); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| public function preUpdate(PreUpdateEventArgs $args) | |
| { | |
| $this->prePersist($args); | |
| } | |
| protected function getParentMethodName(string $methodName) | |
| { | |
| $parentMethodName = 'get'.ucfirst(Inflector::camelize($methodName)); | |
| return $parentMethodName; | |
| } | |
| protected function getSetterName(string $methodName) | |
| { | |
| $methodName[0] = 's'; | |
| return $methodName; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment