| 1 | package Dancer::Route::Registry; | ||||||||||||||||||||||||||||
| 2 | 185 | our $AUTHORITY = 'cpan:SUKRIA'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Route registry for Dancer | ||||||||||||||||||||||||||||
| 4 | 185 | $Dancer::Route::Registry::VERSION = '1.3520'; | |||||||||||||||||||||||||||
| 5 | 185 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 185 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 185 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 185 | ▶ | use Dancer::Route; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 185 | ▶ | use base 'Dancer::Object'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 185 | ▶ | use Dancer::Logger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 185 | ▶ | use Dancer::Exception qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 185 | Dancer::Route::Registry->attributes(qw( id )); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 185 | my $id = 1; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | sub init { | ||||||||||||||||||||||||||||
| 18 | 115 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 115 | ▶ | unless (defined $self->{id}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 115 | $self->id($id++); | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # Routes are stored here. Keys are route methods, and values are | ||||||||||||||||||||||||||||
| 25 | # arrays of routes. Routes with options are stored in the | ||||||||||||||||||||||||||||
| 26 | # beginning of the array while routes without options are stored | ||||||||||||||||||||||||||||
| 27 | # at the end. | ||||||||||||||||||||||||||||
| 28 | 115 | $self->{routes} = {}; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # Keep track of the border between routes with and without | ||||||||||||||||||||||||||||
| 31 | # options, so that routes with options can be easily inserted into | ||||||||||||||||||||||||||||
| 32 | # its routes array. | ||||||||||||||||||||||||||||
| 33 | 115 | $self->{routes_border} = {}; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 115 | return $self; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | sub is_empty { | ||||||||||||||||||||||||||||
| 39 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_empty: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | 2 | for my $method ( keys %{ $self->routes } ) { | |||||||||||||||||||||||||||
| 41 | 1 | ▶ | return 0 if $self->routes($method); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 1 | return 1; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # replace any ':foo' by '(.+)' and stores all the named | ||||||||||||||||||||||||||||
| 47 | # matches defined in $REG->{route_params}{$route} | ||||||||||||||||||||||||||||
| 48 | sub routes { | ||||||||||||||||||||||||||||
| 49 | 3474 | ▶ | my ($self, $method) = @_; | ||||||||||||||||||||||||||
|
Subroutine
routes: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 3474 | ▶ | if ($method) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 3472 | my $route = $self->{routes}{$method}; | |||||||||||||||||||||||||||
| 53 | 3472 | ▶ | return $route ? $route : []; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | else { | ||||||||||||||||||||||||||||
| 56 | 2 | return $self->{routes}; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub add_route { | ||||||||||||||||||||||||||||
| 61 | 1442 | ▶ | my ($self, $route) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add_route: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 62 | 1442 | ▶ | $self->{routes}{$route->method} ||= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"routes"}{$route->method} ||= []
MC/DC100%1/1
$self->{"routes"}{$route->method} ||= []
$self->{"routes"}{$route->method}
Truth table100%2/2
A
A= $self->{"routes"}{$route->method}
| |||||||||||||||||||||||||||||
| 63 | 1442 | my @registered = @{$self->routes($route->method)}; | |||||||||||||||||||||||||||
| 64 | 1442 | my $last = $registered[-1]; | |||||||||||||||||||||||||||
| 65 | 1442 | ▶ | $route->set_previous($last) if defined $last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # Routes are stored in the order they are declared. However, | ||||||||||||||||||||||||||||
| 68 | # routes with options (such as ajax routes) are stored before | ||||||||||||||||||||||||||||
| 69 | # routes without options. This way, we can have the following | ||||||||||||||||||||||||||||
| 70 | # routes: | ||||||||||||||||||||||||||||
| 71 | # | ||||||||||||||||||||||||||||
| 72 | # get '/' => sub {}; | ||||||||||||||||||||||||||||
| 73 | # get qr{.*} => sub {}; | ||||||||||||||||||||||||||||
| 74 | # ajax '/' => sub {}; | ||||||||||||||||||||||||||||
| 75 | # ajax qr{.*} => sub {}; | ||||||||||||||||||||||||||||
| 76 | # | ||||||||||||||||||||||||||||
| 77 | # And the user won't have to declare the ajax routes before the | ||||||||||||||||||||||||||||
| 78 | # get routes. | ||||||||||||||||||||||||||||
| 79 | 1442 | ▶ | if (keys %{$route->{options}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 11 | splice @{$self->routes($route->method)}, | |||||||||||||||||||||||||||
| 81 | 11 | $self->{routes_border}{$route->method}++, | |||||||||||||||||||||||||||
| 82 | 0, | ||||||||||||||||||||||||||||
| 83 | $route; | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | else { | ||||||||||||||||||||||||||||
| 86 | 1431 | push @{$self->routes($route->method)}, $route; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1442 | return $route; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # sugar for add_route | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | sub register_route { | ||||||||||||||||||||||||||||
| 95 | 1443 | ▶ | my ($self, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
register_route: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # look if the caller (where the route is declared) exists as a Dancer::App | ||||||||||||||||||||||||||||
| 98 | # object | ||||||||||||||||||||||||||||
| 99 | 1443 | my ($package) = caller(2); | |||||||||||||||||||||||||||
| 100 | 1443 | ▶ | if ($package && Dancer::App->app_exists($package)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$package and "Dancer::App"->app_exists($package)
MC/DC100%2/2
$package and "Dancer::App"->app_exists($package)
$package
"Dancer::App"->app_exists($package)
Truth table100%3/3
A and B
A= $packageB= "Dancer::App"->app_exists($package)
| |||||||||||||||||||||||||||||
| 101 | 655 | my $app = Dancer::App->get($package); | |||||||||||||||||||||||||||
| 102 | 655 | my $route = Dancer::Route->new(prefix => $app->prefix, %args); | |||||||||||||||||||||||||||
| 103 | 654 | return $app->registry->add_route($route); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | else { | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # FIXME maybe this code is useless, drop it later if so | ||||||||||||||||||||||||||||
| 108 | 788 | my $route = Dancer::Route->new(%args); | |||||||||||||||||||||||||||
| 109 | 788 | return $self->add_route($route); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # sugar for Dancer.pm | ||||||||||||||||||||||||||||
| 114 | # class, any, ARRAY(0x9864818), '/path', CODE(0x990ac88) | ||||||||||||||||||||||||||||
| 115 | # or | ||||||||||||||||||||||||||||
| 116 | # class, any, '/path', CODE(0x990ac88) | ||||||||||||||||||||||||||||
| 117 | sub any_add { | ||||||||||||||||||||||||||||
| 118 | 24 | ▶ | my ($self, $pattern, @rest) = @_; | ||||||||||||||||||||||||||
|
Subroutine
any_add: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 24 | my @methods = qw(get post put patch delete options head); | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 24 | ▶ | if (ref($pattern) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 19 | @methods = @$pattern; | |||||||||||||||||||||||||||
| 124 | # 'get' defaults to 'get' and 'head' | ||||||||||||||||||||||||||||
| 125 | 38 | push @methods, 'head' if ((grep { $_ eq 'get' } @methods) and | |||||||||||||||||||||||||||
| 126 | 19 | ▶ | not (grep { $_ eq 'head' } @methods)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
grep {$_ eq "get";} @methods and not grep({$_ eq "head";} @methods)
MC/DC50%1/2
grep {$_ eq "get";} @methods and not grep({$_ eq "head";} @methods)
grep {$_ eq "get";} @methods
not grep({$_ eq "head";} @methods)
Truth table66%2/3
A and not(B)
A= grep {$_ eq "get";} @methodsB= not grep({$_ eq "head";} @methods)
| |||||||||||||||||||||||||||||
| 127 | 19 | $pattern = shift @rest; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | raise core_route => "Syntax error, methods should be provided as an ARRAY ref" | ||||||||||||||||||||||||||||
| 131 | 24 | ▶ | if grep {$_ eq $pattern} @methods; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 23 | $self->universal_add($_, $pattern, @rest) for @methods; | |||||||||||||||||||||||||||
| 134 | 23 | return scalar(@methods); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | sub universal_add { | ||||||||||||||||||||||||||||
| 138 | 1443 | ▶ | my ($self, $method, $pattern, @rest) = @_; | ||||||||||||||||||||||||||
|
Subroutine
universal_add: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 1443 | my %options; | |||||||||||||||||||||||||||
| 141 | my $code; | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 1443 | ▶ | if (@rest == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 1431 | $code = $rest[0]; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | else { | ||||||||||||||||||||||||||||
| 147 | 12 | %options = %{$rest[0]}; | |||||||||||||||||||||||||||
| 148 | 12 | $code = $rest[1]; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 1443 | my %route_args = ( | |||||||||||||||||||||||||||
| 152 | method => $method, | ||||||||||||||||||||||||||||
| 153 | code => $code, | ||||||||||||||||||||||||||||
| 154 | options => \%options, | ||||||||||||||||||||||||||||
| 155 | pattern => $pattern, | ||||||||||||||||||||||||||||
| 156 | ); | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 1443 | return $self->register_route(%route_args); | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # look for a route in the given array | ||||||||||||||||||||||||||||
| 162 | sub find_route { | ||||||||||||||||||||||||||||
| 163 | 0 | ▶ | my ($self, $r, $reg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
find_route: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 164 | 0 | foreach my $route (@$reg) { | |||||||||||||||||||||||||||
| 165 | 0 | ▶ | return $route if $r->equals($route); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | 0 | return; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 185 | 1; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | __END__ | ||||||||||||||||||||||||||||