- Refactored get_action into get_action and get_actions
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Base.pm
index 6bda121..0703b5c 100644 (file)
@@ -6,59 +6,50 @@ use base qw/Catalyst::AttrContainer Class::Accessor::Fast/;
 use Catalyst::Exception;
 use NEXT;
 
-__PACKAGE__->mk_classdata($_) for qw/_config/;
+__PACKAGE__->mk_classdata($_) for qw/_config _dispatch_steps/;
 
-sub _DISPATCH :Private {
+__PACKAGE__->_dispatch_steps( [qw/_BEGIN _AUTO _ACTION/] );
+
+sub _DISPATCH : Private {
     my ( $self, $c ) = @_;
-    my @containers = $c->dispatcher->get_containers( $c->namespace );
-    my %actions;
-    foreach my $name (qw/begin auto end/) {
-
-        # Go down the container list representing each part of the
-        # current namespace inheritance tree, grabbing the actions hash
-        # of the ActionContainer object and looking for actions of the
-        # appropriate name registered to the namespace
-
-        $actions{$name} = [
-            map    { $_->{$name} }
-              grep { exists $_->{$name} }
-              map  { $_->actions } @containers
-        ];
+
+    foreach my $disp ( @{ $self->_dispatch_steps } ) {
+        last unless $c->forward($disp);
     }
 
-    # Errors break the normal flow and the end action is instantly run
-    my $error = 0;
+    $c->forward('_END');
+}
 
-    # Execute last begin
-    $c->state(1);
-    if ( my $begin = @{ $actions{begin} }[-1] ) {
-        $begin->execute($c);
-        $error++ if scalar @{ $c->error };
-    }
+sub _BEGIN : Private {
+    my ( $self, $c ) = @_;
+    my $begin = ($c->get_actions( 'begin', $c->namespace))[-1];
+    return 1 unless $begin;
+    $begin->execute($c);
+    return !@{ $c->error };
+}
 
-    # Execute the auto chain
-    my $autorun = 0;
-    for my $auto ( @{ $actions{auto} } ) {
-        last if $error;
-        $autorun++;
+sub _AUTO : Private {
+    my ( $self, $c ) = @_;
+    my @auto = $c->get_actions('auto', $c->namespace);
+    foreach my $auto (@auto) {
         $auto->execute($c);
-        $error++ if scalar @{ $c->error };
-        last unless $c->state;
+        return 0 unless $c->state;
     }
+    return 1;
+}
 
-    # Execute the action or last default
-    my $mkay = $autorun ? $c->state ? 1 : 0 : 1;
-    if ($mkay) {
-        unless ($error) {
-            $c->action->execute($c);
-            $error++ if scalar @{ $c->error };
-        }
-    }
+sub _ACTION : Private {
+    my ( $self, $c ) = @_;
+    $c->action->execute($c);
+    return !@{ $c->error };
+}
 
-    # Execute last end
-    if ( my $end = @{ $actions{end} }[-1] ) {
-        $end->execute($c);
-    }
+sub _END : Private {
+    my ( $self, $c ) = @_;
+    my $end = ($c->get_actions( 'end', $c->namespace))[-1];
+    return 1 unless $end;
+    $end->execute($c);
+    return !@{ $c->error };
 }
 
 =head1 NAME
@@ -112,11 +103,11 @@ component loader with config() support and a process() method placeholder.
 
 sub new {
     my ( $self, $c ) = @_;
+
     # Temporary fix, some components does not pass context to constructor
     my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
 
-    return $self->NEXT::new( { %{ $self->config }, %{ $arguments } } );
+    return $self->NEXT::new( { %{ $self->config }, %{$arguments} } );
 }
 
 # remember to leave blank lines between the consecutive =item's
@@ -148,15 +139,10 @@ sub config {
 
 sub process {
 
-    Catalyst::Exception->throw( 
-        message => ( ref $_[0] || $_[0] ) . " did not override Catalyst::Base::process"
-    );
+    Catalyst::Exception->throw( message => ( ref $_[0] || $_[0] )
+          . " did not override Catalyst::Base::process" );
 }
 
-=item FETCH_CODE_ATTRIBUTES
-
-=item MODIFY_CODE_ATTRIBUTES
-
 =back
 
 =head1 SEE ALSO