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
|
package Jaos::WebApp::Route;
use strict;
use warnings;
sub new
{
my $class = shift;
my $path = shift;
my %args = @_;
my $controller = delete $args{controller};
my $action = delete $args{action};
my $self = {%args};
my $pattern = $path;
# support {} for matching patterns
$pattern =~ s/{(\w+)}/:$1/g;
# strip leading /
#XXX #$pattern =~ s!^/+!!;
# store captured names and replace with match expr
my @capture;
$pattern =~ s{:([a-z0-9_]+)}{ push @capture, $1; '([^\/]+)' }ge;
# try and auto detect controller and action if nothing passed
if (!$controller) {
(my $parts = $path) =~ s/\/:.*//;
my @parts = grep { !/^$/ } split('/',$parts);
$action = pop @parts;
if (@parts && $action) {
my $name = join('::', map { ucfirst } @parts);
$controller = $name;
}
}
# bail early if this route is bad
eval {
if (!$controller->can($action)) {
eval "require $controller";
}
$controller->can($action) or die "$controller cannot $action";
};
if ($@) {
die "Nothing found to dispatch to for '$path'";
}
$self->{controller} = $controller;
$self->{action} = $action;
$self->{re} = qr{^$pattern$};
$self->{capture} = [@capture];
$self->{path} = $path;
bless $self, $class;
}
sub match
{
my ($self, $app) = @_;
if (my @matches = $app->req->path_info =~ $self->{re}) {
$app->log->debug("Found route for " . $app->req->path_info);
# store captures first, so available in hooks
for (my $i = 0; $i < scalar @{$self->{capture}}; $i++) {
my $label = $self->{capture}->[$i];
my $value = $matches[$i];
$app->log->debug("Found $label=$value from path");
$app->req->parameters->add($label => $value);
}
# advanced handling of constraint hooks
if (my $route_hooks = $app->{route_hooks}) {
for my $hook ( keys %$route_hooks ) {
if (my $options = $self->{$hook}) {
$app->log->debug("Running $hook");
return unless $route_hooks->{$hook}->($self, $app, $options);
}
}
}
return $self;
}
return;
}
sub controller { shift->{controller}; }
sub action { shift->{action}; }
=head1 NAME
Jaos::WebApp::Route - Route class for dispatching
=head1 SYNOPSIS
my $route = Jaos::WebApp::Route->new(
'/path/{variable}',
controller => 'Controller::Class',
action => 'method',
%options
);
# auto detect controller and action method
my $route = Jaos::WebApp::Route->new(
'/user/:id',
);
# use custom constraints through plugins
my $route = Jaos::WebApp::Route->new(
'/path/{variable}',
controller => 'Controller::Class',
action => 'method',
constraint => { variable => qr/\d/ },
header => { host => qr/\.foo\.org/, referer => qr/\.foo\.org/ },
method => 'get',
);
# match the current request
if ($route->match($app, $req)) {
my $controller_name = $r->controller;
my $controller_method = $r->action;
}
=head1 DESCRIPTION
Jaos::WebApp::Route is a representation of paths to controllers.
=head1 METHODS
=head2 new(path, %options)
Instantiate a new route object. Pass in the path to route and a series of key/value pairs to define the handler of the path. The basic supported options are controller and action. These define the handling controller and the method to dispatch to.
If controller and action are not specified, an attempt is made to auto detect the controller and method. It is fatal to declare a path that cannot be matched to a controller and method.
Path can contain placeholders for capturing parts of the path as parameters. Currently supported syntax includes B<:var> and B<{var}>.
Plugins can extend the handling of the option keys by passing a key => sub {} callback through the main Jaos::WebApp application plugin system. See L<Jaos::WebApp::Route::HeaderConstraint>, L<Jaos::WebApp::Route::HeaderConstraint>, and L<Jaos::WebApp::Route::MethodConstraint>.
Route objects are primarily instantiated through the connect method of L<Jaos::WebApp::Dispatcher> by the main L<Jaos::WebApp> application.
=head2 match
Returns self if the current request match the rules setup at instantiation. Undef otherwise.
=head2 controller
Returns the name of the defined controller for this route.
=head2 action
Returns the name of the defined method for this route for use with the controller.
=head1 AUTHOR
Jason Woodward <woodwardj@jaos.org>
=head1 LICENSE
Copyright (C) 2010-2011 Jason Woodward
All rights reserved
This library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this library. If not, see <http://www.gnu.org/licenses/>.
=cut
1;
|