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