Added Catalyst::Request::Upload
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine.pm
index 8dee01f..95f7058 100644 (file)
@@ -10,9 +10,11 @@ use HTTP::Headers;
 use Memoize;
 use Time::HiRes qw/gettimeofday tv_interval/;
 use Text::ASCIITable;
+use Text::ASCIITable::Wrap 'wrap';
 use Tree::Simple;
 use Tree::Simple::Visitor::FindByPath;
 use Catalyst::Request;
+use Catalyst::Request::Upload;
 use Catalyst::Response;
 
 require Module::Pluggable::Fast;
@@ -94,6 +96,65 @@ sub component {
     }
 }
 
+=item $c->dispatch
+
+Dispatch request to actions.
+
+=cut
+
+sub dispatch {
+    my $c         = shift;
+    my $action    = $c->req->action;
+    my $namespace = '';
+    $namespace = ( join( '/', @{ $c->req->args } ) || '/' )
+      if $action eq 'default';
+    unless ($namespace) {
+        if ( my $result = $c->get_action($action) ) {
+            $namespace = _class2prefix( $result->[0]->[0]->[0] );
+        }
+    }
+    my $default = $action eq 'default' ? $namespace : undef;
+    my $results = $c->get_action( $action, $default );
+    $namespace ||= '/';
+    if ( @{$results} ) {
+
+        # Execute last begin
+        $c->state(1);
+        if ( my $begin = @{ $c->get_action( 'begin', $namespace ) }[-1] ) {
+            $c->execute( @{ $begin->[0] } );
+            return if scalar @{ $c->error };
+        }
+
+        # Execute the auto chain
+        for my $auto ( @{ $c->get_action( 'auto', $namespace ) } ) {
+            $c->execute( @{ $auto->[0] } );
+            return if scalar @{ $c->error };
+            last unless $c->state;
+        }
+
+        # Execute the action or last default
+        if ( ( my $action = $c->req->action ) && $c->state ) {
+            if ( my $result = @{ $c->get_action( $action, $default ) }[-1] ) {
+                $c->execute( @{ $result->[0] } );
+            }
+        }
+
+        # Execute last end
+        if ( my $end = @{ $c->get_action( 'end', $namespace ) }[-1] ) {
+            $c->execute( @{ $end->[0] } );
+            return if scalar @{ $c->error };
+        }
+    }
+    else {
+        my $path  = $c->req->path;
+        my $error = $path
+          ? qq/Unknown resource "$path"/
+          : "No default action defined";
+        $c->log->error($error) if $c->debug;
+        $c->error($error);
+    }
+}
+
 =item $c->error
 
 =item $c->error($error, ...)
