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

feat: introduce hook services #790

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open

Conversation

nikophil
Copy link
Member

No description provided.

@nikophil nikophil force-pushed the hooks branch 2 times, most recently from fab6978 to 2b0f3a1 Compare January 18, 2025 16:32

namespace Zenstruck\Foundry\Hooks;

#[\Attribute(\Attribute::TARGET_CLASS)]
Copy link
Member Author

@nikophil nikophil Jan 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in a first time, I think this is enough.
Maybe in a second time, if someone needs it, we could implement TARGET_METHOD and IS_REPEATABLE, but this will complexify a lot the DI

Comment on lines +343 to +350
foreach ($container->findTaggedServiceIds('foundry.hook') as $id => $tags) {
$index = isset($tags[0]['class'])
? HooksRegistry::hookClassSpecificIndex($tags[0]['hook'], $tags[0]['class'])
: $tags[0]['hook'];

$iterators[$index] ??= [];
$iterators[$index][] = new Reference($id);
}
Copy link
Member Author

@nikophil nikophil Jan 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're kinda missing a priority here. But that's not straightforward to do, because "global hook" and "hook specific to a class" are not in the same iterator.

I propose to leave this like this, and see if someone ever asks for it :)

Comment on lines +20 to +30
#[AsAfterInstantiateFoundryHook(class: EntityForHooks::class)]
final class ObjectAfterInstantiateHook
{
/**
* @param AfterInstantiate<EntityForHooks> $hook
*/
public function __invoke(AfterInstantiate $hook): void
{
$hook->object->name = "{$hook->object->name}\nObjectAfterInstantiateHook";
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here is an example of a hook.

another syntax is possible:

#[AsFoundryHook(event: AfterInstantiate::class(, class: EntityForHooks::class))]
final class AddressAfterInstantiateHook
{
    public function __invoke(AfterInstantiate $hook): void;
}

it would be clear then, that you should pass a AfterInstantiate to the __invoke() method.

I also wondered if we should not introduce an interface, but, that would mean 3 interfaces. And an attribute AND an interface seems redundant.

WDYT?

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

Successfully merging this pull request may close these issues.

1 participant