Skip to content

Instantly share code, notes, and snippets.

@TiSiE
Last active November 3, 2016 09:39
Show Gist options
  • Save TiSiE/7c4f1a028bc3d03f0ef997d064399198 to your computer and use it in GitHub Desktop.
Save TiSiE/7c4f1a028bc3d03f0ef997d064399198 to your computer and use it in GitHub Desktop.
Attached entities skeletons
<?php
class AttachedEntitiesCollection extends \Doctrine\ArrayCollection
{
private $repositories;
private $referencePrototype;
public function __construct(RepositoryService $repositories, AttachedEntityReference $reference = null)
{
$this->repositories = $repositories;
if (null === $reference) {
$reference = new AttachedEntityReference;
}
$this->referencePrototype = $reference;
}
public function set($key, $value)
{
if (null === $key) {
$key = get_class($value);
}
$ref = $this->getReference();
$repository = get_class($value);
$entityId = $value->getId();
if (!$entityId) {
$this->repositories->get($repository)->store($value);
$entityId = $value->getId();
}
$ref->setRepository(get_class($value))
->setEntityId($value->getId())
;
return parent::set($key, $ref);
}
public function get($key)
{
$ref = parent::get($key);
if (!$ref) {
return null;
}
return $this->repositories
->get($ref->getRepository())
->find($ref->getEntityId())
;
}
public function remove($key)
{
$ref = parent::get($key);
if (!$ref) { return $this; }
$entity = $this->get($key);
$this->repositories->get($ref->getRepository())->remove($entity);
return parent::remove($key);
}
private function getReference()
{
return clone $this->referencePrototype;
}
}
<?php
/**
* @ODM/EmbeddedDocument
*/
class AttachedEntityReference
{
/**
* @ODM\String
*/
private $repository;
/**
* @ODM\String
*/
private $entityId;
/* Setter / Getter omitted */
}
<?php
interface ExpandableEntityInterface
{
public function setAttachedEntitiesCollection(AttachedEntitiesCollection $collection);
public function attach($entity, $key=null);
public function getAttached($key);
public function hasAttached($key);
public function detach($key);
}
<?php
trait ExpandableEntityTrait
{
/**
* @ODM\EmbedMany(collectionClass="AttachedEntitiesCollection", targetDocument="AttachedEntityReference")
*/
private $attachedEntities
public function setAttachedEntitiesCollection(AttachedEntitiesCollection $collection)
{
if (null !== $this->attachedEntities) { /* Exception */ }
$this->attachedEntities = $collection;
return $this;
}
public function attach($entity, $key=null)
{
$this->attachedEntities->set($key, $entity);
return $this;
}
public function getAttached($key)
{
return $this->attachedEntities->get($key);
}
public function hasAttached($key)
{
return $this->attachedEntities->containsKey($key);
}
public function detach($key)
{
$this->attachedEntities->remove($key);
return $this;
}
}
<?php
/**
* modified: Core/Repository/DoctrineMongoODM/Event/RepositoryEventsSubscriber.php
*/
class RepositoryEventSubscriber
{
/* ... */
public function postRepositoryConstruct($eventArgs)
{
$repo = $eventArgs->getRepository();
if ($repo instanceof RepositoryInterface) {
$documentName = $repo->getDocumentName();
$entity = new $documentName();
/* INJECT the AttachedEntityCollection here
*/
if ($entity instanceof ExpandableEntityInterface) {
$repositories = $this->services->get('repositories');
$attachedEntityCollection = new AttachedEntityCollection($repositories);
$entity->setAttachedEntitiesCollection($attachedEntitiesCollection);
}
/* end modification */
$repo->setEntityPrototype($entity);
$repo->init($this->services);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment