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,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;
}
}

View File

@@ -0,0 +1,52 @@
<?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;
use Doctrine\ODM\MongoDB\DocumentManager;
/**
* @author Hugo Briand <briand@ekino.com>
*/
abstract class BaseDocumentManager extends BaseManager
{
/**
* Make sure the code is compatible with legacy code.
*
* @param $name
*
* @return mixed
*/
public function __get($name)
{
if ($name === 'dm') {
return $this->getObjectManager();
}
throw new \RuntimeException(sprintf('The property %s does not exists', $name));
}
/**
* {@inheritdoc}
*/
public function getConnection()
{
return $this->getObjectManager()->getConnection();
}
/**
* @return DocumentManager
*/
public function getDocumentManager()
{
return $this->getObjectManager();
}
}

View File

@@ -0,0 +1,52 @@
<?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;
use Doctrine\ORM\EntityManager;
/**
* @author Sylvain Deloux <sylvain.deloux@ekino.com>
*/
abstract class BaseEntityManager extends BaseManager
{
/**
* Make sure the code is compatible with legacy code.
*
* @param $name
*
* @return mixed
*/
public function __get($name)
{
if ($name === 'em') {
return $this->getObjectManager();
}
throw new \RuntimeException(sprintf('The property %s does not exists', $name));
}
/**
* {@inheritdoc}
*/
public function getConnection()
{
return $this->getEntityManager()->getConnection();
}
/**
* @return EntityManager
*/
public function getEntityManager()
{
return $this->getObjectManager();
}
}

View File

@@ -0,0 +1,171 @@
<?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;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Persistence\ObjectRepository;
/**
* @author Hugo Briand <briand@ekino.com>
*/
abstract class BaseManager implements ManagerInterface
{
/**
* @var ManagerRegistry
*/
protected $registry;
/**
* @var string
*/
protected $class;
/**
* @param string $class
* @param ManagerRegistry $registry
*/
public function __construct($class, ManagerRegistry $registry)
{
$this->registry = $registry;
$this->class = $class;
}
/**
* @return ObjectManager
*/
public function getObjectManager()
{
$manager = $this->registry->getManagerForClass($this->class);
if (!$manager) {
throw new \RuntimeException(sprintf(
'Unable to find the mapping information for the class %s.'
.' Please check the `auto_mapping` option'
.' (http://symfony.com/doc/current/reference/configuration/doctrine.html#configuration-overview)'
.' or add the bundle to the `mappings` section in the doctrine configuration.',
$this->class
));
}
return $manager;
}
/**
* {@inheritdoc}
*/
public function getClass()
{
return $this->class;
}
/**
* {@inheritdoc}
*/
public function findAll()
{
return $this->getRepository()->findAll();
}
/**
* {@inheritdoc}
*/
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
{
return $this->getRepository()->findBy($criteria, $orderBy, $limit, $offset);
}
/**
* {@inheritdoc}
*/
public function findOneBy(array $criteria, array $orderBy = null)
{
return $this->getRepository()->findOneBy($criteria, $orderBy);
}
/**
* {@inheritdoc}
*/
public function find($id)
{
return $this->getRepository()->find($id);
}
/**
* {@inheritdoc}
*/
public function create()
{
return new $this->class();
}
/**
* {@inheritdoc}
*/
public function save($entity, $andFlush = true)
{
$this->checkObject($entity);
$this->getObjectManager()->persist($entity);
if ($andFlush) {
$this->getObjectManager()->flush();
}
}
/**
* {@inheritdoc}
*/
public function delete($entity, $andFlush = true)
{
$this->checkObject($entity);
$this->getObjectManager()->remove($entity);
if ($andFlush) {
$this->getObjectManager()->flush();
}
}
/**
* {@inheritdoc}
*/
public function getTableName()
{
return $this->getObjectManager()->getClassMetadata($this->class)->table['name'];
}
/**
* Returns the related Object Repository.
*
* @return ObjectRepository
*/
protected function getRepository()
{
return $this->getObjectManager()->getRepository($this->class);
}
/**
* @param $object
*
* @throws \InvalidArgumentException
*/
protected function checkObject($object)
{
if (!$object instanceof $this->class) {
throw new \InvalidArgumentException(sprintf(
'Object must be instance of %s, %s given',
$this->class, is_object($object) ? get_class($object) : gettype($object)
));
}
}
}

