| 1 | package Dancer::Hook; | ||||||||||||||
| 2 | 171 | our $AUTHORITY = 'cpan:SUKRIA'; | |||||||||||||
| 3 | #ABSTRACT: Class to manipulate hooks with Dancer | ||||||||||||||
| 4 | 171 | $Dancer::Hook::VERSION = '1.3520'; | |||||||||||||
| 5 | 171 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 171 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 171 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 171 | ▶ | use base 'Dancer::Object'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 171 | __PACKAGE__->attributes(qw/name code properties/); | |||||||||||||
| 12 | |||||||||||||||
| 13 | 171 | ▶ | use Dancer::Factory::Hook; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 171 | ▶ | use Dancer::Hook::Properties; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 171 | ▶ | use Dancer::Exception qw(:all); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | |||||||||||||||
| 17 | sub new { | ||||||||||||||
| 18 | 134 | ▶ | my ($class, @args) = @_; | ||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.8
Pod
documented
| |||||||||||||||
| 19 | |||||||||||||||
| 20 | 134 | my $self = bless {}, $class; | |||||||||||||
| 21 | |||||||||||||||
| 22 | 134 | ▶ | if (!scalar @args) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 23 | 0 | raise core_hook => "one name and a coderef are required"; | |||||||||||||
| 24 | } | ||||||||||||||
| 25 | |||||||||||||||
| 26 | 134 | 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 | 134 | ▶ | $hook_name = 'before_template_render' if $hook_name eq 'before_template'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 33 | |||||||||||||||
| 34 | 134 | $self->name($hook_name); | |||||||||||||
| 35 | |||||||||||||||
| 36 | 134 | my ( $properties, $code ); | |||||||||||||
| 37 | 134 | ▶ | if ( scalar @args == 1 ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 38 | 134 | $properties = Dancer::Hook::Properties->new(); | |||||||||||||
| 39 | 134 | $code = shift @args; | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | elsif ( scalar @args == 2 ) { | ||||||||||||||
| 42 | 0 | my $prop = shift @args; | |||||||||||||
| 43 | 0 | $properties = Dancer::Hook::Properties->new(%$prop); | |||||||||||||
| 44 | 0 | $code = shift @args; | |||||||||||||
| 45 | } | ||||||||||||||
| 46 | else { | ||||||||||||||
| 47 | 0 | raise core_hook => "something's wrong with parameters passed to Hook constructor"; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | 134 | ▶ | ref $code eq 'CODE' | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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 | 299 | ▶ | my @arguments = @_; | ||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||
| 55 | 299 | ▶ | return if Dancer::SharedData->response->halted; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | |||||||||||||||
| 57 | 298 | my $app = Dancer::App->current(); | |||||||||||||
| 58 | 298 | ▶ | return unless $properties->should_run_this_app($app->name); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | |||||||||||||||
| 60 | 298 | Dancer::Logger::core( "entering " . $hook_name . " hook" ); | |||||||||||||
| 61 | |||||||||||||||
| 62 | 298 | $code->(@arguments); | |||||||||||||
| 63 | |||||||||||||||
| 64 | 133 | }; | |||||||||||||
| 65 | |||||||||||||||
| 66 | 133 | $self->properties($properties); | |||||||||||||
| 67 | 133 | $self->code($compiled_filter); | |||||||||||||
| 68 | |||||||||||||||
| 69 | 133 | Dancer::Factory::Hook->instance->register_hook($self); | |||||||||||||
| 70 | 133 | return $self; | |||||||||||||
| 71 | } | ||||||||||||||
| 72 | |||||||||||||||
| 73 | 171 | 1; | |||||||||||||
| 74 | |||||||||||||||
| 75 | __END__ | ||||||||||||||