| 1 | package Dancer2::Core::Role::Hookable; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Role for hookable objects | ||||||||||||||||||||||||||||
| 3 | 227 | $Dancer2::Core::Role::Hookable::VERSION = '2.3.0'; | |||||||||||||||||||||||||||
| 4 | 227 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 227 | ▶ | use Dancer2::Core; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 227 | ▶ | use Dancer2::Core::Types; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 227 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 227 | ▶ | use Safe::Isa; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 227 | ▶ | use Sub::Util qw/ subname /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 227 | requires 'supported_hooks', 'hook_aliases'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # The hooks registry | ||||||||||||||||||||||||||||
| 14 | 227 | has hooks => ( | |||||||||||||||||||||||||||
| 15 | is => 'ro', | ||||||||||||||||||||||||||||
| 16 | isa => HashRef, | ||||||||||||||||||||||||||||
| 17 | builder => '_build_hooks', | ||||||||||||||||||||||||||||
| 18 | lazy => 1, | ||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1146 | ▶ | sub BUILD { } | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # after a hookable object is built, we go over its postponed hooks and register | ||||||||||||||||||||||||||||
| 24 | # them if any. | ||||||||||||||||||||||||||||
| 25 | 1477 | ▶ | after BUILD => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 26 | 1477 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 27 | $self->_add_postponed_hooks($args) | ||||||||||||||||||||||||||||
| 28 | 1477 | ▶ | if defined $args->{postponed_hooks}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 227 | }; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 933 | ▶ | sub _add_postponed_hooks { | ||||||||||||||||||||||||||
|
Subroutine
_add_postponed_hooks: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 32 | 933 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 33 | 933 | my $postponed_hooks = $args->{postponed_hooks}; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # find the internal name of the hooks, from the caller name | ||||||||||||||||||||||||||||
| 36 | 933 | my $caller = ref($self); | |||||||||||||||||||||||||||
| 37 | 933 | my ( $dancer, $h_type, $h_name, @rest ) = map lc, split /::/, $caller; | |||||||||||||||||||||||||||
| 38 | 933 | ▶ | $h_name = $rest[0] if $h_name eq 'role'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 933 | ▶ | if ( $h_type =~ /(template|logger|serializer|session)/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 682 | $h_name = $h_type; | |||||||||||||||||||||||||||
| 41 | 682 | $h_type = 'engine'; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # keep only the hooks we want | ||||||||||||||||||||||||||||
| 45 | 933 | $postponed_hooks = $postponed_hooks->{$h_type}{$h_name}; | |||||||||||||||||||||||||||
| 46 | 933 | ▶ | return unless defined $postponed_hooks; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 10 | foreach my $name ( keys %{$postponed_hooks} ) { | |||||||||||||||||||||||||||
| 49 | 11 | my $hook = $postponed_hooks->{$name}{hook}; | |||||||||||||||||||||||||||
| 50 | 11 | 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 | 11 | ▶ | . join( ", ", @{$caller} ) . ")"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 10 | $self->add_hook($hook); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # mst++ for the hint | ||||||||||||||||||||||||||||
| 61 | 558 | ▶ | sub _build_hooks { | ||||||||||||||||||||||||||
|
Subroutine
_build_hooks: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | 558 | my ($self) = @_; | |||||||||||||||||||||||||||
| 63 | 558 | my %hooks = map +( $_ => [] ), $self->supported_hooks; | |||||||||||||||||||||||||||
| 64 | 558 | return \%hooks; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # This binds a coderef to an installed hook if not already | ||||||||||||||||||||||||||||
| 68 | # existing | ||||||||||||||||||||||||||||
| 69 | 424 | ▶ | sub add_hook { | ||||||||||||||||||||||||||
|
Subroutine
add_hook: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | 424 | my ( $self, $hook ) = @_; | |||||||||||||||||||||||||||
| 71 | 424 | my $name = $hook->name; | |||||||||||||||||||||||||||
| 72 | 424 | my $code = $hook->code; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 424 | ▶ | croak "Unsupported hook '$name'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | unless $self->has_hook($name); | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 423 | 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 | 2595 | ▶ | sub replace_hook { | ||||||||||||||||||||||||||
|
Subroutine
replace_hook: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 84 | 2595 | my ( $self, $position, $hooks ) = @_; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 2595 | ▶ | croak "Hook '$position' must be installed first" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | unless $self->has_hook($position); | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 2594 | $self->hooks->{$position} = $hooks; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # Boolean flag to tells if the hook is registered or not | ||||||||||||||||||||||||||||
| 93 | 28840 | ▶ | sub has_hook { | ||||||||||||||||||||||||||
|
Subroutine
has_hook: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 94 | 28840 | my ( $self, $hook_name ) = @_; | |||||||||||||||||||||||||||
| 95 | 28840 | return exists $self->hooks->{$hook_name}; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # Execute the hook at the given position | ||||||||||||||||||||||||||||
| 99 | 23211 | ▶ | sub execute_hook { | ||||||||||||||||||||||||||
|
Subroutine
execute_hook: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 100 | 23211 | my $self = shift; | |||||||||||||||||||||||||||
| 101 | 23211 | my $name = shift; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 23211 | ▶ | $name and !ref $name | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$name and not ref $name
MC/DC50%1/2
$name and not ref $name
$name
not ref $name
Truth table66%2/3
A and not(B)
A= $nameB= not ref $name
| |||||||||||||||||||||||||||||
| 104 | or croak "execute_hook needs a hook name"; | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | $name = $self->hook_aliases->{$name} | ||||||||||||||||||||||||||||
| 107 | 23210 | ▶ | if exists $self->hook_aliases->{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 23210 | ▶ | croak "Hook '$name' does not exist" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | if !$self->has_hook($name); | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 23210 | ▶ | $self->$_isa('Dancer2::Core::App') && | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | $self->log( core => "Entering hook $name" ); | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 23210 | for my $hook ( @{ $self->hooks->{$name} } ) { | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 834 | ▶ | $self->log( core => "running hook entry " . subname $hook) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | if $self->$_isa('Dancer2::Core::App'); | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 834 | $hook->(@_); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 227 | 1; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | __END__ | ||||||||||||||||||||||||||||