-
-
Notifications
You must be signed in to change notification settings - Fork 77
Allow multiple entity manager #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jdeniau
wants to merge
14
commits into
theofidry:main
Choose a base branch
from
jdeniau:feature/multiple-entity-managers-fixes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
57ce628
Allow multiple doctrine entity manager instances
Zayon 2216d97
WIP
Zayon 6c3c04d
Add a persister and purger for Doctrine which operates on all known m…
theofidry 7564d02
Fix some issues with multiple entity manager
jdeniau 4465497
Merge remote-tracking branch 'upstream/master' into feature/multiple-…
jdeniau b6a8ab7
change version number in deprecation
jdeniau 9502a66
try to fix tests
jdeniau c1fd2e1
try replacing Purger by ObjectManagerPurger
jdeniau 49eafce
Merge remote-tracking branch 'upstream/master' into feature/multiple-…
jdeniau 9465be2
Merge remote-tracking branch 'upstream/master' into feature/multiple-…
jdeniau 790fc81
tmp
jdeniau 98c0c61
clear unit of work to avoid conflict between tests
jdeniau 21255fe
use ManagerRegistryPurger for doctrine orm, odm and phpcr
jdeniau 20e5db2
fix wrong namespace
jdeniau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
src/Bridge/Doctrine/Persister/ManagerRegistryPersister.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Fidry\AliceDataFixtures package. | ||
| * | ||
| * (c) Théo FIDRY <theo.fidry@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Fidry\AliceDataFixtures\Bridge\Doctrine\Persister; | ||
|
|
||
| use Doctrine\Persistence\ManagerRegistry; | ||
| use Fidry\AliceDataFixtures\Persistence\PersisterInterface; | ||
| use function get_class; | ||
| use function implode; | ||
| use InvalidArgumentException; | ||
| use Nelmio\Alice\IsAServiceTrait; | ||
| use function sprintf; | ||
|
|
||
| /** | ||
| * @final | ||
| */ | ||
| class ManagerRegistryPersister implements PersisterInterface | ||
| { | ||
| use IsAServiceTrait; | ||
|
|
||
| private $registry; | ||
|
|
||
| /** | ||
| * @var PersisterInterface[] | ||
| */ | ||
| private $persisters = []; | ||
|
|
||
| public function __construct(ManagerRegistry $registry) | ||
| { | ||
| $this->registry = $registry; | ||
|
|
||
| $managers = $registry->getManagers(); | ||
|
|
||
| foreach ($managers as $manager) { | ||
| $this->persisters[spl_object_hash($manager)] = new ObjectManagerPersister($manager); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function persist($object) | ||
| { | ||
| $persister = $this->getPersisterForClass(get_class($object)); | ||
|
|
||
| $persister->persist($object); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function flush() | ||
| { | ||
| foreach ($this->persisters as $persister) { | ||
| $persister->flush(); | ||
| } | ||
| } | ||
|
|
||
| private function getPersisterForClass(string $class): PersisterInterface | ||
| { | ||
| $manager = $this->registry->getManagerForClass($class); | ||
|
|
||
| if (null === $manager) { | ||
| throw new InvalidArgumentException( | ||
| sprintf( | ||
| 'Could not find a manager for the class "%s". Known managers: "%s"', | ||
| $class, | ||
| implode('", "', $this->registry->getManagerNames()) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| return $this->persisters[spl_object_hash($manager)]; | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (this matches previous comment) |
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Fidry\AliceDataFixtures package. | ||
| * | ||
| * (c) Théo FIDRY <theo.fidry@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Fidry\AliceDataFixtures\Bridge\Doctrine\Purger; | ||
|
|
||
| use function array_map; | ||
| use Doctrine\Persistence\ManagerRegistry; | ||
| use Doctrine\Persistence\ObjectManager; | ||
| use Fidry\AliceDataFixtures\Persistence\PurgeMode; | ||
| use Fidry\AliceDataFixtures\Persistence\PurgerFactoryInterface; | ||
| use Fidry\AliceDataFixtures\Persistence\PurgerInterface; | ||
| use InvalidArgumentException; | ||
| use Nelmio\Alice\IsAServiceTrait; | ||
|
|
||
| /** | ||
| * @final | ||
| */ | ||
| /* final */ class ManagerRegistryPurger implements PurgerInterface, PurgerFactoryInterface | ||
| { | ||
| use IsAServiceTrait; | ||
|
|
||
| private $registry; | ||
| private $purgeMode; | ||
|
|
||
| /** | ||
| * @var PurgerInterface[] | ||
| */ | ||
| private $purgers = []; | ||
|
|
||
| public function __construct(ManagerRegistry $registry, PurgeMode $purgeMode = null) | ||
| { | ||
| $this->registry = $registry; | ||
|
|
||
| $this->purgers = array_map( | ||
| function (ObjectManager $manager) use ($purgeMode): PurgerInterface { | ||
| return new ObjectManagerPurger($manager, $purgeMode); | ||
| }, | ||
| $registry->getManagers() | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc | ||
| */ | ||
| public function create(PurgeMode $mode, PurgerInterface $purger = null): PurgerInterface | ||
| { | ||
| if (null !== $purger) { | ||
| throw new InvalidArgumentException('Cannot create a new purger from an existing one.'); | ||
| } | ||
|
|
||
| return new self($this->registry, $mode); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc | ||
| */ | ||
| public function purge(): void | ||
| { | ||
| foreach ($this->purgers as $purger) { | ||
| $purger->purge(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Fidry\AliceDataFixtures package. | ||
| * | ||
| * (c) Théo FIDRY <theo.fidry@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Fidry\AliceDataFixtures\Bridge\Doctrine\Purger; | ||
|
|
||
| use Doctrine\Common\DataFixtures\Purger\MongoDBPurger as DoctrineMongoDBPurger; | ||
| use Doctrine\Common\DataFixtures\Purger\ORMPurger as DoctrineOrmPurger; | ||
| use Doctrine\Common\DataFixtures\Purger\PHPCRPurger as DoctrinePhpCrPurger; | ||
| use Doctrine\Common\DataFixtures\Purger\PurgerInterface as DoctrinePurgerInterface; | ||
| use Doctrine\DBAL\Driver\AbstractMySQLDriver; | ||
| use Doctrine\ODM\MongoDB\DocumentManager as DoctrineMongoDocumentManager; | ||
| use Doctrine\ODM\PHPCR\DocumentManager as DoctrinePhpCrDocumentManager; | ||
| use Doctrine\ORM\EntityManagerInterface; | ||
| use Doctrine\Persistence\ObjectManager; | ||
| use Fidry\AliceDataFixtures\Persistence\PurgeMode; | ||
| use Fidry\AliceDataFixtures\Persistence\PurgerFactoryInterface; | ||
| use Fidry\AliceDataFixtures\Persistence\PurgerInterface; | ||
| use InvalidArgumentException; | ||
| use Nelmio\Alice\IsAServiceTrait; | ||
|
|
||
| /** | ||
| * Bridge for Doctrine purger. | ||
| * | ||
| * @author Vincent CHALAMON <vincentchalamon@gmail.com> | ||
| * @final | ||
| */ | ||
| /* final */ class ObjectManagerPurger implements PurgerInterface, PurgerFactoryInterface | ||
| { | ||
| use IsAServiceTrait; | ||
|
|
||
| private $manager; | ||
| private $purgeMode; | ||
| private $purger; | ||
|
|
||
| public function __construct(ObjectManager $manager, PurgeMode $purgeMode = null) | ||
| { | ||
| $this->manager = $manager; | ||
| $this->purgeMode = $purgeMode; | ||
|
|
||
| $this->purger = static::createPurger($manager, $purgeMode); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc | ||
| */ | ||
| public function create(PurgeMode $mode, PurgerInterface $purger = null): PurgerInterface | ||
| { | ||
| if (null === $purger) { | ||
| return new self($this->manager, $mode); | ||
| } | ||
|
|
||
| if ($purger instanceof DoctrinePurgerInterface) { | ||
| $manager = $purger->getObjectManager(); | ||
| } elseif ($purger instanceof self) { | ||
| $manager = $purger->manager; | ||
| } else { | ||
| throw new InvalidArgumentException( | ||
| sprintf( | ||
| 'Expected purger to be either and instance of "%s" or "%s". Got "%s".', | ||
| DoctrinePurgerInterface::class, | ||
| __CLASS__, | ||
| get_class($purger) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| if (null === $manager) { | ||
| throw new InvalidArgumentException( | ||
| sprintf( | ||
| 'Expected purger "%s" to have an object manager, got "null" instead.', | ||
| get_class($purger) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| return new self($manager, $mode); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc | ||
| */ | ||
| public function purge(): void | ||
| { | ||
| // Because MySQL rocks, you got to disable foreign key checks when doing a TRUNCATE/DELETE unlike in for example | ||
| // PostgreSQL. This ideally should be done in the Purger of doctrine/data-fixtures but meanwhile we are doing | ||
| // it here. | ||
| // See the progress in https://github.com/doctrine/data-fixtures/pull/272 | ||
| $disableFkChecks = ( | ||
| $this->purger instanceof DoctrineOrmPurger | ||
| && in_array($this->purgeMode->getValue(), [PurgeMode::createDeleteMode()->getValue(), PurgeMode::createTruncateMode()->getValue()]) | ||
| && $this->purger->getObjectManager()->getConnection()->getDriver() instanceof AbstractMySQLDriver | ||
| ); | ||
|
|
||
| if ($disableFkChecks) { | ||
| $connection = $this->purger->getObjectManager()->getConnection(); | ||
|
|
||
| $connection->exec('SET FOREIGN_KEY_CHECKS = 0;'); | ||
| } | ||
|
|
||
| $this->purger->purge(); | ||
|
|
||
| if ($disableFkChecks && isset($connection)) { | ||
| $connection->exec('SET FOREIGN_KEY_CHECKS = 1;'); | ||
| } | ||
| } | ||
|
|
||
| private static function createPurger(ObjectManager $manager, ?PurgeMode $purgeMode): DoctrinePurgerInterface | ||
| { | ||
| if ($manager instanceof EntityManagerInterface) { | ||
| $purger = new DoctrineOrmPurger($manager); | ||
|
|
||
| if (null !== $purgeMode) { | ||
| $purger->setPurgeMode($purgeMode->getValue()); | ||
| } | ||
|
|
||
| return $purger; | ||
| } | ||
|
|
||
| if ($manager instanceof DoctrinePhpCrDocumentManager) { | ||
| return new DoctrinePhpCrPurger($manager); | ||
| } | ||
|
|
||
| if ($manager instanceof DoctrineMongoDocumentManager) { | ||
| return new DoctrineMongoDBPurger($manager); | ||
| } | ||
|
|
||
| throw new InvalidArgumentException( | ||
| sprintf( | ||
| 'Cannot create a purger for ObjectManager of class %s', | ||
| get_class($manager) | ||
| ) | ||
| ); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This allows having multiple instance of
Doctrine\ORM\EntityManagerThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