Skip to content
This repository has been archived by the owner on Oct 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1 from chubbyphp/allow-factory
Browse files Browse the repository at this point in the history
allow-factory
  • Loading branch information
healerz authored Oct 1, 2019
2 parents 76f1b8e + a7d20de commit 781818d
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ matrix:
env: dependencies=lowest
- php: 7.3
env: dependencies=highest
- php: 7.4snapshot
env: dependencies=lowest
- php: 7.4snapshot
env: dependencies=highest
allow_failures:
- php: 7.4snapshot

before_script:
- echo "USE mysql;\nUPDATE user SET authentication_string=PASSWORD('root') WHERE user='root';\nFLUSH PRIVILEGES;\n" | mysql -u root
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Doctrine database service providers for doctrine dbal and orm.
Through [Composer](http://getcomposer.org) as [chubbyphp/chubbyphp-doctrine-db-service-provider][1].

```sh
composer require chubbyphp/chubbyphp-doctrine-db-service-provider "~1.2"
composer require chubbyphp/chubbyphp-doctrine-db-service-provider "^1.3"
```

## Providers
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.2-dev"
"dev-master": "1.3-dev"
}
}
}
5 changes: 4 additions & 1 deletion src/Registry/DoctrineOrmManagerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ public function resetManager($name = null)

$originalManager = $this->originalManagers[$name];

$this->resetedManagers[$name] = EntityManager::create(
/** @var callable $entityManagerFactory */
$entityManagerFactory = $this->container['doctrine.orm.em.factory'];

$this->resetedManagers[$name] = $entityManagerFactory(
$originalManager->getConnection(),
$originalManager->getConfiguration(),
$originalManager->getEventManager()
Expand Down
19 changes: 18 additions & 1 deletion src/ServiceProvider/DoctrineOrmServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
use Chubbyphp\DoctrineDbServiceProvider\Driver\ClassMapDriver;
use Chubbyphp\DoctrineDbServiceProvider\Registry\DoctrineOrmManagerRegistry;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\EventManager;
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
use Doctrine\Common\Persistence\Mapping\Driver\PHPDriver;
use Doctrine\Common\Persistence\Mapping\Driver\StaticPHPDriver;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\Cache\CacheConfiguration;
use Doctrine\ORM\Cache\DefaultCacheFactory;
use Doctrine\ORM\Cache\RegionsConfiguration;
Expand Down Expand Up @@ -44,6 +46,7 @@ public function register(Container $container)
$container['doctrine.orm.em'] = $this->getOrmEmDefinition($container);
$container['doctrine.orm.em.config'] = $this->getOrmEmConfigDefinition($container);
$container['doctrine.orm.em.default_options'] = $this->getOrmEmDefaultOptions();
$container['doctrine.orm.em.factory'] = $this->getOrmEmFactory($container);
$container['doctrine.orm.ems'] = $this->getOrmEmsDefinition($container);
$container['doctrine.orm.ems.config'] = $this->getOrmEmsConfigServiceProvider($container);
$container['doctrine.orm.ems.options.initializer'] = $this->getOrmEmsOptionsInitializerDefinition($container);
Expand Down Expand Up @@ -121,6 +124,20 @@ private function getOrmEmDefaultOptions(): array
];
}

/**
* @param Container $container
*
* @return callable
*/
private function getOrmEmFactory(Container $container): callable
{
return $container->protect(
function (Connection $connection, Configuration $config, EventManager $eventManager) {
return EntityManager::create($connection, $config, $eventManager);
}
);
}

/**
* @param Container $container
*
Expand All @@ -140,7 +157,7 @@ private function getOrmEmsDefinition(Container $container): callable
}

$ems[$name] = function () use ($container, $options, $config) {
return EntityManager::create(
return $container['doctrine.orm.em.factory'](
$container['doctrine.dbal.dbs'][$options['connection']],
$config,
$container['doctrine.dbal.dbs.event_manager'][$options['connection']]
Expand Down
11 changes: 5 additions & 6 deletions tests/Registry/DoctrineOrmManagerRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,6 @@ public function testGetConnectionNames()

public function testGetDefaultManagerName()
{
/** @var EntityManager|MockObject $manager */
$manager = $this->getMockByCalls(EntityManager::class);

/** @var Container|MockObject $container */
$container = $this->getMockByCalls(Container::class, [
Call::create('offsetGet')->with('doctrine.orm.ems')->willReturn($this->getMockByCalls(Container::class)),
Expand Down Expand Up @@ -317,6 +314,11 @@ public function testResetManager()
])
),
Call::create('offsetGet')->with('doctrine.orm.ems.default')->willReturn('default'),
Call::create('offsetGet')->with('doctrine.orm.em.factory')->willReturn(
function (Connection $connection, Configuration $config, EventManager $eventManager) {
return EntityManager::create($connection, $config, $eventManager);
}
),
]);

$registry = new DoctrineOrmManagerRegistry($container);
Expand All @@ -328,9 +330,6 @@ public function testResetMissingManager()
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Missing manager with name "default".');

/** @var EntityManager|MockObject $manager */
$manager = $this->getMockByCalls(EntityManager::class);

/** @var Container|MockObject $container */
$container = $this->getMockByCalls(Container::class, [
Call::create('offsetGet')->with('doctrine.orm.ems')->willReturn(
Expand Down

0 comments on commit 781818d

Please sign in to comment.