revised documentation for 5.0
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine.pm
index 5b3e3d5..57e73ee 100644 (file)
@@ -47,6 +47,9 @@ See L<Catalyst>.
 
 =head1 DESCRIPTION
 
+This is the core of catalyst. The various drivers are subclasses
+of this class.
+
 =head1 METHODS
 
 =over 4
@@ -71,6 +74,8 @@ sub benchmark {
 
 =item $c->comp($name)
 
+Shortcut for $c->component
+
 =item $c->component($name)
 
 Get a component object by name.
@@ -121,13 +126,13 @@ sub dispatch {
         $c->state(1);
         if ( my $begin = @{ $c->get_action( 'begin', $namespace ) }[-1] ) {
             $c->execute( @{ $begin->[0] } );
-            return if scalar @{$c->error};
-            last unless $c->state;
+            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;
         }
 
@@ -141,8 +146,7 @@ sub dispatch {
         # Execute last end
         if ( my $end = @{ $c->get_action( 'end', $namespace ) }[-1] ) {
             $c->execute( @{ $end->[0] } );
-            return if scalar @{$c->error};
-            last unless $c->state;            
+            return if scalar @{ $c->error };
         }
     }
     else {
@@ -204,8 +208,12 @@ 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);
         $c->state(0);
@@ -215,7 +223,8 @@ sub execute {
 
 =item $c->finalize
 
-Finalize request.
+Finalize request. This function can typically be overloaded with
+NEXT by plugins that need to do something at the end of the request.
 
 =cut
 
@@ -273,7 +282,8 @@ sub finalize_cookies {
 
 =item $c->finalize_error
 
-Finalize error.
+This is the default error screen displayed from finalize. Override
+with your own output if you need something special.
 
 =cut
 
@@ -376,7 +386,7 @@ sub finalize_error {
 
 =item $c->finalize_headers
 
-Finalize headers.
+Finalize headers. Null action by default.
 
 =cut
 
@@ -384,7 +394,7 @@ sub finalize_headers { }
 
 =item $c->finalize_output
 
-Finalize output.
+Finalize output. Null action by default
 
 =cut
 
@@ -396,6 +406,7 @@ Forward processing to a private action or a method from a class.
 If you define a class without method it will default to process().
 
     $c->forward('/foo');
+    $c->forward('/controller/action');
     $c->forward('index');
     $c->forward(qw/MyApp::Model::CDBI::Foo do_stuff/);
     $c->forward('MyApp::View::TT');
@@ -410,6 +421,7 @@ sub forward {
         return 0;
     }
     my $caller    = caller(0);
+    my $global    = $command =~ /^\// ? 0 : 1;
     my $namespace = '/';
     if ( $command =~ /^\// ) {
         $command =~ /^(.*)\/(\w+)$/;
@@ -417,7 +429,7 @@ sub forward {
         $command = $2;
     }
     else { $namespace = _class2prefix($caller) || '/' }
-    my $results = $c->get_action( $command, $namespace );
+    my $results = $c->get_action( $command, $namespace, $global );
     unless ( @{$results} ) {
         my $class = $command;
         if ( $class =~ /[^\w\:]/ ) {
@@ -437,37 +449,50 @@ sub forward {
     }
     for my $result ( @{$results} ) {
         $c->execute( @{ $result->[0] } );
+        return if scalar @{ $c->error };
+        last unless $c->state;
     }
     return $c->state;
 }
 
-=item $c->get_action( $action, $namespace )
+=item $c->get_action( $action, $namespace, $global )
 
 Get an action in a given namespace.
 
 =cut
 
 sub get_action {
-    my ( $c, $action, $namespace ) = @_;
+    my ( $c, $action, $namespace, $global ) = @_;
     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;
+        if ($global) {
+            my @results;
+            for my $uid ( keys %{ $c->actions->{private} } ) {
+                if ( my $result = $c->actions->{private}->{$uid}->{$action} ) {
+                    push @results, [$result];
+                }
+            }
+            return \@results;
+        }
+        else {
+            $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;
+            }
+            return \@results;
         }
-        return \@results;
     }
     elsif ( my $p = $c->actions->{plain}->{$action} ) { return [ [$p] ] }
     elsif ( my $r = $c->actions->{regex}->{$action} ) { return [ [$r] ] }
@@ -491,7 +516,7 @@ sub get_action {
 
 =item $c->handler( $class, $r )
 
-Handles the request.
+The main request handler.
 
 =cut
 
@@ -599,7 +624,7 @@ sub prepare {
 
 =item $c->prepare_action
 
-Prepare action.
+Prepare action for processing.
 
 =cut
 
@@ -644,7 +669,7 @@ sub prepare_action {
 
 =item $c->prepare_connection
 
-Prepare connection.
+Prepare connection. Null action by default
 
 =cut
 
@@ -652,7 +677,7 @@ sub prepare_connection { }
 
 =item $c->prepare_cookies
 
-Prepare cookies.
+Prepare cookies. 
 
 =cut
 
@@ -666,7 +691,7 @@ sub prepare_cookies {
 
 =item $c->prepare_headers
 
-Prepare headers.
+Prepare headers. Null action by default
 
 =cut
 
@@ -674,7 +699,7 @@ sub prepare_headers { }
 
 =item $c->prepare_parameters
 
-Prepare parameters.
+Prepare parameters. Null action by default
 
 =cut
 
@@ -682,7 +707,7 @@ sub prepare_parameters { }
 
 =item $c->prepare_path
 
-Prepare path and base.
+Prepare path and base. Null action by default
 
 =cut
 
@@ -690,7 +715,7 @@ sub prepare_path { }
 
 =item $c->prepare_request
 
-Prepare the engine request.
+Prepare the engine request. Null action by default
 
 =cut
 
@@ -698,7 +723,7 @@ sub prepare_request { }
 
 =item $c->prepare_uploads
 
-Prepare uploads.
+Prepare uploads.  Null action by default
 
 =cut
 
@@ -706,31 +731,36 @@ sub prepare_uploads { }
 
 =item $c->run
 
-Starts the engine.
+Starts the engine. Null action by default
 
 =cut
 
 sub run { }
 
-=item $c->request
-
 =item $c->req
 
-Returns a C<Catalyst::Request> object.
+Shortcut for $c->request
 
-    my $req = $c->req;
+=item $c->request
 
-=item $c->response
+Returns a C<Catalyst::Request> object.
+
+    my $req = $c->request;
 
 =item $c->res
 
+Shortcut for $c->response
+
+=item $c->response
+
 Returns a C<Catalyst::Response> object.
 
     my $res = $c->res;
 
 =item $c->set_action( $action, $code, $namespace, $attrs )
 
-Set an action in a given namespace.
+Set an action in a given namespace. Used to defined the actions
+in the attribute handlers.
 
 =cut
 
@@ -791,7 +821,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} ) {
@@ -804,7 +834,7 @@ sub set_action {
 
 =item $class->setup
 
-Setup.
+Setup the application. required to initialize actions.
 
     MyApp->setup;
 
@@ -856,7 +886,7 @@ sub setup_actions {
 
 =item $class->setup_components
 
-Setup components.
+Setup all the components in YourApp::(M|V|C|Model|View|Controller)::*
 
 =cut
 
@@ -947,7 +977,8 @@ Contains the return value of the last executed action.
 
 =item $c->stash
 
-Returns a hashref containing all your data.
+The stash is a global hash which can be used to pass around data
+between your components.
 
     $c->stash->{foo} ||= 'yada';
     print $c->stash->{foo};
@@ -984,9 +1015,22 @@ sub _class2prefix {
 
 =back
 
+=head1 SEE ALSO
+
+=over 4
+
+=item L<Catalyst::Engine::Apache> - Apache Engines for MP1/2
+=item L<Catalyst::Engine::CGI>    - CGI Engine
+=item L<Catalyst::Engine::FCGI>   - FastCGI Engine
+=item L<Catalyst::Engine::HTTP>   - Standalone Catalyst Server
+=item L<Catalyst::Engine::Test>   - Engine for testing
+
+=back
+
 =head1 AUTHOR
 
 Sebastian Riedel, C<sri@cpan.org>
+Marcus Ramberg, C<mramberg@cpan.org>
 
 =head1 COPYRIGHT