File Coverage

blib/lib/Starch/Store/Catalyst/Plugin/Session.pm
Criterion Covered Total %
statement 48 50 96.0
branch n/a
condition n/a
subroutine 17 19 89.4
pod 2 5 40.0
total 67 74 90.5


line stmt bran cond sub pod time code
1             package Starch::Store::Catalyst::Plugin::Session;
2 1     1   10597 use 5.008001;
  1         4  
3 1     1   6 use strictures 2;
  1         8  
  1         31  
4             our $VERSION = '0.05';
5              
6             =head1 NAME
7              
8             Starch::Store::Catalyst::Plugin::Session - Starch storage backend using
9             Catalyst::Plugin::Session stores.
10              
11             =head1 SYNOPSIS
12              
13             my $starch = Starch->new(
14             store => {
15             class => '::Catalyst::Plugin::Session',
16             store_class => '::File',
17             session_config => {
18             storage => '/tmp/session',
19             },
20             },
21             );
22              
23             =head1 DESCRIPTION
24              
25             This L<Starch> store uses L<Catalyst::Plugin::Session> stores
26             to set and get state data.
27              
28             The reason this module exists is to make the migration from
29             the Catalyst session plugin to Starch as painless as possible.
30              
31             =cut
32              
33 1     1   619 use Catalyst::Plugin::Session::Store;
  1         64  
  1         23  
34 1     1   493 use Moose::Meta::Class qw();
  1         117045  
  1         33  
35 1     1   23 use Types::Standard -types;
  1         3  
  1         18  
36 1     1   3836 use Types::Common::String -types;
  1         3  
  1         9  
37 1     1   1166 use Starch::Util qw( load_prefixed_module );
  1         2  
  1         49  
38              
39 1     1   5 use Moo;
  1         2  
  1         9  
40 1     1   360 use namespace::clean;
  1         2  
  1         7  
41              
42             with qw(
43             Starch::Store
44             );
45              
46             after BUILD => sub{
47             my ($self) = @_;
48              
49             # Get this loaded as early as possible.
50             $self->store();
51              
52             return;
53             };
54              
55             {
56             package # NO INDEX
57             Starch::FakeCatalystContext;
58              
59 1     1   1150 use Moose;
  1         285476  
  1         5  
60             extends 'Catalyst::Component';
61 1     1   6466 use Class::C3::Adopt::NEXT;
  1         3459  
  1         6  
62 1     1   34 use Log::Any qw($log);
  1         2  
  1         11  
63              
64             has config => ( is=>'ro' );
65              
66             sub _session_plugin_config {
67 12     12   2662 return $_[0]->config->{session};
68             }
69              
70             sub setup_session {
71 5     5 0 38 $_[0]->maybe::next::method();
72             }
73              
74 0     0 0 0 sub debug { 0 }
75              
76 0     0 0 0 sub log { $log }
77             }
78              
79             =head1 REQUIRED ARGUMENTS
80              
81             =head2 store_class
82              
83             The full class name for the L<Catalyst::Plugin::Session::Store> you
84             wish to use.
85              
86             If the store class starts with C<::> then it will be considered
87             relative to C<Catalyst::Plugin::Session::Store>. For example, if
88             you set this to C<::File> then it will be internally translated to
89             C<Catalyst::Plugin::Session::Store::File>.
90              
91             =cut
92              
93             has store_class => (
94             is => 'ro',
95             isa => NonEmptySimpleStr,
96             required => 1,
97             );
98              
99             =head1 OPTIONAL ARGUMENTS
100              
101             =head2 session_config
102              
103             The configuration of the session plugin.
104              
105             =cut
106              
107             has session_config => (
108             is => 'ro',
109             isa => HashRef,
110             default => sub{ {} },
111             );
112              
113             =head1 ATTRIBUTES
114              
115             =head2 store
116              
117             This is the L<Catalyst::Plugin::Session::Store> object built from the
118             L</store_class> and with a fake Catalyst superclass to make everything
119             work.
120              
121             =cut
122              
123             has store => (
124             is => 'lazy',
125             init_arg => undef,
126             );
127             sub _build_store {
128 5     5   42 my ($self) = @_;
129              
130 5         24 my $store_class = load_prefixed_module(
131             'Catalyst::Plugin::Session::Store',
132             $self->store_class(),
133             );
134              
135 5         16842 my $class = Moose::Meta::Class->create_anon_class(
136             superclasses => [
137             'Starch::FakeCatalystContext',
138             $store_class,
139             ],
140             );
141              
142 5         14814 my $store = $class->new_object(
143             config => {
144             session => $self->session_config(),
145             'Plugin::Session' => $self->session_config(),
146             },
147             );
148              
149 5         5791 $store->setup_session();
150              
151 5         208 return $store;
152             }
153              
154             =head1 METHODS
155              
156             =head2 set
157              
158             See L<Starch::Store/set>. Calls C<store_session_data> on L</store>.
159              
160             =head2 get
161              
162             See L<Starch::Store/get>. Calls C<get_session_data> on L</store>.
163              
164             =head2 remove
165              
166             See L<Starch::Store/remove>. Calls C<delete_session_data> on L</store>.
167              
168             =cut
169              
170             sub set {
171             my ($self, $id, $namespace, $data, $expires) = @_;
172              
173             $self->store->store_session_data( "session:$id", $data );
174              
175             return;
176             }
177              
178             sub get {
179 19     19 1 12205 my ($self, $id, $namespace) = @_;
180              
181 19         275 return $self->store->get_session_data( "session:$id" );
182             }
183              
184             sub remove {
185 6     6 1 4619 my ($self, $id, $namespace) = @_;
186              
187 6         103 $self->store->delete_session_data( "session:$id" );
188              
189 6         1728 return;
190             }
191              
192             1;
193             __END__
194              
195             =head1 SUPPORT
196              
197             Please submit bugs and feature requests to the
198             Starch-Store-Catalyst-Plugin-Session GitHub issue tracker:
199              
200             L<https://github.com/bluefeet/Starch-Store-Calatlyst-Plugin-Session/issues>
201              
202             =head1 AUTHORS
203              
204             Aran Clary Deltac <bluefeet@gmail.com>
205              
206             =head1 ACKNOWLEDGEMENTS
207              
208             Thanks to L<ZipRecruiter|https://www.ziprecruiter.com/>
209             for encouraging their employees to contribute back to the open
210             source ecosystem. Without their dedication to quality software
211             development this distribution would not exist.
212              
213             =head1 LICENSE
214              
215             This library is free software; you can redistribute it and/or modify
216             it under the same terms as Perl itself.
217              
218             =cut
219