diff --git a/config/default.php b/config/default.php index bee2eae..c9e2371 100644 --- a/config/default.php +++ b/config/default.php @@ -22,25 +22,6 @@ return array_merge( 'doctrine.debug' => get('debug'), 'doctrine.single-connection' => false, - 'doctrine.connections' => add([ - 'default', - 'example', - ]), - - 'doctrine.connection.default' => get('doctrine.connection'), - 'doctrine.entity-paths.default' => get('doctrine.entity-paths'), - - 'doctrine.entity-paths' => add([ - string('{basedir}/src/Entity') - ]), - - 'doctrine.connection' => add([ - 'driver' => 'pdo_mysql', - 'hostname' => 'localhost', - 'password' => '', - 'user' => 'root', - ]), - 'em' => get(EntityManager::class), EntityManager::class => factory(function (Container $c) { @@ -65,8 +46,15 @@ return array_merge( 'entity-paths' => realpath(__DIR__ . '/../src/Entity') ], 'default' => [ - 'connection' => get('doctrine.connection'), - 'entity-paths' => get('doctrine.entity-paths'), + 'connection' => add([ + 'driver' => 'pdo_mysql', + 'hostname' => 'localhost', + 'password' => '', + 'user' => 'root', + ]), + 'entity-paths' => add([ + string('{basedir}/src/Entity') + ]), ] ]) ); \ No newline at end of file diff --git a/src/ContextAwareEntityManager.php b/src/ContextAwareEntityManager.php index ceddead..37a253d 100644 --- a/src/ContextAwareEntityManager.php +++ b/src/ContextAwareEntityManager.php @@ -5,6 +5,8 @@ namespace BitCommunism\Doctrine; use DI\Container; +use Doctrine\Common\Persistence\Mapping\ClassMetadata; +use Doctrine\Common\Persistence\ObjectRepository; use Doctrine\ORM\Tools\Setup; class ContextAwareEntityManager implements EntityManager @@ -70,6 +72,10 @@ class ContextAwareEntityManager implements EntityManager $entityPaths = $this->container->get($fullEntityPathKey); + if (!is_array($entityPaths)) { + $entityPaths = [$entityPaths]; + } + foreach ($entityPaths as $entityPath) { $this->entityManagerPathMap[$entityPath] = $name; } @@ -246,12 +252,12 @@ class ContextAwareEntityManager implements EntityManager } } - public function getRepository($className) + public function getRepository($className): ObjectRepository { return $this->getEntityManagerForClass($className)->getRepository($className); } - public function getClassMetadata($className) + public function getClassMetadata($className): ClassMetadata { return $this->getEntityManagerForClass($className)->getClassMetadata($className); } @@ -261,7 +267,7 @@ class ContextAwareEntityManager implements EntityManager return $this->getEntityManagerForObject($object)->initializeObject($object); } - public function contains($object) + public function contains($object): bool { return $this->getEntityManagerForObject($object)->contains($object); } diff --git a/src/EntityManager.php b/src/EntityManager.php index 61060a5..ff3eeec 100644 --- a/src/EntityManager.php +++ b/src/EntityManager.php @@ -4,6 +4,9 @@ namespace BitCommunism\Doctrine; +use Doctrine\Common\Persistence\Mapping\ClassMetadata; +use Doctrine\Common\Persistence\ObjectRepository; + interface EntityManager { public function getResponsibleEntityManager(string $name): DefaultEntityManager; diff --git a/src/functions.php b/src/functions.php index d8778e2..5af1ef1 100644 --- a/src/functions.php +++ b/src/functions.php @@ -13,14 +13,16 @@ if (!function_exists(__NAMESPACE__ . '\\connections')) { function connections($connections) { $connectionNames = []; - $entityPaths = []; - $connectionInfos = []; + $containerDef = []; 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'] ?? []); + $entityPathsObject = $connection['entity-paths']; + $connectionObject = $connection['connection']; + + $containerDef['doctrine.entity-paths.' . $name] = $entityPathsObject; + $containerDef['doctrine.connection.' . $name] = $connectionObject; } @@ -28,8 +30,7 @@ if (!function_exists(__NAMESPACE__ . '\\connections')) { [ 'doctrine.connections' => add($connectionNames), ], - $entityPaths, - $connectionInfos + $containerDef ); } } \ No newline at end of file