/usr/share/perl5/Mojolicious/Routes/Match.pm is in libmojolicious-perl 7.59+dfsg-1ubuntu1.
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 | package Mojolicious::Routes::Match;
use Mojo::Base -base;
use Mojo::Util;
has [qw(endpoint root)];
has position => 0;
has stack => sub { [] };
sub find { $_[0]->_match($_[0]->root, $_[1], $_[2]) }
sub path_for {
my ($self, $name, %values) = (shift, Mojo::Util::_options(@_));
# Current route
my $route;
if (!$name || $name eq 'current') {
return {} unless $route = $self->endpoint;
}
# Find endpoint
else { return {path => $name} unless $route = $self->root->lookup($name) }
# Merge values (clear format)
my $captures = $self->stack->[-1] || {};
%values = (%$captures, format => undef, %values);
my $pattern = $route->pattern;
$values{format}
//= defined $captures->{format}
? $captures->{format}
: $pattern->defaults->{format}
if $pattern->constraints->{format};
my $path = $route->render(\%values);
return {path => $path, websocket => $route->has_websocket};
}
sub _match {
my ($self, $r, $c, $options) = @_;
# Pattern
my $path = $options->{path};
my $partial = $r->partial;
my $detect = (my $endpoint = $r->is_endpoint) && !$partial;
return undef
unless my $captures = $r->pattern->match_partial(\$path, $detect);
local $options->{path} = $path;
local @{$self->{captures} ||= {}}{keys %$captures} = values %$captures;
$captures = $self->{captures};
# Method
my $methods = $r->via;
return undef if $methods && !grep { $_ eq $options->{method} } @$methods;
# Conditions
if (my $over = $r->over) {
my $conditions = $self->{conditions} ||= $self->root->conditions;
for (my $i = 0; $i < @$over; $i += 2) {
return undef unless my $condition = $conditions->{$over->[$i]};
return undef if !$condition->($r, $c, $captures, $over->[$i + 1]);
}
}
# WebSocket
return undef if $r->is_websocket && !$options->{websocket};
# Partial
my $empty = !length $path || $path eq '/';
if ($partial) {
$captures->{path} = $path;
$self->endpoint($r);
$empty = 1;
}
# Endpoint (or intermediate destination)
if (($endpoint && $empty) || $r->inline) {
push @{$self->stack}, {%$captures};
if ($endpoint && $empty) {
my $format = $captures->{format};
if ($format) { $_->{format} = $format for @{$self->stack} }
return !!$self->endpoint($r);
}
delete @$captures{qw(app cb)};
}
# Match children
my @snapshot = $r->parent ? ([@{$self->stack}], $captures) : ([], {});
for my $child (@{$r->children}) {
return 1 if $self->_match($child, $c, $options);
$self->stack([@{$snapshot[0]}])->{captures} = $snapshot[1];
}
}
1;
=encoding utf8
=head1 NAME
Mojolicious::Routes::Match - Find routes
=head1 SYNOPSIS
use Mojolicious::Controller;
use Mojolicious::Routes;
use Mojolicious::Routes::Match;
# Routes
my $r = Mojolicious::Routes->new;
$r->get('/:controller/:action');
$r->put('/:controller/:action');
# Match
my $c = Mojolicious::Controller->new;
my $match = Mojolicious::Routes::Match->new(root => $r);
$match->find($c => {method => 'PUT', path => '/foo/bar'});
say $match->stack->[0]{controller};
say $match->stack->[0]{action};
# Render
say $match->path_for->{path};
say $match->path_for(action => 'baz')->{path};
=head1 DESCRIPTION
L<Mojolicious::Routes::Match> finds routes in L<Mojolicious::Routes>
structures.
=head1 ATTRIBUTES
L<Mojolicious::Routes::Match> implements the following attributes.
=head2 endpoint
my $route = $match->endpoint;
$match = $match->endpoint(Mojolicious::Routes::Route->new);
The route endpoint that matched, usually a L<Mojolicious::Routes::Route>
object.
=head2 position
my $position = $match->position;
$match = $match->position(2);
Current position on the L</"stack">, defaults to C<0>.
=head2 root
my $root = $match->root;
$match = $match->root(Mojolicious::Routes->new);
The root of the route structure, usually a L<Mojolicious::Routes> object.
=head2 stack
my $stack = $match->stack;
$match = $match->stack([{action => 'foo'}, {action => 'bar'}]);
Captured parameters with nesting history.
=head1 METHODS
L<Mojolicious::Routes::Match> inherits all methods from L<Mojo::Base> and
implements the following new ones.
=head2 find
$match->find(Mojolicious::Controller->new, {method => 'GET', path => '/'});
Match controller and options against L</"root"> to find an appropriate
L</"endpoint">.
=head2 path_for
my $info = $match->path_for;
my $info = $match->path_for(foo => 'bar');
my $info = $match->path_for({foo => 'bar'});
my $info = $match->path_for('named');
my $info = $match->path_for('named', foo => 'bar');
my $info = $match->path_for('named', {foo => 'bar'});
Render matching route with parameters into path.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.
=cut
|