@@ -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,51 @@ 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 dispatches events that you can listen to, allowing to create hooks globally:
610
+
611
+ ::
612
+
613
+ use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
614
+ use Zenstruck\Foundry\Object\Event\AfterInstantiate;
615
+ use Zenstruck\Foundry\Object\Event\BeforeInstantiate;
616
+ use Zenstruck\Foundry\Persistence\Event\AfterPersist;
617
+
618
+ final class FoundryEventListener
619
+ {
620
+ #[AsEventListener]
621
+ public function beforeInstantiate(BeforeInstantiate $event): void
622
+ {
623
+ // do something before the object is instantiated:
624
+ // $event->parameters is what will be used to instantiate the object, manipulate as required
625
+ // $event->objectClass is the class of the object being instantiated
626
+ // $event->factory is the factory instance which creates the object
627
+ }
628
+
629
+ #[AsEventListener]
630
+ public function afterInstantiate(AfterInstantiate $event): void
631
+ {
632
+ // $event->object is the instantiated object
633
+ // $event->parameters contains the attributes used to instantiate the object and any extras
634
+ // $event->factory is the factory instance which creates the object
635
+ }
636
+
637
+ #[AsEventListener]
638
+ public function afterPersist(AfterPersist $event): void
639
+ {
640
+ // this event is only called if the object was persisted
641
+ // $event->object is the persisted Post object
642
+ // $event->parameters contains the attributes used to instantiate the object and any extras
643
+ // $event->factory is the factory instance which creates the object
644
+ }
645
+ }
646
+
647
+ .. versionadded :: 2.4
648
+
649
+ Those events are triggered since Foundry 2.4.
650
+
606
651
Initialization
607
652
~~~~~~~~~~~~~~
608
653
0 commit comments