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,94 @@
<?php
namespace Gedmo\References\Mapping\Event\Adapter;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Proxy\Proxy as MongoDBProxy;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Proxy\Proxy as ORMProxy;
use Gedmo\Mapping\Event\Adapter\ODM as BaseAdapterODM;
use Gedmo\References\Mapping\Event\ReferencesAdapter;
/**
* Doctrine event adapter for ODM references behavior
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @author Bulat Shakirzyanov <mallluhuct@gmail.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class ODM extends BaseAdapterODM implements ReferencesAdapter
{
/**
* @inheritDoc
*/
public function getIdentifier($om, $object, $single = true)
{
if ($om instanceof DocumentManager) {
return $this->extractIdentifier($om, $object, $single);
}
if ($om instanceof EntityManagerInterface) {
if ($object instanceof ORMProxy) {
$id = $om->getUnitOfWork()->getEntityIdentifier($object);
} else {
$meta = $om->getClassMetadata(get_class($object));
$id = array();
foreach ($meta->identifier as $name) {
$id[$name] = $meta->getReflectionProperty($name)->getValue($object);
// return null if one of identifiers is missing
if (!$id[$name]) {
return null;
}
}
}
if ($single) {
$id = current($id);
}
return $id;
}
}
/**
* @inheritDoc
*/
public function getSingleReference($om, $class, $identifier)
{
$this->throwIfNotEntityManager($om);
$meta = $om->getClassMetadata($class);
if (!$meta->isInheritanceTypeNone()) {
return $om->find($class, $identifier);
}
return $om->getReference($class, $identifier);
}
/**
* @inheritDoc
*/
public function extractIdentifier($om, $object, $single = true)
{
$meta = $om->getClassMetadata(get_class($object));
if ($object instanceof MongoDBProxy) {
$id = $om->getUnitOfWork()->getDocumentIdentifier($object);
} else {
$id = $meta->getReflectionProperty($meta->identifier)->getValue($object);
}
if ($single || !$id) {
return $id;
} else {
return array($meta->identifier => $id);
}
}
/**
* Override so we don't get an exception. We want to allow this.
*/
private function throwIfNotEntityManager(EntityManagerInterface $em)
{
}
}

View File

@@ -0,0 +1,119 @@
<?php
namespace Gedmo\References\Mapping\Event\Adapter;
use Doctrine\ODM\MongoDB\DocumentManager as MongoDocumentManager;
use Doctrine\ODM\MongoDB\Proxy\Proxy as MongoDBProxy;
use Doctrine\ODM\PHPCR\DocumentManager as PhpcrDocumentManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Proxy\Proxy as ORMProxy;
use Gedmo\Exception\InvalidArgumentException;
use Gedmo\Mapping\Event\Adapter\ORM as BaseAdapterORM;
use Gedmo\References\Mapping\Event\ReferencesAdapter;
/**
* Doctrine event adapter for ORM references behavior
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @author Bulat Shakirzyanov <mallluhuct@gmail.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class ORM extends BaseAdapterORM implements ReferencesAdapter
{
/**
* @inheritDoc
*/
public function getIdentifier($om, $object, $single = true)
{
if ($om instanceof EntityManagerInterface) {
return $this->extractIdentifier($om, $object, $single);
}
if ($om instanceof MongoDocumentManager) {
$meta = $om->getClassMetadata(get_class($object));
if ($object instanceof MongoDBProxy) {
$id = $om->getUnitOfWork()->getDocumentIdentifier($object);
} else {
$id = $meta->getReflectionProperty($meta->identifier)->getValue($object);
}
if ($single || !$id) {
return $id;
}
return array($meta->identifier => $id);
}
if ($om instanceof PhpcrDocumentManager) {
$meta = $om->getClassMetadata(get_class($object));
$id = $meta->getReflectionProperty($meta->identifier)->getValue($object);
if ($single || !$id) {
return $id;
}
return array($meta->identifier => $id);
}
}
/**
* @inheritDoc
*/
public function getSingleReference($om, $class, $identifier)
{
$this->throwIfNotDocumentManager($om);
$meta = $om->getClassMetadata($class);
if ($om instanceof MongoDocumentManager) {
if (!$meta->isInheritanceTypeNone()) {
return $om->find($class, $identifier);
}
}
return $om->getReference($class, $identifier);
}
/**
* @inheritDoc
*/
public function extractIdentifier($om, $object, $single = true)
{
if ($object instanceof ORMProxy) {
$id = $om->getUnitOfWork()->getEntityIdentifier($object);
} else {
$meta = $om->getClassMetadata(get_class($object));
$id = array();
foreach ($meta->identifier as $name) {
$id[$name] = $meta->getReflectionProperty($name)->getValue($object);
// return null if one of identifiers is missing
if (!$id[$name]) {
return null;
}
}
}
if ($single) {
$id = current($id);
}
return $id;
}
/**
* Override so we don't get an exception. We want to allow this.
*/
private function throwIfNotDocumentManager($dm)
{
if (!($dm instanceof MongoDocumentManager) && !($dm instanceof PhpcrDocumentManager)) {
throw new InvalidArgumentException(
sprintf(
'Expected a %s or %s instance but got "%s"',
'Doctrine\ODM\MongoDB\DocumentManager',
'Doctrine\ODM\PHPCR\DocumentManager',
is_object($dm) ? get_class($dm) : gettype($dm)
)
);
}
}
}

View File

@@ -0,0 +1,48 @@
<?php
namespace Gedmo\References\Mapping\Event;
use Doctrine\Common\Persistence\ObjectManager;
use Gedmo\Mapping\Event\AdapterInterface;
/**
* Doctrine event adapter interface for References behavior
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @author Bulat Shakirzyanov <mallluhuct@gmail.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
interface ReferencesAdapter extends AdapterInterface
{
/**
* Gets the identifier of the given object using the passed ObjectManager.
*
* @param ObjectManager $om
* @param object $object
* @param bool $single
*
* @return array|string|int $id - array or single identifier
*/
public function getIdentifier($om, $object, $single = true);
/**
* Gets a single reference for the given ObjectManager, class and identifier.
*
* @param ObjectManager $om
* @param string $class
* @param array|string|int $identifier
**/
public function getSingleReference($om, $class, $identifier);
/**
* Extracts identifiers from object or proxy.
*
* @param ObjectManager $om
* @param object $object
* @param bool $single
*
* @return array|string|int - array or single identifier
*/
public function extractIdentifier($om, $object, $single = true);
}