| 1 | package Slick::RouteMap; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.036; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Types::Standard qw(HashRef); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Slick::Methods qw(METHODS); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Slick::Events qw(EVENTS); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use List::Util qw(first); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | has _map => ( | ||||||||||||||||||||||||||||
| 14 | is => 'ro', | ||||||||||||||||||||||||||||
| 15 | isa => HashRef, | ||||||||||||||||||||||||||||
| 16 | 2 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | return { | ||||||||||||||||||||||||||||
| 18 | 2 | '/' => { | |||||||||||||||||||||||||||
| 19 | children => {}, | ||||||||||||||||||||||||||||
| 20 | methods => {} | ||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | }; | ||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | 1 | ); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 11 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=7 SCAR=20.2
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 11 | my ( $self, $route, $method ) = @_; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | croak qq{Unrecognized HTTP method $method.} | ||||||||||||||||||||||||||||
| 30 | 11 | ▶ | unless defined( grep { $_ eq $method } METHODS() ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 11 | chomp( $route->{route} ); | |||||||||||||||||||||||||||
| 33 | 11 | ▶ | my $uri = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | substr( $route->route, 0, 1 ) eq '/' | ||||||||||||||||||||||||||||
| 35 | ? substr( $route->route, 1 ) | ||||||||||||||||||||||||||||
| 36 | : $route->route; | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 11 | my $m = $self->_map->{'/'}; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 11 | my @parts = split /\//x, $uri; | |||||||||||||||||||||||||||
| 41 | 11 | ▶ | if ( @parts && substr( $uri, -1 ) eq '/' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@parts and substr($uri, -1) eq "/"
MC/DC0%0/2
@parts and substr($uri, -1) eq "/"
@parts
substr($uri, -1) eq "/"
Truth table33%1/3
A and B
A= @partsB= substr($uri, -1) eq "/"
| |||||||||||||||||||||||||||||
| 42 | 0 | $parts[-1] .= '/'; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 11 | for (@parts) { | |||||||||||||||||||||||||||
| 46 | 16 | ▶ | if ( exists $m->{children}->{$_} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 4 | $m = $m->{children}->{$_}; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | else { | ||||||||||||||||||||||||||||
| 50 | 12 | $m->{children}->{$_} = { children => {} }; | |||||||||||||||||||||||||||
| 51 | 12 | $m = $m->{children}->{$_}; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 11 | $m->{methods}->{$method} = $route; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 11 | return $self; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | ## no critic qw(Subroutines::ProhibitExplicitReturnUndef) | ||||||||||||||||||||||||||||
| 61 | 12 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=11 SCAR=24.2
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 12 | my ( $self, $uri, $method, $context ) = @_; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 12 | chomp($uri); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 12 | ▶ | return $self->_map->{'/'}->{methods}->{$method} if $uri eq '/'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 11 | ▶ | $uri = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | substr( $uri, 0, 1 ) eq '/' | ||||||||||||||||||||||||||||
| 70 | ? substr( $uri, 1 ) | ||||||||||||||||||||||||||||
| 71 | : $uri; | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 11 | my $m = $self->_map->{'/'}; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 11 | my @parts = split /\//x, $uri; | |||||||||||||||||||||||||||
| 76 | 11 | ▶ | if ( @parts && substr( $uri, -1 ) eq '/' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@parts and substr($uri, -1) eq "/"
MC/DC0%0/2
@parts and substr($uri, -1) eq "/"
@parts
substr($uri, -1) eq "/"
Truth table33%1/3
A and B
A= @partsB= substr($uri, -1) eq "/"
| |||||||||||||||||||||||||||||
| 77 | 0 | $parts[-1] .= '/'; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 11 | my $params = {}; | |||||||||||||||||||||||||||
| 81 | 11 | for (@parts) { | |||||||||||||||||||||||||||
| 82 | 17 | ▶ | if ( exists $m->{children}->{$_} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 14 | $m = $m->{children}->{$_}; | |||||||||||||||||||||||||||
| 84 | 14 | next; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # Path parameters {foo} | ||||||||||||||||||||||||||||
| 88 | 3 | my $param; | |||||||||||||||||||||||||||
| 89 | 3 | my $part = $_; | |||||||||||||||||||||||||||
| 90 | 3 | for ( keys %{ $m->{children} } ) { | |||||||||||||||||||||||||||
| 91 | 5 | ($param) = /^\{([\w_]+)\}$/x; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 5 | ▶ | if ($param) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 1 | $params->{$param} = $part; | |||||||||||||||||||||||||||
| 95 | 1 | $m = $m->{children}->{"{$param}"}; | |||||||||||||||||||||||||||
| 96 | 1 | last; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # Catch all route: * | ||||||||||||||||||||||||||||
| 101 | 3 | ▶ | if ( exists $m->{children}->{'*'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 2 | $m = $m->{children}->{'*'}; | |||||||||||||||||||||||||||
| 103 | 2 | next; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 1 | ▶ | return undef unless defined $param; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 11 | $context->{params} = $params; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 11 | return $m->{methods}->{$method}; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # Merge two route maps | ||||||||||||||||||||||||||||
| 115 | 1 | ▶ | sub merge { | ||||||||||||||||||||||||||
|
Subroutine
merge: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 116 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 117 | 1 | my $other = shift; | |||||||||||||||||||||||||||
| 118 | 1 | my $events = shift; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 1 | my @stack; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | push( @stack, | ||||||||||||||||||||||||||||
| 123 | 1 | ▶ | $other->{_map}->{'/'} // croak qq{Invalid route map passed.} ); | ||||||||||||||||||||||||||
|
Condition50%1/2
$other->{"_map"}{"/"} // croak("Invalid route map passed.")
MC/DC0%0/1
$other->{"_map"}{"/"} // croak("Invalid route map passed.")
$other->{"_map"}{"/"}
Truth table50%1/2
A
A= $other->{"_map"}{"/"}
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | # FIXME: This may get slow on very large projects, but we'll see. | ||||||||||||||||||||||||||||
| 126 | 1 | ▶ | while ( my $p = pop @stack ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 3 | for ( keys $p->{children}->%* ) { | |||||||||||||||||||||||||||
| 128 | 2 | push( @stack, $p->{children}->{$_} ); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 3 | for ( keys $p->{methods}->%* ) { | |||||||||||||||||||||||||||
| 132 | 1 | my $route = $p->{methods}->{$_}; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # TODO: This sucks! | ||||||||||||||||||||||||||||
| 135 | 1 | foreach my $event ( EVENTS()->@* ) { | |||||||||||||||||||||||||||
| 136 | $route->event_handlers->{$event} = [ | ||||||||||||||||||||||||||||
| 137 | $events->{$event}->@*, | ||||||||||||||||||||||||||||
| 138 | 2 | $route->event_handlers->{$event}->@* | |||||||||||||||||||||||||||
| 139 | ]; | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 1 | $self->add( $route, $_ ); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 1 | return $self; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 1 | 1; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =encoding utf8 | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head1 NAME | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | Slick::RouteMap | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | L<Slick::RouteMap> is a simple "Hash-Trie" that resolves routes extremely fast, at the cost of using slightly more | ||||||||||||||||||||||||||||
| 160 | memory than other routing schemes. | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head1 API | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =head2 get | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | Given a uri (Str), method (Str), and L<Slick::Context>, find the associated L<Slick::Route>, and return it. | ||||||||||||||||||||||||||||
| 167 | Otherwise return C<undef>. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =head2 add | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | Given a L<Slick::Route> and a uri (Str), add it to the Hash-Trie for later lookup. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head2 merge | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | Merges two given L<Slick::RouteMap>s. This is the primary mechanism behind L<Slick::Router>. | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head1 See also | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =over 2 | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =item * L<Slick> | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =item * L<Slick::Router> | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =item * L<Slick::Context> | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =item * L<Slick::Database> | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =item * L<Slick::DatabaseExecutor> | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =item * L<Slick::DatabaseExecutor::MySQL> | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =item * L<Slick::DatabaseExecutor::Pg> | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =item * L<Slick::EventHandler> | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =item * L<Slick::Events> | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =item * L<Slick::Methods> | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =item * L<Slick::RouteMap> | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =item * L<Slick::Util> | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =back | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =cut | ||||||||||||||||||||||||||||