]> Dogcows Code - chaz/p5-CGI-Ex/blobdiff - lib/CGI/Ex/App.pm
CGI::Ex 2.16
[chaz/p5-CGI-Ex] / lib / CGI / Ex / App.pm
index ec3ff6a976aee7370cebe4645d266abedfefb26d..2c7ecee6a078da536049cb7a93441245b7b67ba9 100644 (file)
@@ -10,7 +10,7 @@ use strict;
 use vars qw($VERSION);
 
 BEGIN {
-    $VERSION = '2.15';
+    $VERSION = '2.16';
 
     Time::HiRes->import('time') if eval {require Time::HiRes};
     eval {require Scalar::Util};
@@ -54,7 +54,7 @@ sub navigate {
 
         ### run the step loop
         eval {
-            local $self->{'__morph_lineage_start_index'} = $#{$self->{'__morph_lineage'} || []};
+            local $self->{'_morph_lineage_start_index'} = $#{$self->{'_morph_lineage'} || []};
             $self->nav_loop;
         };
         if ($@) {
@@ -80,8 +80,8 @@ sub nav_loop {
     my $self = shift;
 
     ### keep from an infinate nesting
-    local $self->{'recurse'} = $self->{'recurse'} || 0;
-    if ($self->{'recurse'} ++ >= $self->recurse_limit) {
+    local $self->{'_recurse'} = $self->{'_recurse'} || 0;
+    if ($self->{'_recurse'}++ >= $self->recurse_limit) {
         my $err = "recurse_limit (".$self->recurse_limit.") reached";
         $err .= " number of jumps (".$self->{'jumps'}.")" if ($self->{'jumps'} || 0) > 1;
         croak $err;
@@ -170,15 +170,22 @@ sub handle_error {
   my $self = shift;
   my $err  = shift;
 
-  die $err;
+  die $err if $self->{'_handling_error'};
+  local $self->{'_handling_error'} = 1;
+  local $self->{'_recurse'} = 0; # allow for this next step - even if we hit a recurse error
+
+  $self->stash->{'error_step'} = $self->current_step;
+  $self->stash->{'error'}      = $err;
+  $self->replace_path($self->error_step);
+  $self->jump; # exits nav loop when finished
 }
 
 ###----------------------------------------------------------------###
 
-sub default_step { shift->{'default_step'} || 'main' }
-
-sub js_step { shift->{'js_step'} || 'js' }
-
+sub default_step   { shift->{'default_step'}   || 'main'    }
+sub js_step        { shift->{'js_step'}        || 'js'      }
+sub login_step     { shift->{'login_step'}     || '__login' }
+sub error_step     { shift->{'error_step'}     || '__error' }
 sub forbidden_step { shift->{'forbidden_step'} || '__forbidden' }
 
 sub step_key { shift->{'step_key'} || 'step' }
@@ -277,9 +284,9 @@ sub exit_nav_loop {
     my $self = shift;
 
     ### undo morphs
-    if (my $ref = $self->{'__morph_lineage'}) {
+    if (my $ref = $self->{'_morph_lineage'}) {
         ### use the saved index - this allows for early "morphers" to only get rolled back so far
-        my $index = $self->{'__morph_lineage_start_index'};
+        my $index = $self->{'_morph_lineage_start_index'};
         $index = -1 if ! defined $index;
         $self->unmorph while $#$ref != $index;
     }
@@ -497,7 +504,7 @@ sub morph {
     my $allow = $self->allow_morph($step) || return;
 
     ### place to store the lineage
-    my $lin = $self->{'__morph_lineage'} ||= [];
+    my $lin = $self->{'_morph_lineage'} ||= [];
     my $cur = ref $self; # what are we currently
     push @$lin, $cur;    # store so subsequent unmorph calls can do the right thing
 
@@ -555,12 +562,12 @@ sub morph {
 
 sub unmorph {
     my $self = shift;
-    my $step = shift || '__no_step';
-    my $lin  = $self->{'__morph_lineage'} || return;
+    my $step = shift || '_no_step';
+    my $lin  = $self->{'_morph_lineage'} || return;
     my $cur  = ref $self;
 
     my $prev = pop(@$lin) || croak "unmorph called more times than morph - current ($cur)";
-    delete $self->{'__morph_lineage'} if ! @$lin;
+    delete $self->{'_morph_lineage'} if ! @$lin;
 
     ### if we are not already that package - bless us there
     my $hist = {
@@ -642,7 +649,7 @@ sub get_valid_auth {
     $args->{'cleanup_user'}     ||= sub { my ($auth, $user) = @_; $self->cleanup_user(    $user, $auth) };
     $args->{'login_print'}      ||= sub {
         my ($auth, $template, $hash) = @_;
-        my $step = '__login';
+        my $step = $self->login_step;
         my $hash_base = $self->run_hook('hash_base',   $step) || {};
         my $hash_comm = $self->run_hook('hash_common', $step) || {};
         my $hash_swap = $self->run_hook('hash_swap',   $step) || {};
@@ -732,8 +739,8 @@ sub clear_app {
         path
         path_i
         history
-        __morph_lineage_start_index
-        __morph_lineage
+        _morph_lineage_start_index
+        _morph_lineage
         hash_errors
         hash_fill
         hash_swap
@@ -818,11 +825,8 @@ sub prepared_print {
 
 sub print {
     my ($self, $step, $swap, $fill) = @_;
-
     my $file = $self->run_hook('file_print', $step); # get a filename relative to base_dir_abs
-
     my $out  = $self->run_hook('swap_template', $step, $file, $swap);
-
     $self->run_hook('fill_template', $step, \$out, $fill);
     $self->run_hook('print_out',     $step, \$out);
 }
@@ -831,24 +835,17 @@ sub print_out {
     my ($self, $step, $out) = @_;
 
     $self->cgix->print_content_type;
-    print ref($out) ? $$out : $out;
+    print ref($out) eq 'SCALAR' ? $$out : $out;
 }
 
 sub swap_template {
     my ($self, $step, $file, $swap) = @_;
 
     my $args = $self->run_hook('template_args', $step);
-    my $copy = $self;
-    eval {require Scalar::Util; Scalar::Util::weaken($copy)};
-    $args->{'INCLUDE_PATH'} ||= sub {
-        my $dir = $copy->base_dir_abs || die "Could not find base_dir_abs while looking for template INCLUDE_PATH on step \"$step\"";
-        $dir = $dir->() if UNIVERSAL::isa($dir, 'CODE');
-        return $dir;
-    };
+    $args->{'INCLUDE_PATH'} ||= $self->base_dir_abs;
 
-    my $t   = $self->template_obj($args);
+    my $t = $self->template_obj($args);
     my $out = '';
-
     $t->process($file, $swap, \$out) || die $t->error;
 
     return $out;
@@ -1107,25 +1104,25 @@ sub add_to_hash {
 
 sub base_dir_rel {
     my $self = shift;
-    $self->{'base_dir_rel'} = shift if $#_ != -1;
+    $self->{'base_dir_rel'} = shift if @_ == 1;
     return $self->{'base_dir_rel'} || '';
 }
 
 sub base_dir_abs {
     my $self = shift;
-    $self->{'base_dir_abs'} = shift if $#_ != -1;
-    return $self->{'base_dir_abs'} || '';
+    $self->{'base_dir_abs'} = shift if @_ == 1;
+    return $self->{'base_dir_abs'} || ['.']; # default to the current directory
 }
 
 sub ext_print {
     my $self = shift;
-    $self->{'ext_print'} = shift if $#_ != -1;
+    $self->{'ext_print'} = shift if @_ == 1;
     return $self->{'ext_print'} || 'html';
 }
 
 sub ext_val {
     my $self = shift;
-    $self->{'ext_val'} = shift if $#_ != -1;
+    $self->{'ext_val'} = shift if @_ == 1;
     return $self->{'ext_val'} || 'val';
 }
 
@@ -1164,10 +1161,19 @@ sub js_run_step {
 
 sub __forbidden_info_complete { 0 }
 
-sub __forbidden_hash_swap { {forbidden_step => shift->stash->{'forbidden_step'}} }
+sub __forbidden_hash_swap { shift->stash }
 
 sub __forbidden_file_print { \ "<h1>Denied</h1>You do not have access to the step <b>\"[% forbidden_step %]\"</b>" }
 
+###----------------------------------------------------------------###
+### a step that is used by the default handle_error
+
+sub __error_info_complete { 0 }
+
+sub __error_hash_swap { shift->stash }
+
+sub __error_file_print { \ "<h1>An a fatal error occurred</h1>Step: <b>\"[% error_step %]\"</b><br>[% TRY; CONFIG DUMP => {header => 0}; DUMP error; END %]" }
+
 ###----------------------------------------------------------------###
 
 1;
This page took 0.024689 seconds and 4 git commands to generate.