1 package Catalyst::DispatchType::Path;
4 extends 'Catalyst::DispatchType';
9 use Encode 2.21 'decode_utf8';
15 default => sub { +{} },
22 Catalyst::DispatchType::Path - Path DispatchType
26 See L<Catalyst::DispatchType>.
30 Dispatch type managing full path matching behaviour. For more information on
35 =item * L<Catalyst::Manual::Intro> for how they affect application authors
37 =item * L<Catalyst::DispatchType> for implementation information.
43 =head2 $self->list($c)
45 Debug output for Path dispatch points
50 my ( $self, $c ) = @_;
51 my $avail_width = Catalyst::Utils::term_width() - 9;
52 my $col1_width = ($avail_width * .50) < 35 ? 35 : int($avail_width * .50);
53 my $col2_width = $avail_width - $col1_width;
54 my $paths = Text::SimpleTable->new(
55 [ $col1_width, 'Path' ], [ $col2_width, 'Private' ]
57 foreach my $path ( sort keys %{ $self->_paths } ) {
58 foreach my $action ( @{ $self->_paths->{$path} } ) {
59 my $args = $action->number_of_args;
60 my $parts = defined($args) ? '/*' x $args : '/...';
62 my $display_path = "/$path/$parts";
63 $display_path =~ s{/{1,}}{/}g;
64 $display_path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; # deconvert urlencoded for pretty view
65 $display_path = decode_utf8 $display_path; # URI does encoding
66 $paths->row( $display_path, "/$action" );
69 $c->log->debug( "Loaded Path actions:\n" . $paths->draw . "\n" )
70 if ( keys %{ $self->_paths } );
73 =head2 $self->match( $c, $path )
75 For each action registered to this exact path, offers the action a chance to
76 match the path (in the order in which they were registered). Succeeds on the
77 first action that matches, if any; if not, returns 0.
82 my ( $self, $c, $path ) = @_;
84 $path = '/' if !defined $path || !length $path;
86 my @actions = @{ $self->_paths->{$path} || [] };
88 foreach my $action ( @actions ) {
89 next unless $action->match($c);
90 $c->req->action($path);
91 $c->req->match($path);
93 $c->namespace( $action->namespace );
100 =head2 $self->register( $c, $action )
102 Calls register_path for every Path attribute for the given $action.
107 my ( $self, $c, $action ) = @_;
109 my @register = @{ $action->attributes->{Path} || [] };
111 $self->register_path( $c, $_, $action ) for @register;
113 return 1 if @register;
117 =head2 $self->register_path($c, $path, $action)
119 Registers an action at a given path.
124 my ( $self, $c, $path, $action ) = @_;
126 $path = '/' unless length $path;
127 $path = URI->new($path)->canonical;
128 $path =~ s{(?<=[^/])/+\z}{};
130 $self->_paths->{$path} = [
131 sort { $a->compare($b) } ($action, @{ $self->_paths->{$path} || [] })
137 =head2 $self->uri_for_action($action, $captures)
139 get a URI part for an action; always returns undef is $captures is set
140 since Path actions don't have captures
145 my ( $self, $action, $captures ) = @_;
147 return undef if @$captures;
149 if (my $paths = $action->attributes->{Path}) {
150 my $path = $paths->[0];
151 $path = '/' unless length($path);
152 $path = "/${path}" unless ($path =~ m/^\//);
153 $path = URI->new($path)->canonical;
162 Catalyst Contributors, see Catalyst.pm
166 This library is free software. You can redistribute it and/or modify it under
167 the same terms as Perl itself.
171 __PACKAGE__->meta->make_immutable;