- Shifted Path dispatch into a DispatchType and nuked old set_action stuff
Matt S Trout [Sat, 22 Oct 2005 05:02:54 +0000 (05:02 +0000)]
lib/Catalyst/Action.pm
lib/Catalyst/DispatchType/Path.pm [new file with mode: 0644]
lib/Catalyst/DispatchType/Regex.pm
lib/Catalyst/Dispatcher.pm

index 807a6db..f80c00b 100644 (file)
@@ -3,7 +3,7 @@ package Catalyst::Action;
 use strict;
 use base qw/Class::Accessor::Fast/;
 
-__PACKAGE__->mk_accessors(qw/code namespace reverse prefix attributes/);
+__PACKAGE__->mk_accessors(qw/code namespace reverse prefix attributes name/);
 
 use overload (
 
@@ -46,6 +46,8 @@ sub execute {    # Execute ourselves against a context
 
 =item reverse
 
+=item name
+
 =item new
 
 =cut
diff --git a/lib/Catalyst/DispatchType/Path.pm b/lib/Catalyst/DispatchType/Path.pm
new file mode 100644 (file)
index 0000000..f51387c
--- /dev/null
@@ -0,0 +1,45 @@
+package Catalyst::DispatchType::Path;
+
+use strict;
+use base qw/Catalyst::DispatchType/;
+
+sub prepare_action {
+    my ($self, $c, $path) = @_;
+
+    if ( my $action = $self->{paths}->{$path} ) {
+        $c->req->action($path);
+        $c->req->match($path);
+        $c->action($action);
+        $c->namespace($action->prefix);
+        return 1;
+    }
+
+    return 0;
+}
+
+sub register_action {
+    my ( $self, $c, $action ) = @_;
+    my $attrs = $action->attributes;
+    my @register;
+    foreach my $r (@{$attrs->{Path} || []}) {
+        unless ($r =~ m!^/!) {
+            $r = $action->prefix."/$r";
+        }
+        push(@register, $r);
+    }
+
+    if ($attrs->{Global} || $attrs->{Absolute}) {
+        push(@register, $action->name);
+    }
+
+    if ($attrs->{Local} || $attrs->{Relative}) {
+        push(@register, join('/', $action->prefix, $action->name));
+    }
+
+    foreach my $r (@register) {
+        $r =~ s!^/!!;
+        $self->{paths}{$r} = $action;
+    }
+}
+
+1;
index 120ae93..8f99f75 100644 (file)
@@ -1,18 +1,12 @@
 package Catalyst::DispatchType::Regex;
 
 use strict;
-use base qw/Catalyst::DispatchType/;
+use base qw/Catalyst::DispatchType::Path/;
 
 sub prepare_action {
     my ($self, $c, $path) = @_;
 
-    if ( my $action = $self->{paths}->{$path} ) {
-        $c->req->action($path);
-        $c->req->match($path);
-        $c->action($action);
-        $c->namespace($action->prefix);
-        return 1;
-    }
+    return if $self->SUPER::prepare_action($c, $path);
 
     foreach my $compiled (@{$self->{compiled}||[]}) {
         if ( my @snippets = ( $path =~ $compiled->{re} ) ) {
index d15f227..e129748 100644 (file)
@@ -6,6 +6,7 @@ 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 Text::ASCIITable;
@@ -175,6 +176,7 @@ qq/Couldn't forward to command "$command". Invalid action or component./;
         if ( my $code = $c->components->{$class}->can($method) ) {
             my $action = Catalyst::Action->new(
                 {
+                    name      => $method,
                     code      => $code,
                     reverse   => "$class->$method",
                     namespace => $class,
@@ -218,22 +220,9 @@ sub prepare_action {
 
   DESCEND: while (@path) {
         $path = join '/', @path;
-        if ( my $result = ${ $c->get_action($path) }[0] ) {
 
-            $c->req->action($path);
-            $c->log->debug(qq/Requested action is "$path"/) if $c->debug;
-
-            $c->req->match($path);
-            $c->action($result->[0]);
-            $c->namespace($result->[0]->prefix);
-            last DESCEND;
-        }
-
-        unless ( $c->action ) {
-            foreach my $type (@{$self->dispatch_types}) {
-                last DESCEND if $type->prepare_action($c, $path);
-                #last DESCEND if $c->action;
-            }
+        foreach my $type (@{$self->dispatch_types}) {
+            last DESCEND if $type->prepare_action($c, $path);
         }
 
         unshift @args, pop @path;
@@ -266,8 +255,6 @@ sub get_action {
         return \@results;
     }
 
-    elsif ( my $p = $self->actions->{plain}->{$action} ) { return [ [$p] ] }
-
     return [];
 }
 
@@ -322,19 +309,9 @@ sub set_action {
     my $prefix =
       Catalyst::Utils::class2prefix( $namespace, $c->config->{case_sensitive} )
       || '';
-    my %flags;
     my %attributes;
 
     for my $attr ( @{$attrs} ) {
-        if    ( $attr =~ /^(Local|Relative)$/ )    { $flags{local}++ }
-        elsif ( $attr =~ /^(Global|Absolute)$/ )   { $flags{global}++ }
-        elsif ( $attr =~ /^Path\(\s*(.+)\s*\)$/i ) {
-            push @{ $flags{path} }, $1;
-        }
-        elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
-        elsif ( $attr =~ /^(Regex|Regexp)\(\s*(.+)\s*\)$/i ) {
-            push @{ $flags{regex} }, $2;
-        }
         if ( my ($key, $value) = ($attr =~ /^(.*?)(?:\(\s*(.+)\s*\))?$/) ) {
             if ( defined $value ) {
                 ($value =~ s/^'(.*)'$/$1/) || ($value =~ s/^"(.*)"/$1/);
@@ -343,14 +320,14 @@ sub set_action {
         }
     }
 
-    if ( $flags{private} && ( keys %flags > 1 ) ) {
+    if ( $attributes{Private} && ( keys %attributes > 1 ) ) {
         $c->log->debug( 'Bad action definition "'
               . join( ' ', @{$attrs} )
               . qq/" for "$namespace->$method"/ )
           if $c->debug;
         return;
     }
-    return unless keys %flags;
+    return unless keys %attributes;
 
     my $parent  = $self->tree;
     my $visitor = Tree::Simple::Visitor::FindByPath->new;
@@ -382,6 +359,7 @@ sub set_action {
 
     my $action = Catalyst::Action->new(
         {
+            name       => $method,
             code       => $code,
             reverse    => $reverse,
             namespace  => $namespace,
@@ -393,29 +371,6 @@ sub set_action {
     # Set the method value
     $parent->getNodeValue->actions->{$method} = $action;
 
-    my @path;
-    for my $path ( @{ $flags{path} } ) {
-        $path =~ s/^\w+//;
-        $path =~ s/\w+$//;
-        if ( $path =~ /^\s*'(.*)'\s*$/ ) { $path = $1 }
-        if ( $path =~ /^\s*"(.*)"\s*$/ ) { $path = $1 }
-        push @path, $path;
-    }
-    $flags{path} = \@path;
-
-    if ( $flags{local} || $flags{global} ) {
-        push( @{ $flags{path} }, $prefix ? "/$prefix/$method" : "/$method" )
-          if $flags{local};
-
-        push( @{ $flags{path} }, "/$method" ) if $flags{global};
-    }
-
-    for my $path ( @{ $flags{path} } ) {
-        if ( $path =~ /^\// ) { $path =~ s/^\/// }
-        else { $path = $prefix ? "$prefix/$path" : $path }
-        $self->actions->{plain}->{$path} = $action;
-    }
-
     foreach my $type ( @{ $self->dispatch_types } ) {
         $type->register_action($c, $action);
     }
@@ -440,7 +395,7 @@ sub setup_actions {
 
     $self->dispatch_types([
         map { "Catalyst::DispatchType::$_"->new }
-            qw/Regex Default/ ]);
+            qw/Path Regex Default/ ]);
 
     # We use a tree
     my $container = Catalyst::ActionContainer->new(