[Catalyst-commits] r11733 - Catalyst-Runtime/5.80/branches/refactoring_dispatcher/lib/Catalyst

jnapiorkowski at dev.catalyst.perl.org jnapiorkowski at dev.catalyst.perl.org
Wed Nov 4 19:01:02 GMT 2009


Author: jnapiorkowski
Date: 2009-11-04 19:01:02 +0000 (Wed, 04 Nov 2009)
New Revision: 11733

Modified:
   Catalyst-Runtime/5.80/branches/refactoring_dispatcher/lib/Catalyst/Dispatcher.pm
Log:
refactored prepare_action to make one big method into several smaller ones

Modified: Catalyst-Runtime/5.80/branches/refactoring_dispatcher/lib/Catalyst/Dispatcher.pm
===================================================================
--- Catalyst-Runtime/5.80/branches/refactoring_dispatcher/lib/Catalyst/Dispatcher.pm	2009-11-04 18:24:07 UTC (rev 11732)
+++ Catalyst-Runtime/5.80/branches/refactoring_dispatcher/lib/Catalyst/Dispatcher.pm	2009-11-04 19:01:02 UTC (rev 11733)
@@ -359,38 +359,63 @@
 sub prepare_action {
     my ( $self, $c ) = @_;
     my $req = $c->req;
-    my $path = $req->path;
-    my @path = split /\//, $req->path;
-    $req->args( \my @args );
+    my @path = $self->decompose_path_for_prepare_action($c, $req->path);
+    my $args = $self->dispatch_against_paths($c, \@path);
+    $req->args(@$args);
 
-    unshift( @path, '' );    # Root action
+    s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg
+      for grep { defined } @{$req->captures||[]};
 
-  DESCEND: while (@path) {
-        $path = join '/', @path;
-        $path =~ s#^/+##;
+    $c->log->debug( 'Path is "' . $req->match . '"' )
+      if ( $c->debug && defined $req->match && length $req->match );
 
-        # Check out dispatch types to see if any will handle the path at
-        # this level
+    $c->log->debug( 'Arguments are "' . join( '/', @$args ) . '"' )
+      if ( $c->debug && @$args );
+}
 
-        foreach my $type ( @{ $self->dispatch_types } ) {
-            last DESCEND if $type->match( $c, $path );
-        }
-
+sub dispatch_against_paths {
+    my ($self, $c, $paths, $args) = (@_, []);
+    my $path = join '/', @$paths;
+    $path =~ s#^/+##;
+    if($self->match_dispatch_types_to_path($c, $path, @{ $self->_dispatch_types })) {
+        return $args; ## all done
+    } else {
         # If not, move the last part path to args
-        my $arg = pop(@path);
+        my $arg = pop(@$paths);
         $arg =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
-        unshift @args, $arg;
+        unshift @$args, $arg;
+        if(@$paths) {
+            return $self->dispatch_against_paths($c, $paths, $args);
+        } else {
+            return $args;
+        }
     }
+}
 
-    s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg for grep { defined } @{$req->captures||[]};
+sub match_dispatch_types_to_path {
+    my ($self, $c, $path, $dispatch_type, @dispatch_types) = @_;
+    if(my $match = $self->match_dispatch_type_to_path($c, $path, $dispatch_type)) {
+        return $match;
+    } elsif(@dispatch_types) {
+        return $self->match_dispatch_types_to_path($c, $path, @dispatch_types);
+    } else {
+        return;
+    }
+}
 
-    $c->log->debug( 'Path is "' . $req->match . '"' )
-      if ( $c->debug && defined $req->match && length $req->match );
-
-    $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
-      if ( $c->debug && @args );
+sub match_dispatch_type_to_path {
+    my ($self, $c, $path, $dispatch_type) = @_;
+    return $dispatch_type->match($c, $path);
 }
+	
+sub decompose_path_for_prepare_action {
+    my ($self, $c, $path) = @_;
+    my @path = (split(/\//, $path));
+    unshift( @path, '' ); ## Root action
+    return @path;
+}
 
+
 =head2 $self->get_action( $action, $namespace )
 
 returns a named action from a given namespace.




More information about the Catalyst-commits mailing list