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 ) = @_; |
48d435ba |
50 | my $avail_width = Catalyst::Utils::term_width() - 9; |
51 | my $col1_width = ($avail_width * .50) < 35 ? 35 : int($avail_width * .50); |
52 | my $col2_width = $avail_width - $col1_width; |
b0ad47c1 |
53 | my $paths = Text::SimpleTable->new( |
48d435ba |
54 | [ $col1_width, 'Path' ], [ $col2_width, 'Private' ] |
39fc2ce1 |
55 | ); |
36dbb986 |
56 | foreach my $path ( sort keys %{ $self->_paths } ) { |
c5b74a51 |
57 | my $display_path = $path eq '/' ? $path : "/$path"; |
36dbb986 |
58 | foreach my $action ( @{ $self->_paths->{$path} } ) { |
e8b9f2a9 |
59 | $paths->row( $display_path, "/$action" ); |
0bd5f8a2 |
60 | } |
a9cbd748 |
61 | } |
e8b9f2a9 |
62 | $c->log->debug( "Loaded Path actions:\n" . $paths->draw . "\n" ) |
36dbb986 |
63 | if ( keys %{ $self->_paths } ); |
a9cbd748 |
64 | } |
65 | |
b5ecfcf0 |
66 | =head2 $self->match( $c, $path ) |
2633d7dc |
67 | |
b2b90ec2 |
68 | For each action registered to this exact path, offers the action a chance to |
69 | match the path (in the order in which they were registered). Succeeds on the |
70 | first action that matches, if any; if not, returns 0. |
4ab87e27 |
71 | |
2633d7dc |
72 | =cut |
73 | |
74 | sub match { |
75 | my ( $self, $c, $path ) = @_; |
6b239949 |
76 | |
2f381252 |
77 | $path = '/' if !defined $path || !length $path; |
0bd5f8a2 |
78 | |
91955398 |
79 | my @actions = @{ $self->_paths->{$path} || [] }; |
80 | |
81 | foreach my $action ( @actions ) { |
0bd5f8a2 |
82 | next unless $action->match($c); |
6b239949 |
83 | $c->req->action($path); |
84 | $c->req->match($path); |
85 | $c->action($action); |
11bd4e3e |
86 | $c->namespace( $action->namespace ); |
6b239949 |
87 | return 1; |
88 | } |
89 | |
90 | return 0; |
91 | } |
92 | |
b5ecfcf0 |
93 | =head2 $self->register( $c, $action ) |
2633d7dc |
94 | |
b2b90ec2 |
95 | Calls register_path for every Path attribute for the given $action. |
4ab87e27 |
96 | |
2633d7dc |
97 | =cut |
98 | |
99 | sub register { |
6b239949 |
100 | my ( $self, $c, $action ) = @_; |
22f3a8dd |
101 | |
27708fc5 |
102 | my @register = @{ $action->attributes->{Path} || [] }; |
6b239949 |
103 | |
694d15f1 |
104 | $self->register_path( $c, $_, $action ) for @register; |
5b707014 |
105 | |
694d15f1 |
106 | return 1 if @register; |
107 | return 0; |
081def36 |
108 | } |
109 | |
b5ecfcf0 |
110 | =head2 $self->register_path($c, $path, $action) |
081def36 |
111 | |
b2b90ec2 |
112 | Registers an action at a given path. |
4ab87e27 |
113 | |
081def36 |
114 | =cut |
115 | |
116 | sub register_path { |
694d15f1 |
117 | my ( $self, $c, $path, $action ) = @_; |
081def36 |
118 | $path =~ s!^/!!; |
0ba80bce |
119 | $path = '/' unless length $path; |
595f3872 |
120 | $path = URI->new($path)->canonical; |
5299fff8 |
121 | $path =~ s{(?<=[^/])/+\z}{}; |
27708fc5 |
122 | |
91955398 |
123 | $self->_paths->{$path} = [ |
cb3d811c |
124 | sort { $a->compare($b) } ($action, @{ $self->_paths->{$path} || [] }) |
91955398 |
125 | ]; |
05b47f2e |
126 | |
0bd5f8a2 |
127 | return 1; |
6b239949 |
128 | } |
129 | |
ea0e58d9 |
130 | =head2 $self->uri_for_action($action, $captures) |
131 | |
132 | get a URI part for an action; always returns undef is $captures is set |
133 | since Path actions don't have captures |
134 | |
135 | =cut |
136 | |
137 | sub uri_for_action { |
138 | my ( $self, $action, $captures ) = @_; |
139 | |
140 | return undef if @$captures; |
141 | |
142 | if (my $paths = $action->attributes->{Path}) { |
143 | my $path = $paths->[0]; |
144 | $path = '/' unless length($path); |
145 | $path = "/${path}" unless ($path =~ m/^\//); |
146 | $path = URI->new($path)->canonical; |
147 | return $path; |
148 | } else { |
149 | return undef; |
150 | } |
151 | } |
152 | |
2f381252 |
153 | =head1 AUTHORS |
2633d7dc |
154 | |
2f381252 |
155 | Catalyst Contributors, see Catalyst.pm |
2633d7dc |
156 | |
157 | =head1 COPYRIGHT |
158 | |
536bee89 |
159 | This library is free software. You can redistribute it and/or modify it under |
2633d7dc |
160 | the same terms as Perl itself. |
161 | |
162 | =cut |
163 | |
e5ecd5bc |
164 | __PACKAGE__->meta->make_immutable; |
165 | |
6b239949 |
166 | 1; |