View File

@@ -0,0 +1,61 @@
<?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;
use Doctrine\Common\Persistence\ObjectManager;
abstract class BasePHPCRManager extends BaseManager
{
/**
* Make sure the code is compatible with legacy code.
*
* @param $name
*
* @return mixed
*/
public function __get($name)
{
if ($name === 'dm') {
return $this->getObjectManager();
}
throw new \RuntimeException(sprintf('The property %s does not exists', $name));
}
/**
* {@inheritdoc}
*
* @throws \LogicException Each call
*/
public function getConnection()
{
throw new \LogicException('PHPCR does not use a database connection.');
}
/**
* {@inheritdoc}
*
* @throws \LogicException Each call
*/
public function getTableName()
{
throw new \LogicException('PHPCR does not use a reference name for a list of data.');
}
/**
* @return ObjectManager
*/
public function getDocumentManager()
{
return $this->getObjectManager();
}
}

View File

@@ -0,0 +1,102 @@
<?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;
use Doctrine\DBAL\Connection;
/**
* @author Sylvain Deloux <sylvain.deloux@ekino.com>
*/
interface ManagerInterface
{
/**
* Return the Entity class name.
*
* @return string
*/
public function getClass();
/**
* Find all entities in the repository.
*
* @return array
*/
public function findAll();
/**
* Find entities by a set of criteria.
*
* @param array $criteria
* @param array|null $orderBy
* @param int|null $limit
* @param int|null $offset
*
* @return array
*/
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null);
/**
* Find a single entity by a set of criteria.
*
* @param array $criteria
* @param array|null $orderBy
*
* @return object|null
*/
public function findOneBy(array $criteria, array $orderBy = null);
/**
* Finds an entity by its primary key / identifier.
*
* @param mixed $id The identifier
*
* @return object
*/
public function find($id);
/**
* Create an empty Entity instance.
*
* @return object
*/
public function create();
/**
* Save an Entity.
*
* @param object $entity The Entity to save
* @param bool $andFlush Flush the EntityManager after saving the object?
*/
public function save($entity, $andFlush = true);
/**
* Delete an Entity.
*
* @param object $entity The Entity to delete
* @param bool $andFlush Flush the EntityManager after deleting the object?
*/
public function delete($entity, $andFlush = true);
/**
* Get the related table name.
*
* @return string
*/
public function getTableName();
/**
* Get the DB driver connection.
*
* @return Connection
*/
public function getConnection();
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,51 @@
<?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;
/**
* @author Hugo Briand <briand@ekino.com>
*/
interface MetadataInterface
{
/**
* @return string
*/
public function getTitle();
/**
* @return string
*/
public function getDescription();
/**
* @return mixed
*/
public function getImage();
/**
* @return string
*/
public function getDomain();
/**
* @return array
*/
public function getOptions();
/**
* @param string $name The option key
* @param mixed $default The default value if option not found
*
* @return mixed
*/
public function getOption($name, $default = null);
}

View File

@@ -0,0 +1,30 @@
<?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;
use Sonata\DatagridBundle\Pager\PagerInterface;
/**
* @author Raphaël Benitte <benitteraphael@gmail.com>
*/
interface PageableManagerInterface
{
/**
* @param array $criteria
* @param int $page
* @param int $limit
* @param array $sort
*
* @return PagerInterface
*/
public function getPager(array $criteria, $page, $limit = 10, array $sort = array());
}