vendor/theofidry/alice-data-fixtures/src/Loader/PersisterLoader.php line 47

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Fidry\AliceDataFixtures package.
  4. *
  5. * (c) Théo FIDRY <theo.fidry@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace Fidry\AliceDataFixtures\Loader;
  12. use Fidry\AliceDataFixtures\LoaderInterface;
  13. use Fidry\AliceDataFixtures\Persistence\PersisterAwareInterface;
  14. use Fidry\AliceDataFixtures\Persistence\PersisterInterface;
  15. use Fidry\AliceDataFixtures\Persistence\PurgeMode;
  16. use Fidry\AliceDataFixtures\ProcessorInterface;
  17. use JetBrains\PhpStorm\Pure;
  18. use Nelmio\Alice\IsAServiceTrait;
  19. use Psr\Log\LoggerInterface;
  20. use Psr\Log\NullLogger;
  21. /**
  22. * Loader decorating another loader to add a persistence layer.
  23. *
  24. * @author Jordi Boggiano <j.boggiano@seld.be>
  25. * @author Théo FIDRY <theo.fidry@gmail.com>
  26. *
  27. * @final
  28. */
  29. /*final*/ class PersisterLoader implements LoaderInterface, PersisterAwareInterface
  30. {
  31. use IsAServiceTrait;
  32. private LoaderInterface $loader;
  33. private PersisterInterface $persister;
  34. private LoggerInterface $logger;
  35. private array $processors;
  36. /**
  37. * @param ProcessorInterface[] $processors
  38. */
  39. #[Pure]
  40. public function __construct(
  41. LoaderInterface $decoratedLoader,
  42. PersisterInterface $persister,
  43. LoggerInterface $logger = null,
  44. array $processors = []
  45. ) {
  46. $this->loader = $decoratedLoader;
  47. $this->persister = $persister;
  48. $this->logger = $logger ?? new NullLogger();
  49. $this->processors = (static function (ProcessorInterface ...$processors) {
  50. return $processors;
  51. })(...$processors);
  52. }
  53. public function withPersister(PersisterInterface $persister): self
  54. {
  55. $loader = $this->loader;
  56. if ($loader instanceof PersisterAwareInterface) {
  57. $loader = $loader->withPersister($persister);
  58. }
  59. return new self($loader, $persister, $this->logger, $this->processors);
  60. }
  61. /**
  62. * Pre-process, persist and post process each object loaded.
  63. *
  64. * {@inheritdoc}
  65. */
  66. public function load(array $fixturesFiles, array $parameters = [], array $objects = [], PurgeMode $purgeMode = null): array
  67. {
  68. $objects = $this->loader->load($fixturesFiles, $parameters, $objects, $purgeMode);
  69. $this->logger->info('Pre-processing objects.');
  70. foreach ($objects as $id => $object) {
  71. foreach ($this->processors as $processor) {
  72. $processor->preProcess($id, $object);
  73. }
  74. $this->persister->persist($object);
  75. }
  76. $this->logger->info('Flushing objects.');
  77. $this->persister->flush();
  78. $this->logger->info('Post-processing objects.');
  79. foreach ($objects as $id => $object) {
  80. foreach ($this->processors as $processor) {
  81. $processor->postProcess($id, $object);
  82. }
  83. }
  84. $this->logger->info('Done.');
  85. return $objects;
  86. }
  87. }