summaryrefslogtreecommitdiff
path: root/lib/Dancer2/Core/Session.pm
blob: d58e11826ffeeff286bf41aed06809cdebd62963 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package Dancer2::Core::Session;
# ABSTRACT: class to represent any session object
$Dancer2::Core::Session::VERSION = '1.1.0';
use Moo;
use Dancer2::Core::Types;
use Dancer2::Core::Time;

has id => (
    # for some specific plugins this should be rw.
    # refer to https://github.com/PerlDancer/Dancer2/issues/460
    is       => 'rw',
    isa      => Str,
    required => 1,
);

has data => (
    is      => 'ro',
    lazy    => 1,
    default => sub { {} },
);

has expires => (
    is     => 'rw',
    isa    => Str,
    coerce => sub {
        my $value = shift;
        $value += time if $value =~ /^[\-\+]?\d+$/;
        Dancer2::Core::Time->new( expression => $value )->epoch;
    },
);

has is_dirty => (
    is      => 'rw',
    isa     => Bool,
    default => sub {0},
);


sub read {
    my ( $self, $key ) = @_;
    return $self->data->{$key};
}


sub write {
    my ( $self, $key, $value ) = @_;
    $self->is_dirty(1);
    $self->data->{$key} = $value;
}

sub delete {
    my ( $self, $key, $value ) = @_;
    $self->is_dirty(1);
    delete $self->data->{$key};
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Dancer2::Core::Session - class to represent any session object

=head1 VERSION

version 1.1.0

=head1 DESCRIPTION

A session object encapsulates anything related to a specific session: its ID,
its data, and its expiration.

It is completely agnostic of how it will be stored, this is the role of
a factory that consumes L<Dancer2::Core::Role::SessionFactory> to know about that.

Generally, session objects should not be created directly.  The correct way to
get a new session object is to call the C<create()> method on a session engine
that implements the SessionFactory role.  This is done automatically by the
app object if a session engine is defined.

=head1 ATTRIBUTES

=head2 id

The identifier of the session object. Required. By default,
L<Dancer2::Core::Role::SessionFactory> sets this to a randomly-generated,
guaranteed-unique string.

This attribute can be modified if your Session implementation requires this.

=head2 data

Contains the data of the session (Hash).

=head2 expires

Number of seconds for the expiry of the session cookie. Don't add the current
timestamp to it, will be done automatically.

Default is no expiry (session cookie will leave for the whole browser's
session).

For a lifetime of one hour:

  expires => 3600

=head2 is_dirty

Boolean value for whether data in the session has been modified.

=head1 METHODS

=head2 read

Reader on the session data

    my $value = $session->read('something');

Returns C<undef> if the key does not exist in the session.

=head2 write

Writer on the session data

  $session->write('something', $value);

Sets C<is_dirty> to true. Returns C<$value>.

=head2 delete

Deletes a key from session data

  $session->delete('something');

Sets C<is_dirty> to true. Returns the value deleted from the session.

=head1 AUTHOR

Dancer Core Developers

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2023 by Alexis Sukrieh.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut