X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FDispatcher.pm;h=66e87210ecc712af9ee79796c1f3a9f43da8269d;hb=5d1266aecbc3b839f0b904093ccf282a73e06c91;hp=7bad0f6f5f97ed6e248428624056f5318e335791;hpb=a9cbd748a5bef5badba4a3f012e5df75cccd60ca;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Dispatcher.pm b/lib/Catalyst/Dispatcher.pm index 7bad0f6..66e8721 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 @@ -248,18 +253,32 @@ sub prepare_action { sub get_action { my ( $self, $c, $action, $namespace, $inherit ) = @_; return [] unless $action; - $namespace ||= '/'; - $inherit ||= 0; + $namespace ||= ''; + $namespace = '' if $namespace eq '/'; + $inherit ||= 0; my @match = $self->get_containers($namespace); - my @results; + if ($inherit) { # Return [ [ $act_obj ], ... ] for valid containers + return [ + map { [ $_->{$action} ] } # Make [ $action_obj ] + grep { defined $_->{$action} } # If it exists in the container + map { $_->actions } # Get action hash for container + @match + ]; + } + else { + my $node = $match[-1]->actions; # Only bother looking at the last one - foreach my $child ( $inherit ? @match : $match[-1] ) { - my $node = $child->actions; - push( @results, [ $node->{$action} ] ) if defined $node->{$action}; + if ( defined $node->{$action} + && ( $node->{$action}->prefix eq $namespace ) ) + { + return [ [ $node->{$action} ] ]; + } + else { + return []; + } } - return \@results; } =item $self->get_containers( $namespace ) @@ -320,7 +339,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/ ); } @@ -393,18 +424,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 +462,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 );