| 1 |
package Dancer2::Core::Role::Hookable; |
| 2 |
# ABSTRACT: Role for hookable objects |
| 3 |
$Dancer2::Core::Role::Hookable::VERSION = '2.2.2'; |
| 4 |
use Moo::Role; |
| 5 |
use Dancer2::Core; |
| 6 |
use Dancer2::Core::Types; |
| 7 |
use Carp 'croak'; |
| 8 |
use Safe::Isa; |
| 9 |
use Sub::Util qw/ subname /; |
| 10 |
|
| 11 |
requires 'supported_hooks', 'hook_aliases'; |
| 12 |
|
| 13 |
# The hooks registry |
| 14 |
has hooks => ( |
| 15 |
is => 'ro', |
| 16 |
isa => HashRef, |
| 17 |
builder => '_build_hooks', |
| 18 |
lazy => 1, |
| 19 |
); |
| 20 |
|
| 21 |
sub BUILD { } |
| 22 |
|
| 23 |
# after a hookable object is built, we go over its postponed hooks and register |
| 24 |
# them if any. |
| 25 |
after BUILD => sub { |
| 26 |
my ( $self, $args ) = @_; |
| 27 |
$self->_add_postponed_hooks($args) |
| 28 |
if defined $args->{postponed_hooks}; |
| 29 |
}; |
| 30 |
|
| 31 |
sub _add_postponed_hooks { |
| 32 |
my ( $self, $args ) = @_; |
| 33 |
my $postponed_hooks = $args->{postponed_hooks}; |
| 34 |
|
| 35 |
# find the internal name of the hooks, from the caller name |
| 36 |
my $caller = ref($self); |
| 37 |
my ( $dancer, $h_type, $h_name, @rest ) = map lc, split /::/, $caller; |
| 38 |
$h_name = $rest[0] if $h_name eq 'role'; |
| 39 |
if ( $h_type =~ /(template|logger|serializer|session)/ ) { |
| 40 |
$h_name = $h_type; |
| 41 |
$h_type = 'engine'; |
| 42 |
} |
| 43 |
|
| 44 |
# keep only the hooks we want |
| 45 |
$postponed_hooks = $postponed_hooks->{$h_type}{$h_name}; |
| 46 |
return unless defined $postponed_hooks; |
| 47 |
|
| 48 |
foreach my $name ( keys %{$postponed_hooks} ) { |
| 49 |
my $hook = $postponed_hooks->{$name}{hook}; |
| 50 |
my $caller = $postponed_hooks->{$name}{caller}; |
| 51 |
|
| 52 |
$self->has_hook($name) |
| 53 |
or croak "$h_name $h_type does not support the hook `$name'. (" |
| 54 |
. join( ", ", @{$caller} ) . ")"; |
| 55 |
|
| 56 |
$self->add_hook($hook); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
# mst++ for the hint |
| 61 |
sub _build_hooks { |
| 62 |
my ($self) = @_; |
| 63 |
my %hooks = map +( $_ => [] ), $self->supported_hooks; |
| 64 |
return \%hooks; |
| 65 |
} |
| 66 |
|
| 67 |
# This binds a coderef to an installed hook if not already |
| 68 |
# existing |
| 69 |
sub add_hook { |
| 70 |
my ( $self, $hook ) = @_; |
| 71 |
my $name = $hook->name; |
| 72 |
my $code = $hook->code; |
| 73 |
|
| 74 |
croak "Unsupported hook '$name'" |
| 75 |
unless $self->has_hook($name); |
| 76 |
|
| 77 |
push @{ $self->hooks->{$name} }, $code; |
| 78 |
} |
| 79 |
|
| 80 |
# allows the caller to replace the current list of hooks at the given position |
| 81 |
# this is useful if the object where this role is composed wants to compile the |
| 82 |
# hooks. |
| 83 |
sub replace_hook { |
| 84 |
my ( $self, $position, $hooks ) = @_; |
| 85 |
|
| 86 |
croak "Hook '$position' must be installed first" |
| 87 |
unless $self->has_hook($position); |
| 88 |
|
| 89 |
$self->hooks->{$position} = $hooks; |
| 90 |
} |
| 91 |
|
| 92 |
# Boolean flag to tells if the hook is registered or not |
| 93 |
sub has_hook { |
| 94 |
my ( $self, $hook_name ) = @_; |
| 95 |
return exists $self->hooks->{$hook_name}; |
| 96 |
} |
| 97 |
|
| 98 |
# Execute the hook at the given position |
| 99 |
sub execute_hook { |
| 100 |
my $self = shift; |
| 101 |
my $name = shift; |
| 102 |
|
| 103 |
$name and !ref $name |
| 104 |
or croak "execute_hook needs a hook name"; |
| 105 |
|
| 106 |
$name = $self->hook_aliases->{$name} |
| 107 |
if exists $self->hook_aliases->{$name}; |
| 108 |
|
| 109 |
croak "Hook '$name' does not exist" |
| 110 |
if !$self->has_hook($name); |
| 111 |
|
| 112 |
$self->$_isa('Dancer2::Core::App') && |
| 113 |
$self->log( core => "Entering hook $name" ); |
| 114 |
|
| 115 |
for my $hook ( @{ $self->hooks->{$name} } ) { |
| 116 |
|
| 117 |
$self->log( core => "running hook entry " . subname $hook) |
| 118 |
if $self->$_isa('Dancer2::Core::App'); |
| 119 |
|
| 120 |
$hook->(@_); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
1; |
| 125 |
|
| 126 |
__END__ |
| 127 |
|
| 128 |
=pod |
| 129 |
|
| 130 |
=encoding UTF-8 |
| 131 |
|
| 132 |
=head1 NAME |
| 133 |
|
| 134 |
Dancer2::Core::Role::Hookable - Role for hookable objects |
| 135 |
|
| 136 |
=head1 VERSION |
| 137 |
|
| 138 |
version 2.2.2 |
| 139 |
|
| 140 |
=head1 AUTHOR |
| 141 |
|
| 142 |
Dancer Core Developers |
| 143 |
|
| 144 |
=head1 COPYRIGHT AND LICENSE |
| 145 |
|
| 146 |
This software is copyright (c) 2026 by Alexis Sukrieh. |
| 147 |
|
| 148 |
This is free software; you can redistribute it and/or modify it under |
| 149 |
the same terms as the Perl 5 programming language system itself. |
| 150 |
|
| 151 |
=cut |