@@ -552,10 +552,10 @@ random data for your factories:
552
552
faker :
553
553
service : my_faker # service id for your own instance of Faker\Generator
554
554
555
- Events / Hooks
556
- ~~~~~~~~~~~~~~
555
+ Hooks
556
+ ~~~~~
557
557
558
- The following events can be added to factories. Multiple event callbacks can be added, they are run in the order
558
+ The following events can be added to factories. Multiple hooks callbacks can be added, they are run in the order
559
559
they were added.
560
560
561
561
::
@@ -564,28 +564,28 @@ they were added.
564
564
use Zenstruck\Foundry\Proxy;
565
565
566
566
PostFactory::new()
567
- ->beforeInstantiate(function(array $attributes , string $class, static $factory): array {
568
- // $attributes is what will be used to instantiate the object, manipulate as required
567
+ ->beforeInstantiate(function(array $parameters , string $class, static $factory): array {
568
+ // $parameters is what will be used to instantiate the object, manipulate as required
569
569
// $class is the class of the object being instantiated
570
570
// $factory is the factory instance which creates the object
571
- $attributes ['title'] = 'Different title';
571
+ $parameters ['title'] = 'Different title';
572
572
573
- return $attributes ; // must return the final $attributes
573
+ return $parameters ; // must return the final $parameters
574
574
})
575
- ->afterInstantiate(function(Post $object, array $attributes , static $factory): void {
575
+ ->afterInstantiate(function(Post $object, array $parameters , static $factory): void {
576
576
// $object is the instantiated object
577
- // $attributes contains the attributes used to instantiate the object and any extras
577
+ // $parameters contains the attributes used to instantiate the object and any extras
578
578
// $factory is the factory instance which creates the object
579
579
})
580
- ->afterPersist(function(Post $object, array $attributes , static $factory) {
580
+ ->afterPersist(function(Post $object, array $parameters , static $factory) {
581
581
// this event is only called if the object was persisted
582
582
// $object is the persisted Post object
583
- // $attributes contains the attributes used to instantiate the object and any extras
583
+ // $parameters contains the attributes used to instantiate the object and any extras
584
584
// $factory is the factory instance which creates the object
585
585
})
586
586
587
587
// multiple events are allowed
588
- ->beforeInstantiate(function($attributes ) { return $attributes ; })
588
+ ->beforeInstantiate(function($parameters ) { return $parameters ; })
589
589
->afterInstantiate(function() {})
590
590
->afterPersist(function() {})
591
591
;
@@ -603,6 +603,52 @@ You can also add hooks directly in your factory class:
603
603
604
604
Read `Initialization `_ to learn more about the ``initialize() `` method.
605
605
606
+ Events
607
+ ~~~~~~
608
+
609
+ In addition to hooks, Foundry also leverages `symfony/event-dispatcher ` and dispatches events that you can listen to,
610
+ allowing to create hooks globally, as Symfony services:
611
+
612
+ ::
613
+
614
+ use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
615
+ use Zenstruck\Foundry\Object\Event\AfterInstantiate;
616
+ use Zenstruck\Foundry\Object\Event\BeforeInstantiate;
617
+ use Zenstruck\Foundry\Persistence\Event\AfterPersist;
618
+
619
+ final class FoundryEventListener
620
+ {
621
+ #[AsEventListener]
622
+ public function beforeInstantiate(BeforeInstantiate $event): void
623
+ {
624
+ // do something before the object is instantiated:
625
+ // $event->parameters is what will be used to instantiate the object, manipulate as required
626
+ // $event->objectClass is the class of the object being instantiated
627
+ // $event->factory is the factory instance which creates the object
628
+ }
629
+
630
+ #[AsEventListener]
631
+ public function afterInstantiate(AfterInstantiate $event): void
632
+ {
633
+ // $event->object is the instantiated object
634
+ // $event->parameters contains the attributes used to instantiate the object and any extras
635
+ // $event->factory is the factory instance which creates the object
636
+ }
637
+
638
+ #[AsEventListener]
639
+ public function afterPersist(AfterPersist $event): void
640
+ {
641
+ // this event is only called if the object was persisted
642
+ // $event->object is the persisted Post object
643
+ // $event->parameters contains the attributes used to instantiate the object and any extras
644
+ // $event->factory is the factory instance which creates the object
645
+ }
646
+ }
647
+
648
+ .. versionadded :: 2.4
649
+
650
+ Those events are triggered since Foundry 2.4.
651
+
606
652
Initialization
607
653
~~~~~~~~~~~~~~
608
654
0 commit comments