Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Introduce Doctrine ORM
TODOs:

- [x] EntityManager wrapper
- [ ] EntityRepository wrapper
- [ ] Query wrapper (for the DQL)
- [x] Command integration
- [ ] Psr6 Cache wrapper around the ICache
- [ ] Decide if we want a wrapper for the Mapping DTO class (lot of work
  not much beneficts)

Signed-off-by: Carl Schwan <[email protected]>
  • Loading branch information
CarlSchwan committed Aug 24, 2022
commit e255b4b7a2041efb01f3f161409815dca2d7a5ec
9 changes: 9 additions & 0 deletions core/register_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@
$application->add(new OC\Core\Command\Db\Migrations\MigrateCommand(\OC::$server->get(\OC\DB\Connection::class)));
$application->add(new OC\Core\Command\Db\Migrations\GenerateCommand(\OC::$server->get(\OC\DB\Connection::class), \OC::$server->getAppManager()));
$application->add(new OC\Core\Command\Db\Migrations\ExecuteCommand(\OC::$server->get(\OC\DB\Connection::class), \OC::$server->getConfig()));
Doctrine\ORM\Tools\Console\ConsoleRunner::addCommands($application, new class implements \Doctrine\ORM\Tools\Console\EntityManagerProvider {
public function getDefaultManager(): \Doctrine\ORM\EntityManagerInterface {
return \OCP\Server::get(\OCP\DB\ORM\IEntityManager::class)->get();
}

public function getManager(string $name): \Doctrine\ORM\EntityManagerInterface {
return \OCP\Server::get(\OCP\DB\ORM\IEntityManager::class)->get();
}
});
}

$application->add(new OC\Core\Command\Encryption\Disable(\OC::$server->getConfig()));
Expand Down
2 changes: 1 addition & 1 deletion lib/private/DB/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
namespace OC\DB;

use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration;
use Doctrine\ORM\Configuration;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Event\Listeners\OracleSessionInit;
use Doctrine\DBAL\Event\Listeners\SQLSessionInit;
Expand Down
88 changes: 88 additions & 0 deletions lib/private/DB/ORM/EntityManagerAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace OC\DB\ORM;

use Doctrine\Common\EventManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\ORMSetup;
use OC\DB\ConnectionAdapter;
use OCP\DB\ORM\IQuery;
use OCP\DB\ORM\IEntityManager;
use OCP\DB\ORM\IEntityRepository;
use OCP\IDBConnection;

class EntityManagerAdapter implements IEntityManager {

private EntityManager $em;
private ConnectionAdapter $connection;

public function __construct(ConnectionAdapter $connection) {
$paths = array_filter(array_map(fn ($appId) => \OC_App::getAppPath($appId) . '/lib/Entity/', \OC_App::getEnabledApps()), fn ($path) => is_dir($path));
$isDevMode = true;
$proxyDir = null;
$cache = null;


$evm = $connection->getInner()->getEventManager();
$tablePrefix = new TablePrefix('oc_');

$evm->addEventListener(\Doctrine\ORM\Events::loadClassMetadata, $tablePrefix);
// TODO actually use our cache with a psr6 cache wrapper or at least our cache config
$config = ORMSetup::createAnnotationMetadataConfiguration($paths, $isDevMode, $proxyDir, $cache);

$this->em = EntityManager::create($connection->getInner(), $config, $evm);
$this->connection = $connection;
}

public function createQuery($dql = ''): IQuery
{
return new QueryAdapter($this->em->createQuery($dql));
}

public function flush(): void {
$this->em->flush();
}

public function find(string $className, $id, ?int $lockMode = null, ?int $lockVersion = null): ?object {
return $this->em->find($className, $id, $lockMode, $lockVersion);
}

public function clear(): void {
$this->em->clear();
}

public function persist(object $entity): void {
$this->em->persist($entity);
}

public function remove(object $entity): void {
$this->em->remove($entity);
}

public function lock(object $entity, int $lockMode, $lockVersion = null): void {
$this->em->lock($entity, $lockMode, $lockVersion);
}

public function getRepository($className): IEntityRepository {
/** @var EntityRepository $internalRepo */
$internalRepo = $this->em->getRepository($className);
return new EntityRepositoryAdapter($internalRepo);
}

public function contains(object $entity): bool {
return $this->em->contains($entity);
}

public function getConnection(): IDBConnection {
return $this->connection;
}

/**
* Only for internal use
*/
public function get(): EntityManager {
return $this->em;
}

}
15 changes: 15 additions & 0 deletions lib/private/DB/ORM/EntityRepositoryAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace OC\DB\ORM;

use Doctrine\ORM\EntityRepository;
use OCP\DB\ORM\IEntityRepository;

