X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FDispatcher.pm;h=0fe6c32a337907ea923826049972e621bcaed856;hb=784ab0e42f27feccdf30543d909352ae8657cbeb;hp=e143b001bfca4470f705dd74e12faa0167a75d42;hpb=78d760bb64c2142042834b11911b728259e3005d;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Dispatcher.pm b/lib/Catalyst/Dispatcher.pm index e143b00..0fe6c32 100644 --- a/lib/Catalyst/Dispatcher.pm +++ b/lib/Catalyst/Dispatcher.pm @@ -6,9 +6,8 @@ use Catalyst::Exception; use Catalyst::Utils; use Catalyst::Action; use Catalyst::ActionContainer; -use Catalyst::DispatchType::Path; -use Catalyst::DispatchType::Regex; use Catalyst::DispatchType::Default; +use Catalyst::DispatchType::Index; use Text::ASCIITable; use Tree::Simple; use Tree::Simple::Visitor::FindByPath; @@ -16,7 +15,13 @@ use Tree::Simple::Visitor::FindByPath; # Stringify to class use overload '""' => sub { return ref shift }, fallback => 1; -__PACKAGE__->mk_accessors(qw/actions tree dispatch_types/); +__PACKAGE__->mk_accessors(qw/tree dispatch_types/); + +# Preload these action types +our @PRELOAD = qw/Path Regex/; + +# Postload these action types +our @POSTLOAD = qw/Index Default/; =head1 NAME @@ -229,7 +234,7 @@ sub prepare_action { # this level foreach my $type ( @{ $self->dispatch_types } ) { - last DESCEND if $type->prepare_action( $c, $path ); + last DESCEND if $type->match( $c, $path ); } # If not, move the last part path to args @@ -248,7 +253,7 @@ sub prepare_action { sub get_action { my ( $self, $c, $action, $namespace, $inherit ) = @_; return [] unless $action; - $namespace ||= '/'; + $namespace ||= ''; $inherit ||= 0; my @match = $self->get_containers($namespace); @@ -257,7 +262,19 @@ sub get_action { foreach my $child ( $inherit ? @match : $match[-1] ) { my $node = $child->actions; - push( @results, [ $node->{$action} ] ) if defined $node->{$action}; + if ( defined $node->{$action} ) { + unless ($inherit) { + $namespace = '' if $namespace eq '/'; + my $reverse = $node->{$action}->reverse; + my $name = $namespace + ? $namespace =~ /\/$/ + ? "$namespace$action" + : "$namespace/$action" + : $action; + last unless $name eq $reverse; + } + push( @results, [ $node->{$action} ] ); + } } return \@results; } @@ -320,7 +337,19 @@ sub set_action { # Parse out :Foo(bar) into Foo => bar etc (and arrayify) + my %initialized; + $initialized{ ref $_ }++ for @{ $self->dispatch_types }; + if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+)\s*\))?$/ ) ) { + + # Initialize types + my $class = "Catalyst::DispatchType::$key"; + unless ( $initialized{$class} ) { + eval "require $class"; + push( @{ $self->dispatch_types }, $class->new ) unless $@; + $initialized{$class}++; + } + if ( defined $value ) { ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ ); } @@ -382,7 +411,7 @@ sub set_action { # Pass the action to our dispatch types so they can register it if reqd. foreach my $type ( @{ $self->dispatch_types } ) { - $type->register_action( $c, $action ); + $type->register( $c, $action ); } } @@ -393,18 +422,16 @@ sub set_action { sub setup_actions { my ( $self, $class ) = @_; - # These are the core structures - $self->actions( - { - plain => {}, - private => {}, - regex => {}, - compiled => [] - } - ); + $self->dispatch_types( [] ); - $self->dispatch_types( - [ map { "Catalyst::DispatchType::$_"->new } qw/Path Regex Default/ ] ); + # Preload action types + for my $type (@PRELOAD) { + my $class = "Catalyst::DispatchType::$type"; + eval "require $class"; + Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ ) + if $@; + push @{ $self->dispatch_types }, $class->new; + } # We use a tree my $container = @@ -433,27 +460,28 @@ sub setup_actions { } for my $namespace ( keys %namespaces ) { - for my $sym ( values %{ $namespace . '::' } ) { - if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) { - $name = *{$sym}{NAME}; $class->set_action( $name, $code, $comp, $attrs ); last; } - } - } - } + } + # Postload action types + for my $type (@POSTLOAD) { + my $class = "Catalyst::DispatchType::$type"; + eval "require $class"; + Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ ) + if $@; + push @{ $self->dispatch_types }, $class->new; } return unless $class->debug; - my $actions = $self->actions; my $privates = Text::ASCIITable->new; $privates->setCols( 'Private', 'Class' ); $privates->setColWidth( 'Private', 36, 1 ); @@ -474,34 +502,11 @@ sub setup_actions { }; $walker->( $walker, $self->tree, '' ); - $class->log->debug( "Loaded private actions:\n" . $privates->draw ) + $class->log->debug( "Loaded Private actions:\n" . $privates->draw ) if ( @{ $privates->{tbl_rows} } ); - my $publics = Text::ASCIITable->new; - $publics->setCols( 'Public', 'Private' ); - $publics->setColWidth( 'Public', 36, 1 ); - $publics->setColWidth( 'Private', 37, 1 ); - - for my $plain ( sort keys %{ $actions->{plain} } ) { - my $action = $actions->{plain}->{$plain}; - $publics->addRow( "/$plain", "/$action" ); - } - - $class->log->debug( "Loaded public actions:\n" . $publics->draw ) - if ( @{ $publics->{tbl_rows} } ); - - my $regexes = Text::ASCIITable->new; - $regexes->setCols( 'Regex', 'Private' ); - $regexes->setColWidth( 'Regex', 36, 1 ); - $regexes->setColWidth( 'Private', 37, 1 ); - - for my $regex ( sort keys %{ $actions->{regex} } ) { - my $action = $actions->{regex}->{$regex}; - $regexes->addRow( $regex, "/$action" ); - } - - $class->log->debug( "Loaded regex actions:\n" . $regexes->draw ) - if ( @{ $regexes->{tbl_rows} } ); + # List all public actions + $_->list($class) for @{ $self->dispatch_types }; } =back