and again
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine.pm
index 3b4b758..4259d7c 100644 (file)
@@ -118,27 +118,27 @@ sub component {
     }
 }
 
-=item $c->errors
+=item $c->error
 
-=item $c->errors($error, ...)
+=item $c->error($error, ...)
 
-=item $c->errors($arrayref)
+=item $c->error($arrayref)
 
-Returns an arrayref containing errors messages.
+Returns an arrayref containing error messages.
 
-    my @errors = @{ $c->errors };
+    my @error = @{ $c->error };
 
 Add a new error.
 
-    $c->errors('Something bad happened');
+    $c->error('Something bad happened');
 
 =cut
 
-sub errors {
+sub error {
     my $c = shift;
-    my $errors = ref $_[0] eq 'ARRAY' ? $_[0] : [@_];
-    push @{ $c->{errors} }, @$errors;
-    return $c->{errors};
+    my $error = ref $_[0] eq 'ARRAY' ? $_[0] : [@_];
+    push @{ $c->{error} }, @$error;
+    return $c->{error};
 }
 
 =item $c->finalize
@@ -156,13 +156,13 @@ sub finalize {
         $c->res->status(302);
     }
 
-    if ( !$c->res->output || $#{ $c->errors } >= 0 ) {
+    if ( !$c->res->output || $#{ $c->error } >= 0 ) {
         $c->res->headers->content_type('text/html');
         my $name = $c->config->{name} || 'Catalyst Application';
-        my ( $title, $errors, $infos );
+        my ( $title, $error, $infos );
         if ( $c->debug ) {
-            $errors = join '<br/>', @{ $c->errors };
-            $errors ||= 'No output';
+            $error = join '<br/>', @{ $c->error };
+            $error ||= 'No output';
             $title = $name = "$name on Catalyst $Catalyst::VERSION";
             my $req   = encode_entities Dumper $c->req;
             my $res   = encode_entities Dumper $c->res;
@@ -178,9 +178,9 @@ sub finalize {
 
         }
         else {
-            $title  = $name;
-            $errors = '';
-            $infos  = <<"";
+            $title = $name;
+            $error = '';
+            $infos = <<"";
 <pre>
 (en) Please come back later
 (de) Bitte versuchen sie es spaeter nocheinmal
@@ -214,7 +214,7 @@ sub finalize {
                 margin: 10px;
                 -moz-border-radius: 10px;
             }
-            div.errors {
+            div.error {
                 background-color: #977;
                 border: 1px solid #755;
                 padding: 8px;
@@ -241,7 +241,7 @@ sub finalize {
     </head>
     <body>
         <div class="box">
-            <div class="errors">$errors</div>
+            <div class="error">$error</div>
             <div class="infos">$infos</div>
             <div class="name">$name</div>
         </div>
@@ -301,22 +301,13 @@ sub forward {
     }
     my $results = $c->get_action( $command, $namespace );
     if ( @{$results} ) {
-        if ( $command =~ /^\!/ ) {
-            for my $result ( @{$results} ) {
-                my ( $class, $code ) = @{ $result->[0] };
-                $c->state( $c->process( $class, $code ) );
-            }
-        }
-        else {
-            return 0 unless my $result = $results->[0];
-            if ( $result->[2] ) {
+        unless ( $command =~ /^\!/ ) {
+            $results = [ pop @{$results} ];
+            if ( $results->[0]->[2] ) {
                 $c->log->debug(qq/Couldn't forward "$command" to regex action/)
                   if $c->debug;
                 return 0;
             }
-            my ( $class, $code ) = @{ $result->[0] };
-            $class = $c->components->{$class} || $class;
-            $c->state( $c->process( $class, $code ) );
         }
     }
     else {
@@ -328,8 +319,7 @@ sub forward {
         my $method = shift || 'process';
         if ( my $code = $class->can($method) ) {
             $c->actions->{reverse}->{"$code"} = "$class->$method";
-            $class = $c->comp($class) || $class;
-            $c->state( $c->process( $class, $code ) );
+            $results = [ [ [ $class, $code ] ] ];
         }
         else {
             $c->log->debug(qq/Couldn't forward to "$class->$method"/)
@@ -337,6 +327,9 @@ sub forward {
             return 0;
         }
     }
+    for my $result ( @{$results} ) {
+        $c->state( $c->process( @{ $result->[0] } ) );
+    }
     return $c->state;
 }
 
@@ -356,15 +349,20 @@ sub get_action {
         my $result = $c->actions->{private}->{ $parent->getUID }->{$action};
         push @results, [$result] if $result;
         my $visitor = Tree::Simple::Visitor::FindByPath->new;
+        my $local;
         for my $part ( split '/', $namespace ) {
+            $local = undef;
             $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;
+            return [ [$match] ] if ( $match && $match =~ /^?.*/ );
+            $local = $c->actions->{private}->{$uid}->{"?$action"} if $uid;
             push @results, [$match] if $match;
             $parent = $child if $child;
         }
+        return [ [$local] ] if $local;
         return \@results;
     }
     elsif ( my $p = $c->actions->{plain}->{$action} ) { return [ [$p] ] }
@@ -415,6 +413,7 @@ sub handler ($$) {
                 }
                 for my $result ( @{ $c->get_action( $action, $namespace ) } ) {
                     $c->state( $c->process( @{ $result->[0] } ) );
+                    last unless $action =~ /^\!.*/;
                 }
                 for my $end ( @{ $c->get_action( '!end', $namespace ) } ) {
                     $c->state( $c->process( @{ $end->[0] } ) );
@@ -426,7 +425,7 @@ sub handler ($$) {
                   ? qq/Unknown resource "$path"/
                   : "No default action defined";
                 $c->log->error($error) if $c->debug;
-                $c->errors($error);
+                $c->error($error);
             }
             return $c->finalize;
         };
@@ -449,7 +448,8 @@ sub handler ($$) {
 
 =item $c->prepare($r)
 
-Turns the engine-specific request (Apache, CGI...) into a Catalyst context.
+Turns the engine-specific request( Apache, CGI ... )
+into a Catalyst context .
 
 =cut
 
@@ -482,8 +482,8 @@ sub prepare {
     }
     $c->prepare_request($r);
     $c->prepare_path;
-    $c->prepare_headers;
     $c->prepare_cookies;
+    $c->prepare_headers;
     $c->prepare_connection;
     my $method   = $c->req->method   || '';
     my $path     = $c->req->path     || '';
@@ -609,33 +609,34 @@ sub prepare_uploads { }
 =item $c->process($class, $coderef)
 
 Process a coderef in given class and catch exceptions.
-Errors are available via $c->errors.
+Errors are available via $c->error.
 
 =cut
 
 sub process {
     my ( $c, $class, $code ) = @_;
-    my $status;
+    $class = $c->comp($class) || $class;
+    $c->state(0);
     eval {
         if ( $c->debug )
         {
             my $action = $c->actions->{reverse}->{"$code"} || "$code";
-            my $elapsed;
-            ( $elapsed, $status ) =
+            my ( $elapsed, @state ) =
               $c->benchmark( $code, $class, $c, @{ $c->req->args } );
             $c->log->info( sprintf qq/Processing "$action" took %fs/, $elapsed )
               if $c->debug;
+            $c->state(@state);
         }
-        else { $status = &$code( $class, $c, @{ $c->req->args } ) }
+        else { $c->state( &$code( $class, $c, @{ $c->req->args } ) ) }
     };
     if ( my $error = $@ ) {
         chomp $error;
         $error = qq/Caught exception "$error"/;
         $c->log->error($error);
-        $c->errors($error) if $c->debug;
-        return 0;
+        $c->error($error) if $c->debug;
+        $c->state(0);
     }
-    return $status;
+    return $c->state;
 }
 
 =item $c->run
@@ -670,7 +671,6 @@ Set an action in a given namespace.
 
 sub set_action {
     my ( $c, $action, $code, $namespace ) = @_;
-
     my $prefix = '';
     if ( $action =~ /^\?(.*)$/ ) {
         my $prefix = $1 || '';
@@ -678,7 +678,7 @@ sub set_action {
         $action = $prefix . _prefix( $namespace, $action );
         $c->actions->{plain}->{$action} = [ $namespace, $code ];
     }
-    if ( $action =~ /^\/(.*)\/$/ ) {
+    elsif ( $action =~ /^\/(.*)\/$/ ) {
         my $regex = $1;
         $c->actions->{compiled}->{qr#$regex#} = $action;
         $c->actions->{regex}->{$action} = [ $namespace, $code ];
@@ -704,13 +704,9 @@ sub set_action {
         $c->actions->{private}->{$uid}->{$action} = [ $namespace, $code ];
         $action = "!$action";
     }
-    else {
-        $c->actions->{plain}->{$action} = [ $namespace, $code ];
-    }
-
+    else { $c->actions->{plain}->{$action} = [ $namespace, $code ] }
     my $reverse = $prefix ? "$action ($prefix)" : $action;
     $c->actions->{reverse}->{"$code"} = $reverse;
-
     $c->log->debug(qq/"$namespace" defined "$action" as "$code"/)
       if $c->debug;
 }
@@ -791,6 +787,7 @@ sub stash {
 sub _prefix {
     my ( $class, $name ) = @_;
     my $prefix = _class2prefix($class);
+    warn "$class - $name - $prefix";
     $name = "$prefix/$name" if $prefix;
     return $name;
 }