/usr/share/perl5/Mojolicious/Routes/Match.pm is in libmojolicious-perl 2.23-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | package Mojolicious::Routes::Match;
use Mojo::Base -base;
use Mojo::Util qw/decode url_unescape/;
has captures => sub { {} };
has stack => sub { [] };
has [qw/endpoint root/];
# "I'm Bender, baby, please insert liquor!"
sub new {
my $self = shift->SUPER::new();
# Method
$self->{method} = lc shift;
# Path
my $path = url_unescape shift;
$self->{path} = decode('UTF-8', $path) // $path;
# WebSocket
$self->{websocket} = shift;
return $self;
}
# "Life can be hilariously cruel."
sub match {
my ($self, $r, $c) = @_;
return unless $r;
# Match
$self->root($r) unless $self->root;
my $dictionary = $self->{dictionary} ||= $r->dictionary;
my $path = $self->{path};
my $pattern = $r->pattern;
my $captures = $pattern->shape_match(\$path, $r->is_endpoint);
return unless $captures;
$self->{path} = $path;
$captures = {%{$self->captures}, %$captures};
# Method
if (my $methods = $r->via) {
my $method = lc $self->{method};
$method = 'get' if $method eq 'head';
my $found = 0;
for my $m (@$methods) { ++$found and last if $method eq $m }
return unless $found;
}
# Conditions
my $conditions = $r->conditions;
for (my $i = 0; $i < @$conditions; $i += 2) {
my $name = $conditions->[$i];
my $value = $conditions->[$i + 1];
my $condition = $dictionary->{$name};
# No condition
return unless $condition;
# Match
return if !$condition->($r, $c, $captures, $value);
}
# WebSocket
return if $r->is_websocket && !$self->{websocket};
# Partial
my $empty = !length $path || $path eq '/' ? 1 : 0;
if ($r->partial) {
$captures->{path} = $path;
$self->endpoint($r);
$empty = 1;
}
# Update stack
$self->captures($captures);
my $endpoint = $r->is_endpoint;
if ($r->inline || ($endpoint && $empty)) {
push @{$self->stack}, {%$captures};
delete $captures->{cb};
delete $captures->{app};
}
# Waypoint match
if ($r->block && $empty) {
$self->endpoint($r);
return $self;
}
# Endpoint
return $self->endpoint($r) if $endpoint && $empty;
# Match children
my $snapshot = [@{$self->stack}];
for my $child (@{$r->children}) {
$self->match($child, $c);
# Endpoint found
return $self if $self->endpoint;
# Reset path
$self->{path} = $path;
# Reset stack
if ($r->parent) { $self->stack([@$snapshot]) }
else {
$self->captures({});
$self->stack([]);
}
}
return $self;
}
# "I'm not a robot!
# I don't like having discs crammed into me, unless they're Oreos.
# And then, only in the mouth."
sub path_for {
my $self = shift;
my $values = {};
my $name = undef;
# Single argument
if (@_ == 1) {
# Hash
$values = shift if ref $_[0] eq 'HASH';
# Name
$name = $_[0] if $_[0];
}
# Multiple arguments
elsif (@_ > 1) {
# Odd
if (@_ % 2) {
$name = shift;
$values = {@_};
}
# Even
else {
# Name and hashref
if (ref $_[1] eq 'HASH') {
$name = shift;
$values = shift;
}
# Just values
else { $values = {@_} }
}
}
# Current route
my $captures = $self->captures;
my $endpoint;
if ($name && $name eq 'current' || !$name) {
return unless $endpoint = $self->endpoint;
}
# Find endpoint
else {
my @children = ($self->root);
my $candidate;
while (my $child = shift @children) {
# Match
if ($child->name eq $name) {
$candidate = $child;
last if $child->has_custom_name;
}
# Search too
push @children, @{$child->children};
}
$endpoint = $candidate;
# Nothing
return $name unless $endpoint;
}
# Merge values
$values = {%$captures, format => undef, %$values};
my $pattern = $endpoint->pattern;
$values->{format} =
defined $captures->{format}
? $captures->{format}
: $pattern->defaults->{format}
if $pattern->reqs->{format};
# Render
my $path = $endpoint->render('', $values);
utf8::downgrade $path, 1;
return wantarray ? ($path, $endpoint->has_websocket) : $path;
}
1;
__END__
=head1 NAME
Mojolicious::Routes::Match - Routes visitor
=head1 SYNOPSIS
use Mojolicious::Routes;
use Mojolicious::Routes::Match;
# Routes
my $r = Mojolicious::Routes->new;
$r->route('/foo')->to(action => 'foo');
$r->route('/bar')->to(action => 'bar');
# Match
my $m = Mojolicious::Routes::Match->new(GET => '/bar');
$m->match($r);
say $m->captures->{action};
=head1 DESCRIPTION
L<Mojolicious::Routes::Match> is a visitor for L<Mojolicious::Routes>
structures.
=head1 ATTRIBUTES
L<Mojolicious::Routes::Match> implements the following attributes.
=head2 C<captures>
my $captures = $m->captures;
$m = $m->captures({foo => 'bar'});
Captured parameters.
=head2 C<endpoint>
my $endpoint = $m->endpoint;
$m = $m->endpoint(Mojolicious::Routes->new);
The routes endpoint that actually matched.
=head2 C<root>
my $root = $m->root;
$m = $m->root($routes);
The root of the routes tree.
=head2 C<stack>
my $stack = $m->stack;
$m = $m->stack([{foo => 'bar'}]);
Captured parameters with nesting history.
=head1 METHODS
L<Mojolicious::Routes::Match> inherits all methods from L<Mojo::Base> and
implements the following ones.
=head2 C<new>
my $m = Mojolicious::Routes::Match->new(get => '/foo');
my $m = Mojolicious::Routes::Match->new(get => '/foo', $ws);
Construct a new match object.
=head2 C<match>
$m->match(Mojolicious::Routes->new, Mojolicious::Controller->new);
Match against a routes tree.
=head2 C<path_for>
my $path = $m->path_for;
my $path = $m->path_for(foo => 'bar');
my $path = $m->path_for({foo => 'bar'});
my $path = $m->path_for('named');
my $path = $m->path_for('named', foo => 'bar');
my $path = $m->path_for('named', {foo => 'bar'});
my ($path, $ws) = $m->path_for;
my ($path, $ws) = $m->path_for(foo => 'bar');
my ($path, $ws) = $m->path_for({foo => 'bar'});
my ($path, $ws) = $m->path_for('named');
my ($path, $ws) = $m->path_for('named', foo => 'bar');
my ($path, $ws) = $m->path_for('named', {foo => 'bar'});
Render matching route with parameters into path.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
=cut
|