| 1 | 4 | ▶ | use 5.008005; use strict; use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package Catalyst::Plugin::CachedUriForAction; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 4 | our $VERSION = '1.005'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use mro; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use URI::Encode::XS 'uri_encode_utf8'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | sub CACHE_KEY () { __PACKAGE__ . '::action_uri_info' } | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | sub setup_finalize { | ||||||||||||||||||||||||||
|
Subroutine
setup_finalize: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 14 | 3 | my $c = shift; | |||||||||||||||||||||||||||
| 15 | 3 | $c->maybe::next::method( @_ ); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | my $cache = \%{ $c->dispatcher->{(CACHE_KEY)} }; | |||||||||||||||||||||||||||
| 18 | 3 | for my $action ( values %{ $c->dispatcher->_action_hash } ) { | |||||||||||||||||||||||||||
| 19 | 174 | my $xa = $c->dispatcher->expand_action( $action ); | |||||||||||||||||||||||||||
| 20 | 174 | my $n_caps = $xa->number_of_captures; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # not an action that a request can be dispatched to? | ||||||||||||||||||||||||||||
| 23 | 174 | ▶ | next if not defined $c->dispatcher->uri_for_action( $action, [ ('dummy') x $n_caps ] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 48 | my $n_args = $xa->number_of_args; # might be undef to mean "any number" | |||||||||||||||||||||||||||
| 26 | 48 | ▶ | my $tmpl = $c->uri_for( $action, [ ("\0\0\0\0") x $n_caps ], ("\0\0\0\0") x ( $n_args || 0 ) ); | ||||||||||||||||||||||||||
|
Condition100%2/2
$n_args || 0
MC/DC100%1/1
$n_args || 0
$n_args
Truth table100%2/2
A
A= $n_args
| |||||||||||||||||||||||||||||
| 27 | 48 | my ( $prefix, @part ) = split /%00%00%00%00/, $tmpl, -1; | |||||||||||||||||||||||||||
| 28 | 48 | $prefix =~ s!\A/!!; | |||||||||||||||||||||||||||
| 29 | 48 | $cache->{ '/' . $action->reverse } = [ $n_caps, $n_args, \@part, $prefix ]; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 57 | ▶ | sub uri_for_action { | ||||||||||||||||||||||||||
|
Subroutine
uri_for_action: called CC=34 SCAR=35.4
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 57 | my $c = shift; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 57 | my $dispatcher = $c->dispatcher; | |||||||||||||||||||||||||||
| 37 | 57 | ▶ | my $cache = $dispatcher && $dispatcher->{(CACHE_KEY)} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$dispatcher && $dispatcher->{'Catalyst::Plugin::CachedUriForAction::action_uri_info'}
MC/DC0%0/2
$dispatcher && $dispatcher->{'Catalyst::Plugin::CachedUriForAction::action_uri_info'}
$dispatcher
$dispatcher->{'Catalyst::Plugin::CachedUriForAction::action_uri_info'}
Truth table33%1/3
A && B
A= $dispatcherB= $dispatcher->{'Catalyst::Plugin::CachedUriForAction::action_uri_info'}
| |||||||||||||||||||||||||||||
| 38 | or return $c->next::method( @_ ); # fall back if called too early | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 57 | my $action = shift; | |||||||||||||||||||||||||||
| 41 | 57 | ▶ | my $captures = @_ && 'ARRAY' eq ref $_[0] ? shift : []; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ and "ARRAY" eq ref $_[0]
MC/DC100%2/2
@_ and "ARRAY" eq ref $_[0]
@_
"ARRAY" eq ref $_[0]
Truth table100%3/3
A and B
A= @_B= "ARRAY" eq ref $_[0]
| |||||||||||||||||||||||||||||
| 42 | 57 | ▶ | my $fragment = @_ && 'SCALAR' eq ref $_[-1] ? pop : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ and "SCALAR" eq ref $_[-1]
MC/DC100%2/2
@_ and "SCALAR" eq ref $_[-1]
@_
"SCALAR" eq ref $_[-1]
Truth table100%3/3
A and B
A= @_B= "SCALAR" eq ref $_[-1]
| |||||||||||||||||||||||||||||
| 43 | 57 | ▶ | my $params = @_ && 'HASH' eq ref $_[-1] ? pop : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ and "HASH" eq ref $_[-1]
MC/DC100%2/2
@_ and "HASH" eq ref $_[-1]
@_
"HASH" eq ref $_[-1]
Truth table100%3/3
A and B
A= @_B= "HASH" eq ref $_[-1]
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | $action = '/' . $dispatcher->get_action_by_path( $action )->reverse | ||||||||||||||||||||||||||||
| 46 | if ref $action | ||||||||||||||||||||||||||||
| 47 | 57 | ▶ | and do { local $@; eval { $action->isa( 'Catalyst::Action' ) } }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $action and do {
local $@;
eval {
do {
$action->isa("Catalyst::Action")
}
}
}
MC/DC100%2/2
ref $action and do {
local $@;
eval {
do {
$action->isa("Catalyst::Action")
}
}
}
ref $action
do {
local $@;
eval {
do {
$action->isa("Catalyst::Action")
}
}
}
Truth table100%3/3
A and B
A= ref $actionB= do {
local $@;
eval {
do {
$action->isa("Catalyst::Action")
}
}
}
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 57 | ▶ | my $info = $cache->{ $action } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | or Carp::croak "Can't find action for path '$action' in uri_for_action"; | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 55 | my ( $uri, $base ) = ''; | |||||||||||||||||||||||||||
| 53 | 55 | ▶ | if ( ref $c ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 41 | $base = $c->request->base; | |||||||||||||||||||||||||||
| 55 | 41 | ▶ | $uri = '/' if $$base !~ m!/\z!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | } else { # fallback if called as class method | ||||||||||||||||||||||||||||
| 57 | 14 | $base = bless \( my $tmp = '' ), 'URI::_generic'; | |||||||||||||||||||||||||||
| 58 | 14 | $uri = '/'; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 55 | my ( $n_caps, $n_args, $extra_parts ) = @$info; | |||||||||||||||||||||||||||
| 62 | 55 | $uri .= $info->[-1]; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # this is not very sensical but it has to be like this because it is what Catalyst does: | ||||||||||||||||||||||||||||
| 65 | # the :Args() case (i.e. any number of args) is grouped with the :Args(0) case (i.e. no args) | ||||||||||||||||||||||||||||
| 66 | # instead of being grouped with with the :Args(N) case (i.e. a fixed non-zero number of args) | ||||||||||||||||||||||||||||
| 67 | 55 | ▶ | if ( $n_args ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 29 | ▶ | Carp::croak "Not enough captures for path '$action' (need $n_caps) in uri_for_action" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | if @$captures < $n_caps; | ||||||||||||||||||||||||||||
| 70 | } else { | ||||||||||||||||||||||||||||
| 71 | 26 | ▶ | Carp::croak "Wrong number of captures for path '$action' (need $n_caps) in uri_for_action" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | if @$captures != $n_caps; | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # the following is carefully written to | ||||||||||||||||||||||||||||
| 76 | # - loop over every input array exactly once | ||||||||||||||||||||||||||||
| 77 | # - avoid any conditionals inside each loop body | ||||||||||||||||||||||||||||
| 78 | # - use only simple loop forms that are specially optimised by the perl interpreter | ||||||||||||||||||||||||||||
| 79 | 51 | my $i = -1; | |||||||||||||||||||||||||||
| 80 | 51 | ▶ | if ( defined $n_args ) { # the non-slurpy case | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 32 | ▶ | Carp::croak "Wrong number of args+captures for path '$action' (need ".@$extra_parts.") in uri_for_action" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | if ( @$captures + @_ ) != @$extra_parts; | ||||||||||||||||||||||||||||
| 83 | # and now since @$extra_parts is exactly the same length as @$captures and @_ combined | ||||||||||||||||||||||||||||
| 84 | # iterate over those arrays and use a cursor into @$extra_parts to interleave its elements | ||||||||||||||||||||||||||||
| 85 | 26 | for ( @$captures ) { ( $uri .= uri_encode_utf8 $_ ) .= $extra_parts->[ ++$i ] } | |||||||||||||||||||||||||||
| 86 | 26 | for ( @_ ) { ( $uri .= uri_encode_utf8 $_ ) .= $extra_parts->[ ++$i ] } | |||||||||||||||||||||||||||
| 87 | } else { | ||||||||||||||||||||||||||||
| 88 | # in the slurpy case, the size of @$extra_parts is determined by $n_caps alone since $n_args was undef | ||||||||||||||||||||||||||||
| 89 | # and as we checked above @$captures alone has at least length $n_caps | ||||||||||||||||||||||||||||
| 90 | # so we will need all of @$captures to cover @$extra_parts, and may then still have some of it left over | ||||||||||||||||||||||||||||
| 91 | # so iterate over @$extra_parts and use a cursor into @$captures to interleave its elements | ||||||||||||||||||||||||||||
| 92 | 0 | for ( @$extra_parts ) { ( $uri .= uri_encode_utf8 $captures->[ ++$i ] ) .= $_ } | |||||||||||||||||||||||||||
| 93 | # and then append the rest of @$captures, and then everything from @_ after that | ||||||||||||||||||||||||||||
| 94 | 0 | for ( ++$i .. $#$captures ) { ( $uri .= '/' ) .= uri_encode_utf8 $captures->[ $_ ] } | |||||||||||||||||||||||||||
| 95 | 19 | for ( @_ ) { ( $uri .= '/' ) .= uri_encode_utf8 $_ } | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 45 | $uri =~ s/%2B/+/g; | |||||||||||||||||||||||||||
| 99 | 45 | substr $uri, 0, 0, $$base; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 45 | ▶ | if ( defined $params ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 20 | my $query = ''; | |||||||||||||||||||||||||||
| 103 | 20 | ▶ | my $delim = $URI::DEFAULT_QUERY_FORM_DELIMITER || '&'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$URI::DEFAULT_QUERY_FORM_DELIMITER || '&'
MC/DC0%0/1
$URI::DEFAULT_QUERY_FORM_DELIMITER || '&'
$URI::DEFAULT_QUERY_FORM_DELIMITER
Truth table50%1/2
A
A= $URI::DEFAULT_QUERY_FORM_DELIMITER
| |||||||||||||||||||||||||||||
| 104 | 20 | my ( $v, $enc_key ); | |||||||||||||||||||||||||||
| 105 | 20 | for my $key ( sort keys %$params ) { | |||||||||||||||||||||||||||
| 106 | 21 | $v = $params->{ $key }; | |||||||||||||||||||||||||||
| 107 | 21 | ▶ | if ( 'ARRAY' ne ref $v ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 108 | 20 | ( $query .= $delim ) .= uri_encode_utf8 $key; | |||||||||||||||||||||||||||
| 109 | 20 | ▶ | ( $query .= '=' ) .= uri_encode_utf8 $v if defined $v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | } elsif ( @$v ) { | ||||||||||||||||||||||||||||
| 111 | 1 | $enc_key = $delim . uri_encode_utf8 $key; | |||||||||||||||||||||||||||
| 112 | 1 | for ( @$v ) { | |||||||||||||||||||||||||||
| 113 | 2 | $query .= $enc_key; | |||||||||||||||||||||||||||
| 114 | 2 | ▶ | ( $query .= '=' ) .= uri_encode_utf8 $_ if defined; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | 20 | ▶ | if ( '' ne $query ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 20 | $query =~ s/%20/+/g; | |||||||||||||||||||||||||||
| 120 | 20 | ( $uri .= '?' ) .= substr $query, length $delim; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 45 | ▶ | if ( defined $fragment ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 2 | ( $uri .= '#' ) .= uri_encode_utf8 $$fragment; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 45 | bless \$uri, ref $base; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 4 | ▶ | BEGIN { delete $Catalyst::Plugin::CachedUriForAction::{'uri_encode_utf8'} } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 4 | 1; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | __END__ | ||||||||||||||||||||||||||||