Perltidy + pod tests for all session plugins
[catagits/Catalyst-Plugin-Session-State-Cookie.git] / lib / Catalyst / Plugin / Session / State / Cookie.pm
CommitLineData
1a776a0c 1package Catalyst::Plugin::Session::State::Cookie;
2use base qw/Catalyst::Plugin::Session::State/;
bf2bce67 3
4use strict;
1a776a0c 5use warnings;
bf2bce67 6
1a776a0c 7use NEXT;
bf2bce67 8
81eb8ebf 9our $VERSION = "0.01";
10
5e50008f 11sub setup_session {
20e33791 12 my $c = shift;
5e50008f 13
20e33791 14 $c->NEXT::setup_session(@_);
5e50008f 15
20e33791 16 $c->config->{session}{cookie_name} ||= "session";
5e50008f 17}
18
bf2bce67 19sub finalize {
b2f8df5e 20 my $c = shift;
1a776a0c 21
20e33791 22 my $cookie_name = $c->config->{session}{cookie_name};
5e50008f 23
bf2bce67 24 if ( my $sid = $c->sessionid ) {
5e50008f 25 my $cookie = $c->request->cookies->{$cookie_name};
1a776a0c 26 if ( !$cookie or $cookie->value ne $sid ) {
5e50008f 27 $c->response->cookies->{$cookie_name} = { value => $sid };
1a776a0c 28 $c->log->debug(qq/A cookie with the session id "$sid" was saved/)
29 if $c->debug;
58c05d1a 30 }
bf2bce67 31 }
1a776a0c 32
bf2bce67 33 return $c->NEXT::finalize(@_);
34}
35
1a776a0c 36sub prepare_cookies {
bf2bce67 37 my $c = shift;
1a776a0c 38
7acb108b 39 my $ret = $c->NEXT::prepare_cookies(@_);
40
20e33791 41 my $cookie_name = $c->config->{session}{cookie_name};
5e50008f 42
43 if ( my $cookie = $c->request->cookies->{$cookie_name} ) {
bf2bce67 44 my $sid = $cookie->value;
45 $c->sessionid($sid);
46 $c->log->debug(qq/Found sessionid "$sid" in cookie/) if $c->debug;
47 }
bf2bce67 48
20e33791 49 return $ret;
bf2bce67 50}
51
1a776a0c 52__PACKAGE__
57dbf608 53
1a776a0c 54__END__
bf2bce67 55
1a776a0c 56=pod
b2f8df5e 57
1a776a0c 58=head1 NAME
bf2bce67 59
1a776a0c 60Catalyst::Plugin::Session::State::Cookie - A session ID
bf2bce67 61
1a776a0c 62=head1 SYNOPSIS
bf2bce67 63
20e33791 64 use Catalyst qw/Session Session::State::Cookie Session::Store::Foo/;
bf2bce67 65
1a776a0c 66=head1 DESCRIPTION
bf2bce67 67
1a776a0c 68In order for L<Catalyst::Plugin::Session> to work the session ID needs to be
69stored on the client, and the session data needs to be stored on the server.
bf2bce67 70
1a776a0c 71This plugin stores the session ID on the client using the cookie mechanism.
57dbf608 72
1a776a0c 73=head1 EXTENDED METHODS
58c05d1a 74
57dbf608 75=over 4
76
1a776a0c 77=item prepare_cookies
57dbf608 78
1a776a0c 79Will restore if an appropriate cookie is found.
58c05d1a 80
1a776a0c 81=item finalize
58c05d1a 82
19c2baa1 83Will set a cookie called C<session> if it doesn't exist or if it's value is not
84the current session id.
85
86=item setup_session
87
88Will set the C<cookie_name> parameter to it's default value if it isn't set.
58c05d1a 89
57dbf608 90=back
58c05d1a 91
5e50008f 92=head1 CONFIGURATION
93
94=over 4
95
96=item cookie_name
97
98The name of the cookie to store (defaults to C<session>).
99
100=back
101
bf2bce67 102=head1 SEE ALSO
103
1a776a0c 104L<Catalyst>, L<Catalyst::Plugin::Session>.
bf2bce67 105
106=head1 AUTHOR
107
57dbf608 108Sebastian Riedel E<lt>C<sri@cpan.org>E<gt>,
109Marcus Ramberg E<lt>C<mramberg@cpan.org>E<gt>,
1a776a0c 110Andrew Ford E<lt>C<andrewf@cpan.org>E<gt>,
111Yuval Kogman E<lt>C<nothingmuch@woobling.org>E<gt>
bf2bce67 112
113=head1 COPYRIGHT
114
bfeb5ca0 115This program is free software, you can redistribute it and/or modify it
116under the same terms as Perl itself.
bf2bce67 117
118=cut
119
1201;