# 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/;
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;
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}";
}
unless ( $result ) {
- unless ( $c->components->{$command} ) {
+ my $comp;
+
+ unless ( $comp = $c->component($command) ) {
my $error =
qq/Couldn't forward to command "$command". Invalid action or component./;
$c->error($error);
return 0;
}
- my $class = $command;
+ my $class = ref $comp;
my $method = shift || 'process';
- if ( my $code = $c->components->{$class}->can($method) ) {
+ if ( my $code = $class->can($method) ) {
my $action = Catalyst::Action->new(
{
name => $method,
}
local $c->request->{arguments} = [ @{$arguments} ];
+ local $c->{namespace} = $result->namespace;
$result->execute($c);
=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;
}
}
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 )
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 };
-
- 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;
+sub register {
+ my ( $self, $c, $action ) = @_;
- 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 ) {
}
}
- 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 } ) {
my ( $self, $c ) = @_;
$self->dispatch_types( [] );
+ $self->registered_dispatch_types( {} );
# Preload action types
for my $type (@PRELOAD) {
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
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;
-
- while ( my $class = shift @cache ) {
- $classes{$class}++;
- for my $isa ( @{"$class\::ISA"} ) {
- next if $classes{$isa};
- push @cache, $isa;
- $classes{$isa}++;
- }
- }
+ $c->register_actions( $c );
- 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
=head1 AUTHOR
Sebastian Riedel, C<sri@cpan.org>
+Matt S Trout, C<mst@shadowcatsystems.co.uk>
=head1 COPYRIGHT