-
-
Notifications
You must be signed in to change notification settings - Fork 77
Add a persister and purger for Doctrine which operates on all known managers #108
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
theofidry
wants to merge
3
commits into
main
Choose a base branch
from
feature/multiple-entity-managers
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
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\Common\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[get_class($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[get_class($manager)]; | ||
| } | ||
| } |
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,74 @@ | ||
| <?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 function array_map; | ||
| use Doctrine\Common\Persistence\ManagerRegistry; | ||
| use Doctrine\Common\Persistence\ObjectManager; | ||
| use Fidry\AliceDataFixtures\Bridge\Doctrine\Purger\ObjectManagerPurger; | ||
| 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\Common\Persistence\ObjectManager; | ||
| use Doctrine\DBAL\Driver\AbstractMySQLDriver; | ||
| use Doctrine\ODM\MongoDB\DocumentManager as DoctrineMongoDocumentManager; | ||
| use Doctrine\ODM\PHPCR\DocumentManager as DoctrinePhpCrDocumentManager; | ||
| use Doctrine\ORM\EntityManagerInterface; | ||
| 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 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 | ||
| $truncateOrm = ( | ||
| $this->purger instanceof DoctrineOrmPurger | ||
| && PurgeMode::createTruncateMode()->getValue() === $this->purgeMode->getValue() | ||
| && $this->purger->getObjectManager()->getConnection()->getDriver() instanceof AbstractMySQLDriver | ||
| ); | ||
|
|
||
| if ($truncateOrm) { | ||
| $connection = $this->purger->getObjectManager()->getConnection(); | ||
|
|
||
| $connection->exec('SET FOREIGN_KEY_CHECKS = 0;'); | ||
| } | ||
|
|
||
| $this->purger->purge(); | ||
|
|
||
| if ($truncateOrm && 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 part here makes the assumption that data from all managers (and hence databases) should be purged every time.
IMHO this seems counter intuitive when running
bin/console hautelook:fixtures:load --manager=foo. I certainly wouldn't expect the database of managerbarbe also purged.Looking at the problem from a wider perspective, perhaps it would be more sensible to remove the
--manageroption and instead create a purger for only those managers which contain at least a single entity with fixtures defined for it?Uh oh!
There was an error while loading. Please reload this page.
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.
If that could be done I guess it would be less potential side-effects indeed... Either that or
--managershould restrict the purgers as well