| 1 | package OpenFrame::AppKit::Segment::SessionLoader; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings::register; | ||
| 5 | |||
| 6 | use Pipeline::Segment; | ||
| 7 | use OpenFrame::Cookie; | ||
| 8 | use OpenFrame::Cookies; | ||
| 9 | use OpenFrame::AppKit::Session; | ||
| 10 | |||
| 11 | our $VERSION=3.03; | ||
| 12 | |||
| 13 | use base qw ( Pipeline::Segment ); | ||
| 14 | |||
| 15 | sub dispatch { | ||
| 16 | my $self = shift; | ||
| 17 | my $pipe = shift; | ||
| 18 | |||
| 19 | my ($session, $id); | ||
| 20 | |||
| 21 | ## get the cookie container | ||
| 22 | my $cookies = $pipe->store->get('OpenFrame::Cookies'); | ||
| 23 | ## get the session cookie | ||
| 24 | if ( $cookies ) { | ||
| 25 | my $scookie = $cookies->get('session'); | ||
| 26 | ## get the value of the session cookie | ||
| 27 | $id = $scookie->value() if $scookie; | ||
| 28 | } else { | ||
| 29 | $pipe->store->set( OpenFrame::Cookies->new() ); | ||
| 30 | } | ||
| 31 | |||
| 32 | ## if all that has left us with an id, we have a session to fetch | ||
| 33 | if ($id) { | ||
| 34 | $session = OpenFrame::AppKit::Session->fetch( $id ); | ||
| 35 | } | ||
| 36 | |||
| 37 | ## if we still don't have a session... | ||
| 38 | if (!$session) { | ||
| 39 | ## create a new session | ||
| 40 | $session = OpenFrame::AppKit::Session->new(); | ||
| 41 | |||
| 42 | ## create a cookie to keep track on the client side | ||
| 43 | my $cookie = OpenFrame::Cookie->new(); | ||
| 44 | $cookie->name('session'); | ||
| 45 | $cookie->value( [ $session->id ] ); | ||
| 46 | $pipe->store->get('OpenFrame::Cookies')->set( $cookie ); | ||
| 47 | } | ||
| 48 | |||
| 49 | return ( | ||
| 50 | $session, | ||
| 51 | OpenFrame::AppKit::Segment::SessionSaver->new() | ||
| 52 | ->session( $session ) | ||
| 53 | ); | ||
| 54 | } | ||
| 55 | |||
| 56 | package OpenFrame::AppKit::Segment::SessionSaver; | ||
| 57 | |||
| 58 | use strict; | ||
| 59 | use warnings::register; | ||
| 60 | |||
| 61 | use Pipeline::Segment; | ||
| 62 | use base qw ( Pipeline::Segment ); | ||
| 63 | |||
| 64 | sub dispatch { | ||
| 65 | my $self = shift; | ||
| 66 | $self->session->store(); | ||
| 67 | } | ||
| 68 | |||
| 69 | sub session { | ||
| 70 | my $self = shift; | ||
| 71 | my $sess = shift; | ||
| 72 | if (defined($sess)) { | ||
| 73 | $self->{session} = $sess; | ||
| 74 | return $self; | ||
| 75 | } else { | ||
| 76 | return $self->{session}; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | 1; | ||
| 81 | |||
| 82 | |||
| 83 | =head1 NAME | ||
| 84 | |||
| 85 | OpenFrame::AppKit::Segment::SessionLoader - a pipeline segment to manage sessions | ||
| 86 | |||
| 87 | =head1 SYNOPSIS | ||
| 88 | |||
| 89 | use OpenFrame::AppKit; | ||
| 90 | my $session_loader = OpenFrame::Segment::SessionLoader->new(); | ||
| 91 | |||
| 92 | =head1 DESCRIPTION | ||
| 93 | |||
| 94 | The C<OpenFrame::AppKit::Segment::SessionLoader> class is a pipeline segment and inherits its | ||
| 95 | interface from there. It manages OpenFrame::AppKit::Session objects. | ||
| 96 | |||
| 97 | =head1 AUTHOR | ||
| 98 | |||
| 99 | James A. Duncan <jduncan@fotango.com> | ||
| 100 | |||
| 101 | =head1 COPYRIGHT | ||
| 102 | |||
| 103 | Copyright 2002 Fotango Ltd. All Rights Reserved | ||
| 104 | |||
| 105 | This program is released under the same license as Perl itself. |