Commit | Line | Data |
6b239949 |
1 | package Catalyst::DispatchType::Path; |
2 | |
3c0186f2 |
3 | use Moose; |
3c0186f2 |
4 | extends 'Catalyst::DispatchType'; |
5 | |
e8b9f2a9 |
6 | use Text::SimpleTable; |
39fc2ce1 |
7 | use Catalyst::Utils; |
e8b9f2a9 |
8 | use URI; |
3c0186f2 |
9 | |
36dbb986 |
10 | has _paths => ( |
11 | is => 'rw', |
12 | isa => 'HashRef', |
13 | required => 1, |
14 | default => sub { +{} }, |
15 | ); |
16 | |
0fc2d522 |
17 | no Moose; |
18 | |
2633d7dc |
19 | =head1 NAME |
20 | |
21 | Catalyst::DispatchType::Path - Path DispatchType |
22 | |
23 | =head1 SYNOPSIS |
24 | |
26dc649a |
25 | See L<Catalyst::DispatchType>. |
2633d7dc |
26 | |
27 | =head1 DESCRIPTION |
28 | |
26dc649a |
29 | Dispatch type managing full path matching behaviour. For more information on |
30 | dispatch types, see: |
31 | |
32 | =over 4 |
33 | |
b9b89145 |
34 | =item * L<Catalyst::Manual::Intro> for how they affect application authors |
26dc649a |
35 | |
36 | =item * L<Catalyst::DispatchType> for implementation information. |
37 | |
38 | =back |
39 | |
2633d7dc |
40 | =head1 METHODS |
41 | |
b5ecfcf0 |
42 | =head2 $self->list($c) |
a9cbd748 |
43 | |
4ab87e27 |
44 | Debug output for Path dispatch points |
45 | |
a9cbd748 |
46 | =cut |
47 | |
48 | sub list { |
49 | my ( $self, $c ) = @_; |
39fc2ce1 |
50 | my $column_width = Catalyst::Utils::term_width() - 35 - 9; |
b0ad47c1 |
51 | my $paths = Text::SimpleTable->new( |
39fc2ce1 |
52 | [ 35, 'Path' ], [ $column_width, 'Private' ] |
53 | ); |
36dbb986 |
54 | foreach my $path ( sort keys %{ $self->_paths } ) { |
c5b74a51 |
55 | my $display_path = $path eq '/' ? $path : "/$path"; |
36dbb986 |
56 | foreach my $action ( @{ $self->_paths->{$path} } ) { |
e8b9f2a9 |
57 | $paths->row( $display_path, "/$action" ); |
0bd5f8a2 |
58 | } |
a9cbd748 |
59 | } |
e8b9f2a9 |
60 | $c->log->debug( "Loaded Path actions:\n" . $paths->draw . "\n" ) |
36dbb986 |
61 | if ( keys %{ $self->_paths } ); |
a9cbd748 |
62 | } |
63 | |
b5ecfcf0 |
64 | =head2 $self->match( $c, $path ) |
2633d7dc |
65 | |
b2b90ec2 |
66 | For each action registered to this exact path, offers the action a chance to |
67 | match the path (in the order in which they were registered). Succeeds on the |
68 | first action that matches, if any; if not, returns 0. |
4ab87e27 |
69 | |
2633d7dc |
70 | =cut |
71 | |
72 | sub match { |
73 | my ( $self, $c, $path ) = @_; |
6b239949 |
74 | |
2f381252 |
75 | $path = '/' if !defined $path || !length $path; |
0bd5f8a2 |
76 | |
91955398 |
77 | my @actions = @{ $self->_paths->{$path} || [] }; |
78 | |
79 | foreach my $action ( @actions ) { |
0bd5f8a2 |
80 | next unless $action->match($c); |
6b239949 |
81 | $c->req->action($path); |
82 | $c->req->match($path); |
83 | $c->action($action); |
11bd4e3e |
84 | $c->namespace( $action->namespace ); |
6b239949 |
85 | return 1; |
86 | } |
87 | |
88 | return 0; |
89 | } |
90 | |
b5ecfcf0 |
91 | =head2 $self->register( $c, $action ) |
2633d7dc |
92 | |
b2b90ec2 |
93 | Calls register_path for every Path attribute for the given $action. |
4ab87e27 |
94 | |
2633d7dc |
95 | =cut |
96 | |
97 | sub register { |
6b239949 |
98 | my ( $self, $c, $action ) = @_; |
22f3a8dd |
99 | |
27708fc5 |
100 | my @register = @{ $action->attributes->{Path} || [] }; |
6b239949 |
101 | |
694d15f1 |
102 | $self->register_path( $c, $_, $action ) for @register; |
5b707014 |
103 | |
694d15f1 |
104 | return 1 if @register; |
105 | return 0; |
081def36 |
106 | } |
107 | |
b5ecfcf0 |
108 | =head2 $self->register_path($c, $path, $action) |
081def36 |
109 | |
b2b90ec2 |
110 | Registers an action at a given path. |
4ab87e27 |
111 | |
081def36 |
112 | =cut |
113 | |
114 | sub register_path { |
694d15f1 |
115 | my ( $self, $c, $path, $action ) = @_; |
081def36 |
116 | $path =~ s!^/!!; |
0ba80bce |
117 | $path = '/' unless length $path; |
595f3872 |
118 | $path = URI->new($path)->canonical; |
5299fff8 |
119 | $path =~ s{(?<=[^/])/+\z}{}; |
27708fc5 |
120 | |
91955398 |
121 | $self->_paths->{$path} = [ |
cb3d811c |
122 | sort { $a->compare($b) } ($action, @{ $self->_paths->{$path} || [] }) |
91955398 |
123 | ]; |
05b47f2e |
124 | |
0bd5f8a2 |
125 | return 1; |
6b239949 |
126 | } |
127 | |
ea0e58d9 |
128 | =head2 $self->uri_for_action($action, $captures) |
129 | |
130 | get a URI part for an action; always returns undef is $captures is set |
131 | since Path actions don't have captures |
132 | |
133 | =cut |
134 | |
135 | sub uri_for_action { |
136 | my ( $self, $action, $captures ) = @_; |
137 | |
138 | return undef if @$captures; |
139 | |
140 | if (my $paths = $action->attributes->{Path}) { |
141 | my $path = $paths->[0]; |
142 | $path = '/' unless length($path); |
143 | $path = "/${path}" unless ($path =~ m/^\//); |
144 | $path = URI->new($path)->canonical; |
145 | return $path; |
146 | } else { |
147 | return undef; |
148 | } |
149 | } |
150 | |
2f381252 |
151 | =head1 AUTHORS |
2633d7dc |
152 | |
2f381252 |
153 | Catalyst Contributors, see Catalyst.pm |
2633d7dc |
154 | |
155 | =head1 COPYRIGHT |
156 | |
536bee89 |
157 | This library is free software. You can redistribute it and/or modify it under |
2633d7dc |
158 | the same terms as Perl itself. |
159 | |
160 | =cut |
161 | |
e5ecd5bc |
162 | __PACKAGE__->meta->make_immutable; |
163 | |
6b239949 |
164 | 1; |