paranoid acl -> no permission checks inside of controllers :)
[blog.pm.git] / lib / Catalyst / DispatchType / Route.pm
blobe5d0a40ed491ef71b001f5d51c4c7bf599fd8ad0
1 package Catalyst::DispatchType::Route;
3 use strict;
4 use base qw/Catalyst::DispatchType/;
5 use Text::SimpleTable;
6 use URI;
8 use Catalyst::DispatchType::RouteMapper;
10 =head1 NAME
12 Catalyst::DispatchType::Route - RoR Route DispatchType
14 =head1 SYNOPSIS
16 See L<Catalyst>.
18 =head1 DESCRIPTION
20 =head1 METHODS
22 =head2 $self->list($c)
24 Debug output for Route dispatch points
26 =cut
28 sub list {
29 my ( $self, $c ) = @_;
30 my $paths = Text::SimpleTable->new( [ 35, 'Route' ], [ 36, 'Private' ] );
31 foreach my $path ( sort keys %{ $self->{routes} } ) {
32 my $display_path = $path eq '/' ? $path : "/$path";
33 $paths->row( $display_path, "/" . $self->{routes}->{$path} );
35 $c->log->debug( "Loaded Route actions:\n" . $paths->draw . "\n" )
36 if ( keys %{ $self->{routes} } );
39 =head2 $self->match( $c, $path )
41 For each action registered to this exact path, offers the action a chance to
42 match the path (in the order in which they were registered). Succeeds on the
43 first action that matches, if any; if not, returns 0.
45 =cut
47 sub match {
48 my ( $self, $c, $path ) = @_;
50 return 0 if @{ $c->req->args };
52 $path ||= '/';
53 use Data::Dumper;
55 #$c->log->debug('path=' . $path);
56 #$c->log->debug('ehre' . Dumper($self->{routes} ));
58 my $mapper = $self->{mapper};
59 if (my $route = $mapper->parse($path)) {
60 #$c->log->debug('FOUDN!!! route=' . $route);
62 if (my $action = $self->{routes}->{$route}) {;
63 #$c->log->debug('ACTION!');
64 $c->req->action($path);
65 $c->req->match($path);
66 $c->action($action);
67 $c->namespace( $action->namespace );
69 if (my $params = $mapper->getParams()) {
70 while (my($key, $val) = each %$params ) {
71 $c->req->param( $key => $val);
75 return 1;
79 return 0;
82 =head2 $self->register( $c, $action )
84 Calls register_path for every Path attribute for the given $action.
86 =cut
88 sub register {
89 my ( $self, $c, $action ) = @_;
91 my @register = @{ $action->attributes->{Route} || [] };
93 $self->register_route( $c, $_, $action ) for @register;
95 return 1 if @register;
96 return 0;
99 =head2 $self->register_path($c, $path, $action)
101 Registers an action at a given path.
103 =cut
105 sub register_route {
106 my ( $self, $c, $route, $action ) = @_;
108 #$c->log->debug( 'path=' . $route . ' action=' . $action );
110 $self->{mapper} ||= Catalyst::DispatchType::RouteMapper->new();
112 my $mapper = $self->{ mapper };
114 $self->{routes} ||= {};
116 $route = $action->namespace . '/' . $route unless $route =~ s/^\///o;
118 my $map = {};
120 my @vars = split('/', $route);
121 my @new_vars = ();
122 foreach my $var ( @vars ) {
123 if ( $var =~ m/^\:(.*?)(?:\|(.*?)\|)?(\=(\?)?(.*))?$/o ) {
124 my $name = $1;
125 my $constraint = $2;
126 my $extra = $3;
128 $var = ":$name";
130 $map->{ constraints }->{ $name } = $constraint if $constraint;
132 if ( $extra ) {
133 my $isdefault = $4;
134 my $val = $5;
136 $map->{ defaults }->{ $name } = $val if $isdefault && $val;
137 $map->{ params }->{ $name } = $isdefault
138 ? undef
139 : $val || undef;
143 push @new_vars, $var;
146 $route = join('/', @new_vars);
147 $self->{routes}->{$route} = $action;
149 $map->{ path } = $route;
150 $mapper->addMap($map);
152 #use Data::Dumper;
154 #$c->log->debug(Dumper($map));
156 return 1;
159 =head2 $self->uri_for_action($action, $captures)
161 get a URI part for an action; always returns undef is $captures is set
162 since Path actions don't have captures
164 =cut
166 sub uri_for_action {
167 my ( $self, $action, $captures ) = @_;
169 return undef if @$captures;
171 if (my $paths = $action->attributes->{Path}) {
172 my $path = $paths->[0];
173 $path = '/' unless length($path);
174 $path = "/${path}" unless ($path =~ m/^\//);
175 $path = URI->new($path)->canonical;
176 return $path;
177 } else {
178 return undef;
182 =head1 AUTHOR
186 =head1 COPYRIGHT
188 This program is free software, you can redistribute it and/or modify it under
189 the same terms as Perl itself.
191 =cut