@@ -134,7 +195,7 @@ sub execute {
         {
             my $action = $c->actions->{reverse}->{"$code"};
             $action = "/$action" unless $action =~ /\-\>/;
-            $action = "    $action" if $callsub =~ /forward$/;
+            $action = "-> $action" if $callsub =~ /forward$/;
             my ( $elapsed, @state ) =
               $c->benchmark( $code, $class, $c, @{ $c->req->args } );
             push @{ $c->{stats} }, [ $action, sprintf( '%fs', $elapsed ) ];
@@ -143,10 +204,14 @@ sub execute {
         else { $c->state( &$code( $class, $c, @{ $c->req->args } ) ) }
     };
     if ( my $error = $@ ) {
-        chomp $error;
-        $error = qq/Caught exception "$error"/;
+
+        unless ( ref $error ) {
+            chomp $error;
+            $error = qq/Caught exception "$error"/;
+        }
+
         $c->log->error($error);
-        $c->error($error) if $c->debug;
+        $c->error($error);
         $c->state(0);
     }
     return $c->state;
@@ -166,7 +231,7 @@ sub finalize {
     if ( my $location = $c->response->redirect ) {
         $c->log->debug(qq/Redirecting to "$location"/) if $c->debug;
         $c->response->header( Location => $location );
-        $c->response->status(302) if $c->response->status !~ /3\d\d$/;
+        $c->response->status(302) if $c->response->status !~ /^3\d\d$/;
     }
 
     if ( $#{ $c->error } >= 0 ) {
@@ -375,7 +440,9 @@ sub forward {
         }
     }
     for my $result ( @{$results} ) {
-        $c->state( $c->execute( @{ $result->[0] } ) );
+        $c->execute( @{ $result->[0] } );
+        return if scalar @{ $c->error };
+        last unless $c->state;
     }
     return $c->state;
 }
@@ -388,22 +455,43 @@ Get an action in a given namespace.
 
 sub get_action {
     my ( $c, $action, $namespace ) = @_;
+    return [] unless $action;
     $namespace ||= '';
     if ($namespace) {
         $namespace = '' if $namespace eq '/';
         my $parent = $c->tree;
         my @results;
-        my $result = $c->actions->{private}->{ $parent->getUID }->{$action};
-        push @results, [$result] if $result;
-        my $visitor = Tree::Simple::Visitor::FindByPath->new;
-        for my $part ( split '/', $namespace ) {
-            $visitor->setSearchPath($part);
-            $parent->accept($visitor);
-            my $child = $visitor->getResult;
-            my $uid   = $child->getUID if $child;
-            my $match = $c->actions->{private}->{$uid}->{$action} if $uid;
-            push @results, [$match] if $match;
-            $parent = $child if $child;
+        my %allowed = ( begin => 1, auto => 1, default => 1, end => 1 );
+        if ( $allowed{$action} ) {
+            my $result = $c->actions->{private}->{ $parent->getUID }->{$action};
+            push @results, [$result] if $result;
+            my $visitor = Tree::Simple::Visitor::FindByPath->new;
+            for my $part ( split '/', $namespace ) {
+                $visitor->setSearchPath($part);
+                $parent->accept($visitor);
+                my $child = $visitor->getResult;
+                my $uid   = $child->getUID if $child;
+                my $match = $c->actions->{private}->{$uid}->{$action} if $uid;
+                push @results, [$match] if $match;
+                $parent = $child if $child;
+            }
+        }
+        else {
+            if ($namespace) {
+                my $visitor = Tree::Simple::Visitor::FindByPath->new;
+                $visitor->setSearchPath( split '/', $namespace );
+                $parent->accept($visitor);
+                my $child = $visitor->getResult;
+                my $uid   = $child->getUID if $child;
+                my $match = $c->actions->{private}->{$uid}->{$action}
+                  if $uid;
+                push @results, [$match] if $match;
+            }
+            else {
+                my $result =
+                  $c->actions->{private}->{ $parent->getUID }->{$action};
+                push @results, [$result] if $result;
+            }
         }
         return \@results;
     }
@@ -443,40 +531,7 @@ sub handler {
         my $handler = sub {
             my $c = $class->prepare($engine);
             $c->{stats} = \@stats;
-            my $action    = $c->req->action;
-            my $namespace = '';
-            $namespace = ( join( '/', @{ $c->req->args } ) || '/' )
-              if $action eq 'default';
-            unless ($namespace) {
-                if ( my $result = $c->get_action($action) ) {
-                    $namespace = _class2prefix( $result->[0]->[0]->[0] );
-                }
-            }
-            my $default = $action eq 'default' ? $namespace : undef;
-            my $results = $c->get_action( $action, $default );
-            $namespace ||= '/';
-            if ( @{$results} ) {
-                for my $begin ( @{ $c->get_action( 'begin', $namespace ) } ) {
-                    $c->state( $c->execute( @{ $begin->[0] } ) );
-                }
-                for my $result ( @{ $c->get_action( $action, $default ) }[-1] )
-                {
-                    $c->state( $c->execute( @{ $result->[0] } ) );
-                    last unless $default;
-                }
-                for my $end ( reverse @{ $c->get_action( 'end', $namespace ) } )
-                {
-                    $c->state( $c->execute( @{ $end->[0] } ) );
-                }
-            }
-            else {
-                my $path  = $c->req->path;
-                my $error = $path
-                  ? qq/Unknown resource "$path"/
-                  : "No default action defined";
-                $c->log->error($error) if $c->debug;
-                $c->error($error);
-            }
+            $c->dispatch;
             return $c->finalize;
         };
         if ( $class->debug ) {
@@ -486,8 +541,11 @@ sub handler {
             my $av = sprintf '%.3f', 1 / $elapsed;
             my $t = Text::ASCIITable->new;
             $t->setCols( 'Action', 'Time' );
+            $t->setColWidth( 'Action', 64, 1 );
+            $t->setColWidth( 'Time',   9,  1 );
+
             for my $stat (@stats) {
-                $t->addRow(@$stat);
+                $t->addRow( wrap( $stat->[0], 64 ), wrap( $stat->[1], 9 ) );
             }
             $class->log->info( "Request took $elapsed" . "s ($av/s)",
                 $t->draw );
@@ -531,9 +589,9 @@ sub prepare {
     if ( $c->debug ) {
         my $secs = time - $START || 1;
         my $av = sprintf '%.3f', $COUNT / $secs;
-        $c->log->debug('********************************');
+        $c->log->debug('**********************************');
         $c->log->debug("* Request $COUNT ($av/s) [$$]");
-        $c->log->debug('********************************');
+        $c->log->debug('**********************************');
         $c->res->headers->header( 'X-Catalyst' => $Catalyst::VERSION );
     }
     $c->prepare_request($r);
@@ -553,9 +611,11 @@ sub prepare {
     if ( $c->debug && keys %{ $c->req->params } ) {
         my $t = Text::ASCIITable->new;
         $t->setCols( 'Key', 'Value' );
+        $t->setColWidth( 'Key',   37, 1 );
+        $t->setColWidth( 'Value', 36, 1 );
         for my $key ( keys %{ $c->req->params } ) {
             my $value = $c->req->params->{$key} || '';
-            $t->addRow( $key, $value );
+            $t->addRow( wrap( $key, 37 ), wrap( $value, 36 ) );
         }
         $c->log->debug( 'Parameters are', $t->draw );
     }
@@ -757,7 +817,7 @@ sub set_action {
             $absolute = 1;
         }
         $absolute = 1 if $flags{global};
-        my $name = $absolute ? $path : "$prefix/$path";
+        my $name = $absolute ? $path : $prefix ? "$prefix/$path" : $path;
         $c->actions->{plain}->{$name} = [ $namespace, $code ];
     }
     if ( my $regex = $flags{regex} ) {
@@ -852,14 +912,18 @@ sub setup_components {
         $self->components->{ ref $comp } = $comp;
         $self->setup_actions($comp);
     }
-    my $t = Text::ASCIITable->new;
+    my $t = Text::ASCIITable->new( { hide_HeadRow => 1, hide_HeadLine => 1 } );
     $t->setCols('Class');
-    $t->addRow($_) for keys %{ $self->components };
+    $t->setColWidth( 'Class', 75, 1 );
+    $t->addRow( wrap( $_, 75 ) ) for keys %{ $self->components };
     $self->log->debug( 'Loaded components', $t->draw )
       if ( @{ $t->{tbl_rows} } && $self->debug );
     my $actions  = $self->actions;
     my $privates = Text::ASCIITable->new;
-    $privates->setCols( 'Action', 'Class', 'Code' );
+    $privates->setCols( 'Private', 'Class', 'Code' );
+    $privates->setColWidth( 'Private', 28, 1 );
+    $privates->setColWidth( 'Class',   28, 1 );
+    $privates->setColWidth( 'Code',    14, 1 );
     my $walker = sub {
         my ( $walker, $parent, $prefix ) = @_;
         $prefix .= $parent->getNodeValue || '';
@@ -867,7 +931,11 @@ sub setup_components {
         my $uid = $parent->getUID;
         for my $action ( keys %{ $actions->{private}->{$uid} } ) {
             my ( $class, $code ) = @{ $actions->{private}->{$uid}->{$action} };
-            $privates->addRow( "$prefix$action", $class, $code );
+            $privates->addRow(
+                wrap( "$prefix$action", 28 ),
+                wrap( $class,           28 ),
+                wrap( $code,            14 )
+            );
         }
         $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
     };
@@ -875,23 +943,34 @@ sub setup_components {
     $self->log->debug( 'Loaded private actions', $privates->draw )
       if ( @{ $privates->{tbl_rows} } && $self->debug );
     my $publics = Text::ASCIITable->new;
-    $publics->setCols( 'Action', 'Class', 'Code' );
+    $publics->setCols( 'Public', 'Private' );
+    $publics->setColWidth( 'Public',  37, 1 );
+    $publics->setColWidth( 'Private', 36, 1 );
+
     for my $plain ( sort keys %{ $actions->{plain} } ) {
         my ( $class, $code ) = @{ $actions->{plain}->{$plain} };
-        $publics->addRow( "/$plain", $class, $code );
+        $publics->addRow( wrap( "/$plain", 37 ),
+            wrap( $self->actions->{reverse}->{$code} || $code, 36 ) );
     }
     $self->log->debug( 'Loaded public actions', $publics->draw )
       if ( @{ $publics->{tbl_rows} } && $self->debug );
     my $regexes = Text::ASCIITable->new;
-    $regexes->setCols( 'Action', 'Class', 'Code' );
+    $regexes->setCols( 'Regex', 'Private' );
+    $regexes->setColWidth( 'Regex',   37, 1 );
+    $regexes->setColWidth( 'Private', 36, 1 );
     for my $regex ( sort keys %{ $actions->{regex} } ) {
         my ( $class, $code ) = @{ $actions->{regex}->{$regex} };
-        $regexes->addRow( $regex, $class, $code );
+        $regexes->addRow( wrap( $regex, 37 ),
+            wrap( $self->actions->{reverse}->{$class} || $class, 36 ) );
     }
     $self->log->debug( 'Loaded regex actions', $regexes->draw )
       if ( @{ $regexes->{tbl_rows} } && $self->debug );
 }
 
+=item $c->state
+
+Contains the return value of the last executed action.
+
 =item $c->stash
 
 Returns a hashref containing all your data.