Several fixes
This commit is contained in:
parent
39f910d7e1
commit
90a2f5594d
8 changed files with 168 additions and 61 deletions
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "bitcommunism/doctrine",
|
"name": "bitcommunism/doctrine",
|
||||||
"version": "1.0.0",
|
"version": "1.0.3",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"require": {
|
"require": {
|
||||||
"doctrine/orm": "^2.6"
|
"doctrine/orm": "^2.6"
|
||||||
|
@ -8,6 +8,9 @@
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"BitCommunism\\Doctrine\\": "src/"
|
"BitCommunism\\Doctrine\\": "src/"
|
||||||
}
|
},
|
||||||
|
"files": [
|
||||||
|
"src/functions.php"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,46 +9,64 @@ use function DI\factory;
|
||||||
use function DI\get;
|
use function DI\get;
|
||||||
use function DI\string;
|
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([
|
return ConsoleRunner::createHelperSet($connection);
|
||||||
'doctrine' => function (EntityManagerLike $em, $arguments) {
|
},
|
||||||
$connection = $em->getEntityManager($arguments[0] ?? 'default');
|
]),
|
||||||
|
|
||||||
return ConsoleRunner::createHelperSet($connection);
|
'doctrine.debug' => get('debug'),
|
||||||
},
|
'doctrine.single-connection' => false,
|
||||||
]),
|
|
||||||
|
|
||||||
'doctrine.debug' => get('debug'),
|
'doctrine.connections' => add([
|
||||||
'doctrine.single-connection' => false,
|
'default',
|
||||||
|
'example',
|
||||||
|
]),
|
||||||
|
|
||||||
'doctrine.connections' => add([
|
'doctrine.connection.default' => get('doctrine.connection'),
|
||||||
'default',
|
'doctrine.entity-paths.default' => get('doctrine.entity-paths'),
|
||||||
]),
|
|
||||||
|
|
||||||
'doctrine.connection.default' => get('doctrine.connection'),
|
'doctrine.entity-paths' => add([
|
||||||
'doctrine.entity-paths.default' => get('doctrine.entity-paths'),
|
string('{basedir}/src/Entity')
|
||||||
|
]),
|
||||||
|
|
||||||
'doctrine.entity-paths' => add([
|
'doctrine.connection' => add([
|
||||||
string('{basedir}/src/Entity')
|
'driver' => 'pdo_mysql',
|
||||||
]),
|
'hostname' => 'localhost',
|
||||||
|
'password' => '',
|
||||||
|
'user' => 'root',
|
||||||
|
]),
|
||||||
|
|
||||||
'doctrine.connection' => add([
|
'em' => get(EntityManager::class),
|
||||||
'driver' => 'pdo_mysql',
|
|
||||||
'hostname' => 'localhost',
|
|
||||||
'password' => '',
|
|
||||||
'user' => 'root',
|
|
||||||
]),
|
|
||||||
|
|
||||||
'em' => get(EntityManagerLike::class),
|
EntityManager::class => factory(function (Container $c) {
|
||||||
|
$contextAware = $c->get(ContextAwareEntityManager::class);
|
||||||
|
|
||||||
EntityManagerLike::class => factory(function (Container $c) {
|
if ($c->get('doctrine.single-connection')) {
|
||||||
$contextAware = $c->get(ContextAwareEntityManager::class);
|
return $contextAware->getResponsibleEntityManager('default');
|
||||||
|
}
|
||||||
|
|
||||||
if ($c->get('doctrine.single-connection')) {
|
return $contextAware;
|
||||||
return $contextAware->getEntityManager('default');
|
}),
|
||||||
}
|
],
|
||||||
|
connections([
|
||||||
return $contextAware;
|
'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 DI\Container;
|
||||||
use Doctrine\ORM\Tools\Setup;
|
use Doctrine\ORM\Tools\Setup;
|
||||||
|
|
||||||
class ContextAwareEntityManager implements EntityManagerLike
|
class ContextAwareEntityManager implements EntityManager
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var Container
|
* @var Container
|
||||||
|
@ -50,6 +50,8 @@ class ContextAwareEntityManager implements EntityManagerLike
|
||||||
$this->container = $c;
|
$this->container = $c;
|
||||||
$connectionNames = $c->get('doctrine.connections');
|
$connectionNames = $c->get('doctrine.connections');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
foreach ($connectionNames as $connectionName) {
|
foreach ($connectionNames as $connectionName) {
|
||||||
$this->indexConnection($connectionName);
|
$this->indexConnection($connectionName);
|
||||||
}
|
}
|
||||||
|
@ -100,11 +102,11 @@ class ContextAwareEntityManager implements EntityManagerLike
|
||||||
*/
|
*/
|
||||||
public function getEntityManagerForClass(string $className): DefaultEntityManager
|
public function getEntityManagerForClass(string $className): DefaultEntityManager
|
||||||
{
|
{
|
||||||
if (!isset($this->entityMangerKeyCache)) {
|
if (!isset($this->entityMangerKeyCache[$className])) {
|
||||||
$this->entityMangerKeyCache[$className] = $this->resolveEntityManagerKeyForClass($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
|
private function resolveEntityManagerKeyForClass(string $className): string
|
||||||
{
|
{
|
||||||
|
|
||||||
if (isset($this->entityManagerKeyMap[$className])) {
|
if (isset($this->entityManagerKeyMap[$className])) {
|
||||||
return $this->entityManagerKeyMap[$className];
|
return $this->entityManagerKeyMap[$className];
|
||||||
}
|
}
|
||||||
|
@ -149,7 +152,7 @@ class ContextAwareEntityManager implements EntityManagerLike
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @return DefaultEntityManager
|
* @return DefaultEntityManager
|
||||||
*/
|
*/
|
||||||
public function getEntityManager(string $name): DefaultEntityManager
|
public function getResponsibleEntityManager(string $name): DefaultEntityManager
|
||||||
{
|
{
|
||||||
if (!isset($this->entityManagers[$name])) {
|
if (!isset($this->entityManagers[$name])) {
|
||||||
$this->entityManagers[$name] = $this->createEntityManager($name);
|
$this->entityManagers[$name] = $this->createEntityManager($name);
|
||||||
|
@ -158,10 +161,6 @@ class ContextAwareEntityManager implements EntityManagerLike
|
||||||
return $this->entityManagers[$name];
|
return $this->entityManagers[$name];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $name
|
|
||||||
* @return DefaultEntityManager
|
|
||||||
*/
|
|
||||||
protected function createEntityManager(string $name): DefaultEntityManager
|
protected function createEntityManager(string $name): DefaultEntityManager
|
||||||
{
|
{
|
||||||
if (!$this->container->has('doctrine.connection.' . $name)) {
|
if (!$this->container->has('doctrine.connection.' . $name)) {
|
||||||
|
@ -176,7 +175,14 @@ class ContextAwareEntityManager implements EntityManagerLike
|
||||||
|
|
||||||
$paths = $this->container->get('doctrine.entity-paths.' . $name);
|
$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);
|
return DefaultEntityManager::create($connection, $config, null, $name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -210,7 +216,13 @@ class ContextAwareEntityManager implements EntityManagerLike
|
||||||
|
|
||||||
public function clear($objectName = null)
|
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)
|
public function detach($object)
|
||||||
|
@ -225,7 +237,7 @@ class ContextAwareEntityManager implements EntityManagerLike
|
||||||
|
|
||||||
public function flush($entity = null)
|
public function flush($entity = null)
|
||||||
{
|
{
|
||||||
if ($entity === null) {
|
if ($entity !== null) {
|
||||||
return $this->getEntityManagerForObject($entity)->flush($entity);
|
return $this->getEntityManagerForObject($entity)->flush($entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,14 +3,13 @@
|
||||||
|
|
||||||
namespace BitCommunism\Doctrine;
|
namespace BitCommunism\Doctrine;
|
||||||
|
|
||||||
|
|
||||||
use Doctrine\Common\EventManager;
|
use Doctrine\Common\EventManager;
|
||||||
use Doctrine\DBAL\Connection;
|
use Doctrine\DBAL\Connection;
|
||||||
use Doctrine\ORM\Configuration;
|
use Doctrine\ORM\Configuration;
|
||||||
use Doctrine\ORM\EntityManager;
|
use Doctrine\ORM\EntityManager as DoctrineEntityManager;
|
||||||
use Doctrine\ORM\ORMException;
|
use Doctrine\ORM\ORMException;
|
||||||
|
|
||||||
class DefaultEntityManager extends EntityManager implements EntityManagerLike
|
class DefaultEntityManager extends DoctrineEntityManager implements EntityManager
|
||||||
{
|
{
|
||||||
protected $name;
|
protected $name;
|
||||||
|
|
||||||
|
@ -46,7 +45,7 @@ class DefaultEntityManager extends EntityManager implements EntityManagerLike
|
||||||
return new static($connection, $config, $connection->getEventManager(), $name);
|
return new static($connection, $config, $connection->getEventManager(), $name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getEntityManager(string $name): DefaultEntityManager
|
public function getResponsibleEntityManager(string $name): DefaultEntityManager
|
||||||
{
|
{
|
||||||
if ($name !== $this->name) {
|
if ($name !== $this->name) {
|
||||||
throw new \RuntimeException("This DefaultEntityManager instance only has em with the name '{$name}'");
|
throw new \RuntimeException("This DefaultEntityManager instance only has em with the name '{$name}'");
|
||||||
|
|
26
src/Entity/Lorem.php
Normal file
26
src/Entity/Lorem.php
Normal file
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
25
src/EntityManager.php
Normal file
25
src/EntityManager.php
Normal file
|
@ -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);
|
|
||||||
}
|
|
35
src/functions.php
Normal file
35
src/functions.php
Normal file
|
@ -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…
Reference in a new issue