Skip to content
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

Feature Request: Add preLoad and postLoad events to enhance fixture loading management #71

Open
EdouardCourty opened this issue Nov 29, 2024 · 2 comments

Comments

@EdouardCourty
Copy link

Hi there! πŸ‘‹

First off, thank you for this fantastic bundle β€” it's been a pleasure to use and has made fixture management so much simpler! πŸ™

I've come across a scenario while building an app that leverages Doctrine's onFlush and postFlush events to trigger additional logic. While this works great under normal circumstances, I’d like to ensure these events are bypassed when loading fixtures.

In the past, I’ve tried using a special environment parameter to manage this, but I’ve found it to be somewhat flaky and not the most reliable or elegant solution. I’ve also considered implementing a stateful service to track whether fixtures are being loaded. However, such an approach would greatly benefit from explicit events fired by the fixture loader itself.

Would it be possible to introduce events like preLoad and postLoad into this bundle? This addition would make it much easier to handle cases like mine and would likely be helpful for others too.

I’d be happy to contribute and submit a PR for this feature if it aligns with the goals of the project.

Thank you for considering this suggestion! Looking forward to hearing your thoughts. 😊

@EdouardCourty EdouardCourty changed the title Feature Request: Add preLoad and postLoad Events to Enhance Fixture Loading Management Feature Request: Add preLoad and postLoad events to enhance fixture loading management Nov 29, 2024
@theofidry
Copy link
Owner

Hi @EdouardCourty! I'm glad this bundle helped you!

Would it be possible to introduce events like preLoad and postLoad into this bundle?

Just to clarify as I don't really understand the use case: the goal here is to know that the fixtures have been loaded? If so wouldn't it be sufficient to register a decorator? (pseudo-code, I did not execute this):

class HautelookAliceLoaderEventDispatcher {
  construct(
    private EventDispatcherInterface $eventDispatcher,
    private \Hautelook\AliceBundle\LoaderInterface $decoratedLoader,
  ) {}

  public function load(
        Application $application,
        EntityManagerInterface $manager,
        array $bundles,
        string $environment,
        bool $append,
        bool $purgeWithTruncate,
        bool $noBundles = false
    ): array {
         $this->eventDispatcher->dispatch(new FixturesLoadingWasStarted());

         $result = $this->decoratedLoader->load(...func_get_args());
         
         $this->eventDispatcher->dispatch(new FixturesWereLoaded());

         return $result;
    }
}
// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use App\DecoratingMailer;
use App\Mailer;

return function(ContainerConfigurator $container): void {
    $services = $container->services();

    $services->set(Mailer::class);

    $services->set(\Hautelook\AliceBundle\LoaderInterface::class)
        ->decorate(HautelookAliceLoaderEventDispatcher::class)
        ->args([service('.inner')]);
};

?

I don't particularly mind the of having it in the core, but we'll have to be mindful that the event dispatcher may not always be available and to not fail if the dependency is not present.

@EdouardCourty
Copy link
Author

EdouardCourty commented Dec 20, 2024

That approach would work, I simply thought it could be a cool feature to have in the bundle, and could only be enabled if the EventDispatcher is available.

I don't have time to submit a PR for now, I'll do it if I ever find the time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants