Fixed streaming write from a filehandle to stop writing if the browser is closed
[catagits/Catalyst-Runtime.git] / lib / Catalyst / DispatchType / Regex.pm
CommitLineData
b96f127f 1package Catalyst::DispatchType::Regex;
2
3use strict;
6b239949 4use base qw/Catalyst::DispatchType::Path/;
8c113188 5use Text::SimpleTable;
b96f127f 6
2633d7dc 7=head1 NAME
b96f127f 8
2633d7dc 9Catalyst::DispatchType::Regex - Regex DispatchType
b96f127f 10
2633d7dc 11=head1 SYNOPSIS
12
13See L<Catalyst>.
14
15=head1 DESCRIPTION
16
17=head1 METHODS
18
19=over 4
20
a9cbd748 21=item $self->list($c)
22
23=cut
24
25sub list {
26 my ( $self, $c ) = @_;
8c113188 27 my $re = Text::SimpleTable->new( [ 36, 'Regex' ], [ 37, 'Private' ] );
a9cbd748 28 for my $regex ( @{ $self->{compiled} } ) {
29 my $compiled = $regex->{re};
30 my $action = $regex->{action};
8c113188 31 $re->row( $compiled, "/$action" );
a9cbd748 32 }
33 $c->log->debug( "Loaded Regex actions:\n" . $re->draw )
8c113188 34 if ( @{ $self->{compiled} } );
a9cbd748 35}
36
2633d7dc 37=item $self->match( $c, $path )
38
39=cut
40
41sub match {
42 my ( $self, $c, $path ) = @_;
43
44 return if $self->SUPER::match( $c, $path );
45
46 # Check path against plain text first
47
48 foreach my $compiled ( @{ $self->{compiled} || [] } ) {
b96f127f 49 if ( my @snippets = ( $path =~ $compiled->{re} ) ) {
2633d7dc 50 $c->req->action( $compiled->{path} );
b96f127f 51 $c->req->match($path);
2633d7dc 52 $c->req->snippets( \@snippets );
53 $c->action( $compiled->{action} );
11bd4e3e 54 $c->namespace( $compiled->{action}->namespace );
b96f127f 55 return 1;
56 }
57 }
58
59 return 0;
60}
61
2633d7dc 62=item $self->register( $c, $action )
63
64=cut
65
66sub register {
b96f127f 67 my ( $self, $c, $action ) = @_;
68 my $attrs = $action->attributes;
2633d7dc 69 my @register = map { @{ $_ || [] } } @{$attrs}{ 'Regex', 'Regexp' };
081def36 70 foreach
71 my $r ( map { @{ $_ || [] } } @{$attrs}{ 'LocalRegex', 'LocalRegexp' } )
72 {
73 unless ( $r =~ s/^\^// ) { $r = "(?:.*?)$r"; }
74 push( @register, '^' . $action->namespace . '/' . $r );
75 }
76
b96f127f 77 foreach my $r (@register) {
081def36 78 $self->register_path( $c, $r, $action );
79 $self->register_regex( $c, $r, $action );
b96f127f 80 }
81}
82
081def36 83=item $self->register_regex($c, $re, $action)
84
85=cut
86
87sub register_regex {
88 my ( $self, $c, $re, $action ) = @_;
89 push(
90 @{ $self->{compiled} }, # and compiled regex for us
91 {
92 re => qr#$re#,
93 action => $action,
94 path => $re,
95 }
96 );
97}
98
2633d7dc 99=back
100
101=head1 AUTHOR
102
103Matt S Trout
104Sebastian Riedel, C<sri@cpan.org>
105
106=head1 COPYRIGHT
107
108This program is free software, you can redistribute it and/or modify it under
109the same terms as Perl itself.
110
111=cut
112
b96f127f 1131;