class EntityRepositoryAdapter implements IEntityRepository
{
private EntityRepository $entityRepository;

public function __construct(EntityRepository $entityRepository) {
$this->entityRepository = $entityRepository;
}
}
116 changes: 116 additions & 0 deletions lib/private/DB/ORM/Psr6CacheAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO finish


namespace OC\DB\ORM;

use OCP\ICache;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;

class CacheItemAdapter implements CacheItemInterface {
private ICache $cacheAdapter;
private string $key;
private $value;
private bool $fetched = false;
private ?\DateTime $_expireAt = null;
private int $_expireAfter = -1;

public function __construct(ICache $cache, $key) {
$this->cache = $cache;
$this->key = $key;
}

private function fetch(): void {
if (!$this->fetched) {
$this->value = $this->cache->get($this->key);
$this->fetched = true;
}
}

public function getKey() {
return $this->key;
}

public function get() {
$this->fetch();
return $this->value;
}

public function isHit() {
$this->fetch();
return $this->value !== null;
}

public function set($value) {
$this->value = $value;
}

public function expiresAt($expiration) {
$this->_expireAt = $expiration;
}

public function expiresAfter($time) {
$this->_expireAfter = $time;
}

public function getExpireAt(): ?\DateTime
{
return $this->_expireAt;
}

public function getExpireAfter(): int
{
return $this->_expireAfter;
}
}

class Psr6CacheAdapter implements CacheItemPoolInterface {
private ICache $cache;

public function __construct(ICache $cache) {
$this->cache = $cache;
}

public function getItem($key) {
return new CacheItemAdapter($this->cache, $key);
}

public function getItems(array $keys = array()) {
for (int )
// TODO: Implement getItems() method.
}

public function hasItem($key)
{
// TODO: Implement hasItem() method.
}

public function clear()
{
// TODO: Implement clear() method.
}

public function deleteItem($key)
{
// TODO: Implement deleteItem() method.
}

public function deleteItems(array $keys)
{
// TODO: Implement deleteItems() method.
}

public function save(CacheItemInterface $item)
{
// TODO: Implement save() method.
}

public function saveDeferred(CacheItemInterface $item)
{
// TODO: Implement saveDeferred() method.
}

public function commit()
{
// TODO: Implement commit() method.
}
}
14 changes: 14 additions & 0 deletions lib/private/DB/ORM/QueryAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace OC\DB\ORM;

use Doctrine\ORM\Query;
use OCP\DB\ORM\IQuery;

class QueryAdapter implements IQuery {
private Query $query;

public function __construct(Query $query) {
$this->query = $query;
}
}
34 changes: 34 additions & 0 deletions lib/private/DB/ORM/TablePrefix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace OC\DB\ORM;

use \Doctrine\ORM\Event\LoadClassMetadataEventArgs;

class TablePrefix
{
protected $prefix = 'oc_';

public function __construct($prefix)
{
$this->prefix = (string)$prefix;
}

public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
{
$classMetadata = $eventArgs->getClassMetadata();

if (!$classMetadata->isInheritanceTypeSingleTable() || $classMetadata->getName() === $classMetadata->rootEntityName) {
$classMetadata->setPrimaryTable([
'name' => $this->prefix . $classMetadata->getTableName()
]);
}

foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
if ($mapping['type'] == \Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY && $mapping['isOwningSide']) {
$mappedTableName = $mapping['joinTable']['name'];
$classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix . $mappedTableName;
}
}
}

}
9 changes: 9 additions & 0 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
use OC\Dashboard\DashboardManager;
use OC\DB\Connection;
use OC\DB\ConnectionAdapter;
use OC\DB\ORM\EntityManagerAdapter;
use OC\Diagnostics\EventLogger;
use OC\Diagnostics\QueryLogger;
use OC\EventDispatcher\SymfonyAdapter;
Expand Down Expand Up @@ -167,6 +168,7 @@
use OCP\Contacts\ContactsMenu\IActionFactory;
use OCP\Contacts\ContactsMenu\IContactsStore;
use OCP\Dashboard\IDashboardManager;
use OCP\DB\ORM\IEntityManager;
use OCP\Defaults;
use OCP\Diagnostics\IEventLogger;
use OCP\Diagnostics\IQueryLogger;
Expand Down Expand Up @@ -878,6 +880,13 @@ public function __construct($webRoot, \OC\Config $config) {
$connection = $factory->getConnection($type, $connectionParams);
return $connection;
});

$this->registerService(IEntityManager::class, function (ContainerInterface $c): IEntityManager {
/** @var ConnectionAdapter $connection */
$connection = $c->get(IDBConnection::class);
return new EntityManagerAdapter($connection);
});

/** @deprecated 19.0.0 */
$this->registerDeprecatedAlias('DatabaseConnection', IDBConnection::class);

Expand Down
Loading