Several fixes

master
eater 7 years ago
parent 39f910d7e1
commit 90a2f5594d
Signed by: eater
GPG Key ID: AD2560A0F84F0759

@ -1,6 +1,6 @@
{
"name": "bitcommunism/doctrine",
"version": "1.0.0",
"version": "1.0.3",
"license": "MIT",
"require": {
"doctrine/orm": "^2.6"
@ -8,6 +8,9 @@
"autoload": {
"psr-4": {
"BitCommunism\\Doctrine\\": "src/"
}
},
"files": [
"src/functions.php"
]
}
}

@ -9,46 +9,64 @@ use function DI\factory;
use function DI\get;
use function DI\string;
return [
return array_merge(
[
'marx.calls' => add([
'doctrine' => function (EntityManager $em, $arguments) {
$connection = $em->getResponsibleEntityManager($arguments[0] ?? 'default');
'marx.calls' => add([
'doctrine' => function (EntityManagerLike $em, $arguments) {
$connection = $em->getEntityManager($arguments[0] ?? 'default');
return ConsoleRunner::createHelperSet($connection);
},
]),
return ConsoleRunner::createHelperSet($connection);
},
]),
'doctrine.debug' => get('debug'),
'doctrine.single-connection' => false,
'doctrine.debug' => get('debug'),
'doctrine.single-connection' => false,
'doctrine.connections' => add([
'default',
'example',
]),
'doctrine.connections' => add([
'default',
]),
'doctrine.connection.default' => get('doctrine.connection'),
'doctrine.entity-paths.default' => get('doctrine.entity-paths'),
'doctrine.connection.default' => get('doctrine.connection'),
'doctrine.entity-paths.default' => get('doctrine.entity-paths'),
'doctrine.entity-paths' => add([
string('{basedir}/src/Entity')
]),
'doctrine.entity-paths' => add([
string('{basedir}/src/Entity')
]),
'doctrine.connection' => add([
'driver' => 'pdo_mysql',
'hostname' => 'localhost',
'password' => '',
'user' => 'root',
]),
'doctrine.connection' => add([
'driver' => 'pdo_mysql',
'hostname' => 'localhost',
'password' => '',
'user' => 'root',
]),
'em' => get(EntityManager::class),
'em' => get(EntityManagerLike::class),
EntityManager::class => factory(function (Container $c) {
$contextAware = $c->get(ContextAwareEntityManager::class);
EntityManagerLike::class => factory(function (Container $c) {
$contextAware = $c->get(ContextAwareEntityManager::class);
if ($c->get('doctrine.single-connection')) {
return $contextAware->getResponsibleEntityManager('default');
}
if ($c->get('doctrine.single-connection')) {
return $contextAware->getEntityManager('default');
}
return $contextAware;
}),
];
return $contextAware;
}),
],
connections([
'example' => [
'connection' => [
'driver' => 'pdo_mysql',
'hostname' => 'localhost',
'password' => '',
'user' => 'root',
'dbname' => 'ipsum',
],
'entity-paths' => realpath(__DIR__ . '/../src/Entity')
],
'default' => [
'connection' => get('doctrine.connection'),
'entity-paths' => get('doctrine.entity-paths'),
]
])
);

