1package Dancer::Hook; 2our $AUTHORITY = 'cpan:SUKRIA'; 3#ABSTRACT: Class to manipulate hooks with Dancer 4$Dancer::Hook::VERSION = '1.3513'; 5use strict; 6use warnings; 7use Carp; 8 9use base 'Dancer::Object'; 10 11__PACKAGE__->attributes(qw/name code properties/); 12 13use Dancer::Factory::Hook; 14use Dancer::Hook::Properties; 15use Dancer::Exception qw(:all); 16 17sub new { 18 my ($class, @args) = @_; 19 20 my $self = bless {}, $class; 21 22 if (!scalar @args) { 23 raise core_hook => "one name and a coderef are required"; 24 } 25 26 my $hook_name = shift @args; 27 28 # XXX at the moment, we have a filer position named "before_template". 29 # this one is renamed "before_template_render", so we need to alias it. 30 # maybe we need to deprecate 'before_template' to enforce the use 31 # of 'hook before_template_render => sub {}' ? 32 $hook_name = 'before_template_render' if $hook_name eq 'before_template'; 33 34 $self->name($hook_name); 35 36 my ( $properties, $code ); 37 if ( scalar @args == 1 ) { 38 $properties = Dancer::Hook::Properties->new(); 39 $code = shift @args; 40 } 41 elsif ( scalar @args == 2 ) { 42 my $prop = shift @args; 43 $properties = Dancer::Hook::Properties->new(%$prop); 44 $code = shift @args; 45 } 46 else { 47 raise core_hook => "something's wrong with parameters passed to Hook constructor"; 48 } 49 ref $code eq 'CODE' 50 or raise core_hook => "the code argument passed to hook construction was not a CodeRef. Value was : '$code'"; 51 52 53 my $compiled_filter = sub { 54 my @arguments = @_; 55 return if Dancer::SharedData->response->halted; 56 57 my $app = Dancer::App->current(); 58 return unless $properties->should_run_this_app($app->name); 59 60 Dancer::Logger::core( "entering " . $hook_name . " hook" ); 61 62 $code->(@arguments); 63 64 }; 65 66 $self->properties($properties); 67 $self->code($compiled_filter); 68 69 Dancer::Factory::Hook->instance->register_hook($self); 70 return $self; 71} 72 731; 74 75__END__ 76 77=pod 78 79=encoding UTF-8 80 81=head1 NAME 82 83Dancer::Hook - Class to manipulate hooks with Dancer 84 85=head1 VERSION 86 87version 1.3513 88 89=head1 SYNOPSIS 90 91 # inside a plugin 92 use Dancer::Hook; 93 Dancer::Hook->register_hooks_name(qw/before_auth after_auth/); 94 95=head1 DESCRIPTION 96 97Manipulate hooks with Dancer 98 99=head1 METHODS 100 101=head2 register_hook ($hook_name, [$properties], $code) 102 103 hook 'before', {apps => ['main']}, sub {...}; 104 105 hook 'before' => sub {...}; 106 107Attaches a hook at some point, with a possible list of properties. 108 109Currently supported properties: 110 111=over 4 112 113=item apps 114 115 an array reference containing apps name 116 117=back 118 119=head2 register_hooks_name 120 121Add a new hook name, so application developers can insert some code at this point. 122 123 package My::Dancer::Plugin; 124 Dancer::Hook->instance->register_hooks_name(qw/before_auth after_auth/); 125 126=head2 hook_is_registered 127 128Test if a hook with this name has already been registered. 129 130=head2 execute_hooks 131 132Execute a list of hooks for some position 133 134=head2 get_hooks_for 135 136Returns the list of coderef registered for a given position 137 138=head1 AUTHORS 139 140This module has been written by Alexis Sukrieh and others. 141 142=head1 LICENSE 143 144This module is free software and is published under the same 145terms as Perl itself. 146 147=head1 AUTHOR 148 149Dancer Core Developers 150 151=head1 COPYRIGHT AND LICENSE 152 153This software is copyright (c) 2010 by Alexis Sukrieh. 154 155This is free software; you can redistribute it and/or modify it under 156the same terms as the Perl 5 programming language system itself. 157 158=cut 159