X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=blobdiff_plain;f=lib%2FCatalyst%2FDispatcher.pm;h=851a3b0bec281a0f01d350ad83614ed2100d662b;hp=cd56ad4d8c51bcf5ed9467999ac46254ce99c31c;hb=158c88c0db581dcee202cd854c77186590c2992c;hpb=a9dc674c99f36ff40d94b80753a1504074ba5e22 diff --git a/lib/Catalyst/Dispatcher.pm b/lib/Catalyst/Dispatcher.pm index cd56ad4..851a3b0 100644 --- a/lib/Catalyst/Dispatcher.pm +++ b/lib/Catalyst/Dispatcher.pm @@ -15,7 +15,7 @@ use Tree::Simple::Visitor::FindByPath; # Stringify to class use overload '""' => sub { return ref shift }, fallback => 1; -__PACKAGE__->mk_accessors(qw/tree dispatch_types/); +__PACKAGE__->mk_accessors(qw/tree dispatch_types registered_dispatch_types/); # Preload these action types our @PRELOAD = qw/Path Regex/; @@ -82,10 +82,6 @@ sub forward { return 0; } - # Relative forwards from detach - my $caller = ( caller(1) )[0]->isa('Catalyst::Dispatcher') - && ( ( caller(2) )[3] =~ /::detach$/ ) ? caller(3) : caller(1); - my $arguments = ( ref( $_[-1] ) eq 'ARRAY' ) ? pop(@_) : $c->req->args; my $result; @@ -93,9 +89,7 @@ sub forward { my $command_copy = $command; unless ( $command_copy =~ s/^\/// ) { - my $namespace = - Catalyst::Utils::class2prefix( $caller, $c->config->{case_sensitive} ) - || ''; + my $namespace = $c->namespace; $command_copy = "${namespace}/${command}"; } @@ -154,6 +148,7 @@ qq/Couldn't forward to command "$command". Invalid action or component./; } local $c->request->{arguments} = [ @{$arguments} ]; + local $c->{namespace} = $result->namespace; $result->execute($c); @@ -198,19 +193,18 @@ sub prepare_action { =cut sub get_action { - my ( $self, $c, $action, $namespace ) = @_; - return [] unless $action; + my ( $self, $c, $name, $namespace ) = @_; + return unless $name; $namespace ||= ''; $namespace = '' if $namespace eq '/'; my @match = $self->get_containers($namespace); - my $node = $match[-1]->actions; # Only bother looking at the last one + return unless @match; - if ( defined $node->{$action} - && ( $node->{$action}->namespace eq $namespace ) ) + if ( my $action = $match[-1]->get_action( $c, $name ) ) { - return $node->{$action}; + return $action if $action->namespace eq $namespace; } } @@ -226,11 +220,7 @@ sub get_actions { my @match = $self->get_containers($namespace); - return - map { $_->{$action} } - grep { defined $_->{$action} } # If it exists in the container - map { $_->actions } # Get action hash for container - @match + return map { $_->get_action($c, $action) } @match; } =item $self->get_containers( $namespace ) @@ -275,53 +265,12 @@ sub get_containers { return map { $_->getNodeValue } @match; } -=item $self->set_action( $c, $action, $code, $class, $attrs ) - -=cut - -sub set_action { - my ( $self, $c, $method, $code, $class, $attrs ) = @_; - - my $namespace = - Catalyst::Utils::class2prefix( $class, $c->config->{case_sensitive} ) - || ''; - my %attributes; - - for my $attr ( @{$attrs} ) { - - # Parse out :Foo(bar) into Foo => bar etc (and arrayify) - - my %initialized; - $initialized{ ref $_ }++ for @{ $self->dispatch_types }; +sub register { + my ( $self, $c, $action ) = @_; - 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/ ); - } - push( @{ $attributes{$key} }, $value ); - } - } - - if ( $attributes{Private} && ( keys %attributes > 1 ) ) { - $c->log->debug( 'Bad action definition "' - . join( ' ', @{$attrs} ) - . qq/" for "$class->$method"/ ) - if $c->debug; - return; - } - return unless keys %attributes; - - my $parent = $self->tree; - my $visitor = Tree::Simple::Visitor::FindByPath->new; + my $namespace = $action->namespace; + my $parent = $self->tree; + my $visitor = Tree::Simple::Visitor::FindByPath->new; if ($namespace) { for my $part ( split '/', $namespace ) { @@ -347,21 +296,19 @@ sub set_action { } } - my $reverse = $namespace ? "$namespace/$method" : $method; + # Set the method value + $parent->getNodeValue->actions->{$action->name} = $action; - my $action = Catalyst::Action->new( - { - name => $method, - code => $code, - reverse => $reverse, - namespace => $namespace, - class => $class, - attributes => \%attributes, - } - ); + my $registered = $self->registered_dispatch_types; - # Set the method value - $parent->getNodeValue->actions->{$method} = $action; + foreach my $key (keys %{$action->attributes}) { + my $class = "Catalyst::DispatchType::$key"; + unless ( $registered->{$class} ) { + eval "require $class"; + push( @{ $self->dispatch_types }, $class->new ) unless $@; + $registered->{$class} = 1; + } + } # Pass the action to our dispatch types so they can register it if reqd. foreach my $type ( @{ $self->dispatch_types } ) { @@ -377,6 +324,7 @@ sub setup_actions { my ( $self, $c ) = @_; $self->dispatch_types( [] ); + $self->registered_dispatch_types( {} ); # Preload action types for my $type (@PRELOAD) { @@ -385,6 +333,7 @@ sub setup_actions { Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ ) if $@; push @{ $self->dispatch_types }, $class->new; + $self->registered_dispatch_types->{$class} = 1; } # We use a tree @@ -392,37 +341,10 @@ sub setup_actions { Catalyst::ActionContainer->new( { part => '/', actions => {} } ); $self->tree( Tree::Simple->new( $container, Tree::Simple->ROOT ) ); - for my $comp ( keys %{ $c->components } ) { - - # We only setup components that inherit from Catalyst::Base - next unless $comp->isa('Catalyst::Base'); - - for my $action ( @{ Catalyst::Utils::reflect_actions($comp) } ) { - my ( $code, $attrs ) = @{$action}; - my $name = ''; - no strict 'refs'; - my @cache = ( $comp, @{"$comp\::ISA"} ); - my %classes; + $c->register_actions( $c ); - while ( my $class = shift @cache ) { - $classes{$class}++; - for my $isa ( @{"$class\::ISA"} ) { - next if $classes{$isa}; - push @cache, $isa; - $classes{$isa}++; - } - } - - for my $class ( keys %classes ) { - for my $sym ( values %{ $class . '::' } ) { - if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) { - $name = *{$sym}{NAME}; - $self->set_action( $c, $name, $code, $comp, $attrs ); - last; - } - } - } - } + foreach my $comp ( values %{$c->components} ) { + $comp->register_actions( $c ) if $comp->can('register_actions'); } # Postload action types @@ -471,6 +393,7 @@ sub setup_actions { =head1 AUTHOR Sebastian Riedel, C +Matt S Trout, C =head1 COPYRIGHT