This commit is contained in:
Xes
2025-08-14 22:41:49 +02:00
parent 2de81ccc46
commit 8ce45119b6
39774 changed files with 4309466 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\CoreBundle\Model\Adapter;
class AdapterChain implements AdapterInterface
{
protected $adapters = array();
/**
* @param AdapterInterface $adapter
*/
public function addAdapter(AdapterInterface $adapter)
{
$this->adapters[] = $adapter;
}
/**
* {@inheritdoc}
*/
public function getNormalizedIdentifier($model)
{
foreach ($this->adapters as $adapter) {
$identifier = $adapter->getNormalizedIdentifier($model);
if ($identifier) {
return $identifier;
}
}
return;
}
/**
* {@inheritdoc}
*/
public function getUrlsafeIdentifier($model)
{
foreach ($this->adapters as $adapter) {
$safeIdentifier = $adapter->getUrlsafeIdentifier($model);
if ($safeIdentifier) {
return $safeIdentifier;
}
}
return;
}
}

View File

@@ -0,0 +1,38 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\CoreBundle\Model\Adapter;
interface AdapterInterface
{
const ID_SEPARATOR = '~';
/**
* Get the identifiers for this model class as a string.
*
* @param object $model
*
* @return string a string representation of the identifiers for this instance
*/
public function getNormalizedIdentifier($model);
/**
* Get the identifiers as a string that is save to use in an url.
*
* This is similar to getNormalizedIdentifier but guarantees an id that can
* be used in an URL.
*
* @param object $model
*
* @return string string representation of the id that is save to use in an url
*/
public function getUrlsafeIdentifier($model);
}

View File

@@ -0,0 +1,71 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\CoreBundle\Model\Adapter;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityManagerInterface;
/**
* This is a port of the DoctrineORMAdminBundle / ModelManager class.
*/
class DoctrineORMAdapter implements AdapterInterface
{
/**
* @var ManagerRegistry
*/
protected $registry;
/**
* @param ManagerRegistry $registry
*/
public function __construct(ManagerRegistry $registry)
{
$this->registry = $registry;
}
/**
* {@inheritdoc}
*/
public function getNormalizedIdentifier($entity)
{
if (is_scalar($entity)) {
throw new \RunTimeException('Invalid argument, object or null required');
}
if (!$entity) {
return;
}
$manager = $this->registry->getManagerForClass(get_class($entity));
if (!$manager instanceof EntityManagerInterface) {
return;
}
if (!$manager->getUnitOfWork()->isInIdentityMap($entity)) {
return;
}
return implode(self::ID_SEPARATOR, $manager->getUnitOfWork()->getEntityIdentifier($entity));
}
/**
* {@inheritdoc}
*
* The ORM implementation does nothing special but you still should use
* this method when using the id in a URL to allow for future improvements.
*/
public function getUrlsafeIdentifier($entity)
{
return $this->getNormalizedIdentifier($entity);
}
}

View File

@@ -0,0 +1,80 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\CoreBundle\Model\Adapter;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ODM\PHPCR\DocumentManager;
/**
* This is a port of the DoctrineORMAdminBundle / ModelManager class.
*/
class DoctrinePHPCRAdapter implements AdapterInterface
{
/**
* @var ManagerRegistry
*/
protected $registry;
/**
* @param ManagerRegistry $registry
*/
public function __construct(ManagerRegistry $registry)
{
$this->registry = $registry;
}
/**
* {@inheritdoc}
*/
public function getNormalizedIdentifier($document)
{
if (is_scalar($document)) {
throw new \RunTimeException('Invalid argument, object or null required');
}
if (!$document) {
return;
}
$manager = $this->registry->getManagerForClass($document);
if (!$manager instanceof DocumentManager) {
return;
}
if (!$manager->contains($document)) {
return;
}
$class = $manager->getClassMetadata(get_class($document));
return $class->getIdentifierValue($document);
}
/**
* Currently only the leading slash is removed.
*
* TODO: do we also have to encode certain characters like spaces or does that happen automatically?
*
* {@inheritdoc}
*/
public function getUrlsafeIdentifier($document)
{
$id = $this->getNormalizedIdentifier($document);
if (null !== $id) {
return substr($id, 1);
}
return;
}
}