@ -7,7 +7,7 @@ namespace BitCommunism\Doctrine;
use DI\Container;
use Doctrine\ORM\Tools\Setup;
class ContextAwareEntityManager implements EntityManagerLike
class ContextAwareEntityManager implements EntityManager
{
/**
* @var Container
@ -50,6 +50,8 @@ class ContextAwareEntityManager implements EntityManagerLike
$this->container = $c;
$connectionNames = $c->get('doctrine.connections');
foreach ($connectionNames as $connectionName) {
$this->indexConnection($connectionName);
}
@ -100,11 +102,11 @@ class ContextAwareEntityManager implements EntityManagerLike
*/
public function getEntityManagerForClass(string $className): DefaultEntityManager
{
if (!isset($this->entityMangerKeyCache)) {
if (!isset($this->entityMangerKeyCache[$className])) {
$this->entityMangerKeyCache[$className] = $this->resolveEntityManagerKeyForClass($className);
}
return $this->getEntityManager($this->entityMangerKeyCache[$className]);
return $this->getResponsibleEntityManager($this->entityMangerKeyCache[$className]);
}
/**
@ -114,6 +116,7 @@ class ContextAwareEntityManager implements EntityManagerLike
*/
private function resolveEntityManagerKeyForClass(string $className): string
{
if (isset($this->entityManagerKeyMap[$className])) {
return $this->entityManagerKeyMap[$className];
}
@ -149,7 +152,7 @@ class ContextAwareEntityManager implements EntityManagerLike
* @param string $name
* @return DefaultEntityManager
*/
public function getEntityManager(string $name): DefaultEntityManager
public function getResponsibleEntityManager(string $name): DefaultEntityManager
{
if (!isset($this->entityManagers[$name])) {
$this->entityManagers[$name] = $this->createEntityManager($name);
@ -158,10 +161,6 @@ class ContextAwareEntityManager implements EntityManagerLike
return $this->entityManagers[$name];
}
/**
* @param string $name
* @return DefaultEntityManager
*/
protected function createEntityManager(string $name): DefaultEntityManager
{
if (!$this->container->has('doctrine.connection.' . $name)) {
@ -176,7 +175,14 @@ class ContextAwareEntityManager implements EntityManagerLike
$paths = $this->container->get('doctrine.entity-paths.' . $name);
$config = Setup::createAnnotationMetadataConfiguration($paths, $this->container->get('doctrine.debug'));
$config = Setup::createAnnotationMetadataConfiguration(
$paths,
$this->container->get('doctrine.debug'),
null,
null,
false
);
return DefaultEntityManager::create($connection, $config, null, $name);
}
@ -210,7 +216,13 @@ class ContextAwareEntityManager implements EntityManagerLike
public function clear($objectName = null)
{
// TODO: Implement clear() method.
if ($objectName !== null) {
return $this->getEntityManagerForClass($objectName)->clear($objectName);
}
foreach ($this->entityManagers as $entityManager) {
$entityManager->clear();
}
}
public function detach($object)
@ -225,7 +237,7 @@ class ContextAwareEntityManager implements EntityManagerLike
public function flush($entity = null)
{
if ($entity === null) {
if ($entity !== null) {
return $this->getEntityManagerForObject($entity)->flush($entity);
}

@ -3,14 +3,13 @@
namespace BitCommunism\Doctrine;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManager as DoctrineEntityManager;
use Doctrine\ORM\ORMException;
class DefaultEntityManager extends EntityManager implements EntityManagerLike
class DefaultEntityManager extends DoctrineEntityManager implements EntityManager
{
protected $name;
@ -46,7 +45,7 @@ class DefaultEntityManager extends EntityManager implements EntityManagerLike
return new static($connection, $config, $connection->getEventManager(), $name);
}
public function getEntityManager(string $name): DefaultEntityManager
public function getResponsibleEntityManager(string $name): DefaultEntityManager
{
if ($name !== $this->name) {
throw new \RuntimeException("This DefaultEntityManager instance only has em with the name '{$name}'");

@ -0,0 +1,26 @@
<?php
namespace BitCommunism\Doctrine\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class Lorem
{
/**
* @ORM\Column(type="integer")
* @ORM\Id()
* @ORM\GeneratedValue()
*/
private $id;
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
}

@ -0,0 +1,25 @@
<?php
namespace BitCommunism\Doctrine;
interface EntityManager
{
public function getResponsibleEntityManager(string $name): DefaultEntityManager;
public function find($className, $id);
public function persist($object);
public function remove($object);
public function merge($object);
public function clear($objectName = null);
public function detach($object);
public function refresh($object);
public function flush($entity = null);
public function getRepository($className);
public function getClassMetadata($className);
public function initializeObject($object);
public function contains($object);
public function close();
public function copy($entity, $deep = false);
public function lock($entity, $lockMode, $lockVersion = null);
}

@ -1,11 +0,0 @@
<?php
namespace BitCommunism\Doctrine;
interface EntityManagerLike
{
public function getEntityManager(string $name): DefaultEntityManager;
public function find($className, $id);
}

@ -0,0 +1,35 @@
<?php
namespace BitCommunism\Doctrine;
use function DI\add;
use DI\Definition\EntryReference;
if (!function_exists(__NAMESPACE__ . '\\connections')) {
/**
* @param $connections
* @return array
*/
function connections($connections) {
$connectionNames = [];
$entityPaths = [];
$connectionInfos = [];
foreach ($connections as $name => $connection) {
$connectionNames[] = $name;
$entityPaths['doctrine.entity-paths.' . $name] = $connection['entity-paths'] instanceof EntryReference ? $connection['entity-paths'] : add($connection['entity-paths'] ?? []);
$entityPaths['doctrine.connection.' . $name] = $connection['connection'] instanceof EntryReference ? $connection['connection'] : add($connection['connection'] ?? []);
}
return array_merge(
[
'doctrine.connections' => add($connectionNames),
],
$entityPaths,
$connectionInfos
);
}
}
Loading…
Cancel
Save