Gantry/State/Exceptions.pm untested

filter: Statementstmt 0.0% 0 / 151 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 258 CC 21 SCAR 29.0
CC
21
Cov
0%
state_run
72.0
perform_action
20.0
relocate
6.0
CRAP 18.2
1 package Gantry::State::Exceptions;
2 require Exporter;
3
4 use Switch;
5 use Gantry::Exception;
6 use Gantry::State::Constants;
7
8 use strict;
9 use warnings;
10 use vars qw( @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS );
11
12 ############################################################
13 # Variables #
14 ############################################################
15
16 @ISA = qw( Exporter );
17 @EXPORT = qw(
18 state_run
19 state_engine
20 relocate
21 relocate_permanently
22 );
23
24 @EXPORT_OK = qw( );
25
26 my ( @p, $p1 );
27
28 ############################################################
29 # Functions #
30 ############################################################
31
32 #-------------------------------------------------
33 # $self->state_run( r_or_cgi, plugin_callbacks )
34 #-------------------------------------------------
35 sub state_run {
36 my ( $self, $r_or_cgi, $plugin_callbacks ) = @_;
37
38 my $X;
39 my $status = 200;
40 my $state = STATE_POST_ENGINE_INIT;
41
42 eval {
43
44 while ($state) {
45
46 switch ($state) {
47 case STATE_POST_ENGINE_INIT {
48 $state = post_engine_init($self, $plugin_callbacks);
49 }
50 case STATE_PRE_INIT {
51 $state = pre_init($self, $r_or_cgi, $plugin_callbacks);
52 }
53 case STATE_INIT {
54 $state = initialize($self, $r_or_cgi);
55 }
56 case STATE_POST_INIT {
57 $state = post_init($self, $plugin_callbacks);
58 }
59 case STATE_CACHED_PAGES {
60 $state = cached_pages($self);
61 }
62 case STATE_PRE_ACTION {
63 $state = pre_action($self, $plugin_callbacks);
64 }
65 case STATE_ACTION {
66 $state = perform_action($self);
67 }
68 case STATE_POST_ACTION {
69 $state = post_action($self, $plugin_callbacks);
70 }
71 case STATE_SET_HEADERS {
72 $state = set_headers($self);
73 }
74 case STATE_PRE_PROCESS {
75 $state = pre_process($self, $plugin_callbacks);
76 }
77 case STATE_PROCESS {
78 $state = process_template($self);
79 }
80 case STATE_POST_PROCESS {
81 $state = post_process($self, $plugin_callbacks);
82 }
83 case STATE_OUTPUT {
84 $state = send_output($self);
85 }
86 case STATE_SEND_STATUS {
87 $status = $self->status ? $self->status : $self->success_code ;
88 $state = STATE_FINI;
89 }
90 };
91
92 }
93
94 }; if ($X = $@) {
95
96 my $ref = ref($X);
97
98 if ($ref && $X->isa('Gantry::Exception::Redirect')) {
99
100 $self->header_out('location', "$X");
101 $self->redirect_response();
102 $status = $self->status_const('REDIRECT');
103
104 } elsif ($ref && $X->isa('Gantry::Exception::RedirectPermanently')) {
105
106 $self->header_out('location', "$X");
107 $self->redirect_response();
108 $status = $self->status_const('MOVED_PERMANENTLY');
109
110 } elsif ($ref && $X->isa('Gantry::Exception::Declined')) {
111
112 $status = $self->declined_response($self->action());
113
114 } elsif ($ref && $X->isa('Gantry::Exception')) {
115
116 if ($self->can('exception_handler')) {
117
118 $status = $self->exception_handler($X);
119
120 } else {
121
122 warn "Unexpected exception caught:\n";
123 warn " status = " . $X->status . "\n";
124 warn " message = " . $X->message . "\n";
125 $status = 500;
126
127 }
128
129 } else {
130
131 # Call do_error and return
132
133 $self->do_error($X);
134 $status = $self->cast_custom_error($self->custom_error($X), $X);
135
136 }
137
138 }
139
140 return $status;
141
142 }
143
144 #-------------------------------------------------
145 # $self->relocate( $location )
146 #-------------------------------------------------
147 sub relocate {
148 my ( $self, $location ) = ( shift, shift );
149
150 $location = $self->location if ( ! defined $location );
151 Gantry::Exception::Redirect->throw($location);
152
153 } # end relocate
154
155 #-------------------------------------------------
156 # $self->relocate_permanently( $location )
157 #-------------------------------------------------
158 sub relocate_permanently {
159 my ( $self, $location ) = ( shift, shift );
160
161 $location = $self->location if ( ! defined $location );
162 Gantry::Exception::RedirectPermanently->throw($location);
163
164 } # end relocate_permanently
165
166 #-------------------------------------------------
167 # $self->state_engine
168 #-------------------------------------------------
169 sub state_engine {
170 return __PACKAGE__;
171
172 } # end state_engine
173
174 #-------------------------------------------------
175 # Private methods
176 #-------------------------------------------------
177
178 sub post_engine_init {
179 my ($self, $plugin_callbacks) = @_;
180
181 # Do the plugin callbacks for the 'post_engine_init' phase
182
183 if (defined $plugin_callbacks->{ $self->namespace }->{ post_engine_init }) {
184
185 foreach my $cb (
186 @{ $plugin_callbacks->{ $self->namespace }->{ post_engine_init } }
187 ) {
188 $cb->( $self );
189 }
190
191 }
192
193 return STATE_PRE_INIT;
194
195 }
196
197 sub pre_init {
198 my ($self, $r_or_cgi, $plugin_callbacks) = @_;
199
200 # Do the plugin callbacks for the 'pre_init' phase
201
202 if (defined $plugin_callbacks->{ $self->namespace }->{ pre_init }) {
203
204 foreach my $cb (
205 @{ $plugin_callbacks->{ $self->namespace }->{ pre_init } }
206 ) {
207 $cb->( $self, $r_or_cgi );
208 }
209
210 }
211
212 return STATE_INIT;
213
214 }
215
216 sub initialize {
217 my ($self, $r_or_cgi) = @_;
218
219 $self->init( $r_or_cgi );
220
221 @p = $self->cleanroot( $self->dispatch_location() );
222 $p1 = ( shift( @p ) || 'main' );
223
224 # set the action
225 $self->action( 'do_'. $p1 );
226
227 return STATE_POST_INIT;
228
229 }
230
231 sub post_init {
232 my ($self, $plugin_callbacks) = @_;
233
234 # Do the plugin callbacks for the 'post_init' phase
235
236 if (defined $plugin_callbacks->{ $self->namespace }->{ post_init }) {
237
238 foreach my $cb (
239 @{ $plugin_callbacks->{ $self->namespace }->{ post_init } }
240 ) {
241 $cb->( $self );
242 }
243
244 }
245
246 return STATE_CACHED_PAGES;
247
248 }
249
250 sub cached_pages {
251 my ($self) = @_;
252
253 # check for response page -- used primarily for caching
254
255 if ($self->gantry_response_page()) {
256
257 $self->set_content_type();
258 $self->set_no_cache();
259 $self->status($self->success_code());
260 return STATE_OUTPUT;
261
262 }
263
264 return STATE_PRE_ACTION;
265
266 }
267
268 sub pre_action {
269 my ($self, $plugin_callbacks) = @_;
270
271 # Do the plugin callbacks for the 'pre_action' phase
272
273 if (defined $plugin_callbacks->{ $self->namespace }->{ pre_action }) {
274
275 foreach my $cb (
276 @{ $plugin_callbacks->{ $self->namespace }->{ pre_action } }
277 ) {
278 $cb->( $self );
279 }
280
281 }
282
283 return STATE_ACTION;
284
285 }
286
287
288 sub perform_action {
289 my ($self) = @_;
290
291 # Do action if valid
292
293 if ($self->can($self->action())) {
294
295 $self->do_action($self->action(), @p);
296 $self->cleanup(); # Cleanup.
297
298 } elsif ($self->can('do_default')) {
299
300 # try the default action
301
302 $self->do_action('do_default', $p1, @p);
303 $self->cleanup();
304
305 } else {
306
307 # else return declined
308
309 Gantry::Exception::Declined->throw();
310
311 }
312
313 Gantry::Exception::Declined->throw() if ($self->is_status_declined());
314
315 return STATE_POST_ACTION;
316
317 }
318
319 sub post_action {
320 my ($self, $plugin_callbacks) = @_;
321
322 # Do the plugin callbacks for the 'post_action' phase
323
324 if (defined $plugin_callbacks->{ $self->namespace }->{ post_action }) {
325
326 foreach my $cb (
327 @{ $plugin_callbacks->{ $self->namespace }->{ post_action } }
328 ) {
329 $cb->( $self );
330 }
331
332 }
333
334 return STATE_SET_HEADERS;
335
336 }
337
338 sub set_headers {
339 my ($self) = @_;
340
341 # set http headers
342
343 $self->set_content_type();
344 $self->set_no_cache();
345
346 return STATE_PRE_PROCESS;
347
348 }
349
350 sub pre_process {
351 my ($self, $plugin_callbacks) = @_;
352
353 # Do the plugin callbacks for the 'pre_process' phase
354
355 if (defined $plugin_callbacks->{ $self->namespace }->{ pre_process }) {
356
357 foreach my $cb (
358 @{ $plugin_callbacks->{ $self->namespace }->{ pre_process } }
359 ) {
360 $cb->( $self );
361 }
362
363 }
364
365 return STATE_PROCESS;
366
367 }
368
369 sub process_template {
370 my ($self) = @_;
371
372 $self->gantry_response_page( $self->do_process() || '' );
373
374 return STATE_POST_PROCESS;
375
376 }
377
378 sub post_process {
379 my ($self, $plugin_callbacks) = @_;
380
381 # Do the plugin callbacks for the 'post_process' phase
382
383 if (defined $plugin_callbacks->{ $self->namespace }->{ post_process }) {
384
385 foreach my $cb (
386 @{ $plugin_callbacks->{ $self->namespace }->{ post_process } }
387 ) {
388 $cb->( $self );
389 }
390
391 }
392
393 return STATE_OUTPUT;
394
395 }
396
397 sub send_output {
398 my ($self) = @_;
399
400 $self->send_http_header();
401 $self->print_output( $self->gantry_response_page() );
402 $self->status($self->status_const('OK'));
403
404 return STATE_SEND_STATUS;
405
406 }
407
408 1;
409
410 __END__