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