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