Actualización

This commit is contained in:
Xes
2025-04-10 12:24:57 +02:00
parent 8969cc929d
commit 45420b6f0d
39760 changed files with 4303286 additions and 0 deletions

View File

@@ -0,0 +1,100 @@
<?php
namespace Gedmo\Tool\Wrapper;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityManagerInterface;
use Gedmo\Tool\WrapperInterface;
use Gedmo\Exception\UnsupportedObjectManagerException;
/**
* Wraps entity or proxy for more convenient
* manipulation
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
abstract class AbstractWrapper implements WrapperInterface
{
/**
* Object metadata
*
* @var object
*/
protected $meta;
/**
* Wrapped object
*
* @var object
*/
protected $object;
/**
* Object manager instance
*
* @var \Doctrine\Common\Persistence\ObjectManager
*/
protected $om;
/**
* List of wrapped object references
*
* @var array
*/
private static $wrappedObjectReferences;
/**
* Wrap object factory method
*
* @param object $object
* @param ObjectManager $om
*
* @throws \Gedmo\Exception\UnsupportedObjectManagerException
*
* @return \Gedmo\Tool\WrapperInterface
*/
public static function wrap($object, ObjectManager $om)
{
if ($om instanceof EntityManagerInterface) {
return new EntityWrapper($object, $om);
} elseif ($om instanceof DocumentManager) {
return new MongoDocumentWrapper($object, $om);
}
throw new UnsupportedObjectManagerException('Given object manager is not managed by wrapper');
}
public static function clear()
{
self::$wrappedObjectReferences = array();
}
/**
* {@inheritDoc}
*/
public function getObject()
{
return $this->object;
}
/**
* {@inheritDoc}
*/
public function getMetadata()
{
return $this->meta;
}
/**
* {@inheritDoc}
*/
public function populate(array $data)
{
foreach ($data as $field => $value) {
$this->setPropertyValue($field, $value);
}
return $this;
}
}

View File

@@ -0,0 +1,138 @@
<?php
namespace Gedmo\Tool\Wrapper;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Proxy\Proxy;
/**
* Wraps entity or proxy for more convenient
* manipulation
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class EntityWrapper extends AbstractWrapper
{
/**
* Entity identifier
*
* @var array
*/
private $identifier;
/**
* True if entity or proxy is loaded
*
* @var boolean
*/
private $initialized = false;
/**
* Wrap entity
*
* @param object $entity
* @param \Doctrine\ORM\EntityManagerInterface $em
*/
public function __construct($entity, EntityManagerInterface $em)
{
$this->om = $em;
$this->object = $entity;
$this->meta = $em->getClassMetadata(get_class($this->object));
}
/**
* {@inheritDoc}
*/
public function getPropertyValue($property)
{
$this->initialize();
return $this->meta->getReflectionProperty($property)->getValue($this->object);
}
/**
* {@inheritDoc}
*/
public function setPropertyValue($property, $value)
{
$this->initialize();
$this->meta->getReflectionProperty($property)->setValue($this->object, $value);
return $this;
}
/**
* {@inheritDoc}
*/
public function hasValidIdentifier()
{
return (null !== $this->getIdentifier());
}
/**
* {@inheritDoc}
*/
public function getRootObjectName()
{
return $this->meta->rootEntityName;
}
/**
* {@inheritDoc}
*/
public function getIdentifier($single = true)
{
if (null === $this->identifier) {
if ($this->object instanceof Proxy) {
$uow = $this->om->getUnitOfWork();
if ($uow->isInIdentityMap($this->object)) {
$this->identifier = $uow->getEntityIdentifier($this->object);
} else {
$this->initialize();
}
}
if (null === $this->identifier) {
$this->identifier = array();
$incomplete = false;
foreach ($this->meta->identifier as $name) {
$this->identifier[$name] = $this->getPropertyValue($name);
if (null === $this->identifier[$name]) {
$incomplete = true;
}
}
if ($incomplete) {
$this->identifier = null;
}
}
}
if ($single && is_array($this->identifier)) {
return reset($this->identifier);
}
return $this->identifier;
}
/**
* Initialize the entity if it is proxy
* required when is detached or not initialized
*/
protected function initialize()
{
if (!$this->initialized) {
if ($this->object instanceof Proxy) {
if (!$this->object->__isInitialized__) {
$this->object->__load();
}
}
}
}
/**
* {@inheritDoc}
*/
public function isEmbeddedAssociation($field)
{
return false;
}
}

View File

@@ -0,0 +1,137 @@
<?php
namespace Gedmo\Tool\Wrapper;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Proxy\Proxy;
/**
* Wraps document or proxy for more convenient
* manipulation
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class MongoDocumentWrapper extends AbstractWrapper
{
/**
* Document identifier
*
* @var mixed
*/
private $identifier;
/**
* True if document or proxy is loaded
*
* @var boolean
*/
private $initialized = false;
/**
* Wrap document
*
* @param object $document
* @param \Doctrine\ODM\MongoDB\DocumentManager $dm
*/
public function __construct($document, DocumentManager $dm)
{
$this->om = $dm;
$this->object = $document;
$this->meta = $dm->getClassMetadata(get_class($this->object));
}
/**
* {@inheritDoc}
*/
public function getPropertyValue($property)
{
$this->initialize();
return $this->meta->getReflectionProperty($property)->getValue($this->object);
}
/**
* {@inheritDoc}
*/
public function getRootObjectName()
{
return $this->meta->rootDocumentName;
}
/**
* {@inheritDoc}
*/
public function setPropertyValue($property, $value)
{
$this->initialize();
$this->meta->getReflectionProperty($property)->setValue($this->object, $value);
return $this;
}
/**
* {@inheritDoc}
*/
public function hasValidIdentifier()
{
return (bool) $this->getIdentifier();
}
/**
* {@inheritDoc}
*/
public function getIdentifier($single = true)
{
if (!$this->identifier) {
if ($this->object instanceof Proxy) {
$uow = $this->om->getUnitOfWork();
if ($uow->isInIdentityMap($this->object)) {
$this->identifier = (string) $uow->getDocumentIdentifier($this->object);
} else {
$this->initialize();
}
}
if (!$this->identifier) {
$this->identifier = (string) $this->getPropertyValue($this->meta->identifier);
}
}
return $this->identifier;
}
/**
* Initialize the document if it is proxy
* required when is detached or not initialized
*/
protected function initialize()
{
if (!$this->initialized) {
if ($this->object instanceof Proxy) {
$uow = $this->om->getUnitOfWork();
if (!$this->object->__isInitialized__) {
$persister = $uow->getDocumentPersister($this->meta->name);
$identifier = null;
if ($uow->isInIdentityMap($this->object)) {
$identifier = $this->getIdentifier();
} else {
// this may not happen but in case
$reflProperty = new \ReflectionProperty($this->object, 'identifier');
$reflProperty->setAccessible(true);
$identifier = $reflProperty->getValue($this->object);
}
$this->object->__isInitialized__ = true;
$persister->load($identifier, $this->object);
}
}
}
}
/**
* {@inheritDoc}
*/
public function isEmbeddedAssociation($field)
{
return $this->getMetadata()->isSingleValuedEmbed($field);
}
}