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 16 18 88.8
pod 2 5 40.0
total 66 73 90.4


line stmt bran cond sub pod time code
1             package Starch::Store::Catalyst::Plugin::Session;
2              
3             $Starch::Store::Catalyst::Plugin::Session::VERSION = '0.04';
4              
5             =head1 NAME
6              
7             Starch::Store::Catalyst::Plugin::Session - Starch storage backend using
8             Catalyst::Plugin::Session stores.
9              
10             =head1 SYNOPSIS
11              
12             my $starch = Starch->new(
13             store => {
14             class => '::Catalyst::Plugin::Session',
15             store_class => '::File',
16             session_config => {
17             storage => '/tmp/session',
18             },
19             },
20             );
21              
22             =head1 DESCRIPTION
23              
24             This L<Starch> store uses L<Catalyst::Plugin::Session> stores
25             to set and get state data.
26              
27             The reason this module exists is to make the migration from
28             the Catalyst session plugin to Starch as painless as possible.
29              
30             =cut
31              
32 1     1   13079 use Catalyst::Plugin::Session::Store;
  1         84  
  1         30  
33 1     1   547 use Moose::Meta::Class qw();
  1         141139  
  1         42  
34 1     1   9 use Types::Standard -types;
  1         3  
  1         21  
35 1     1   4727 use Types::Common::String -types;
  1         3  
  1         10  
36 1     1   1517 use Starch::Util qw( load_prefixed_module );
  1         3  
  1         54  
37              
38 1     1   7 use Moo;
  1         2  
  1         8  
39 1     1   442 use strictures 2;
  1         10  
  1         46  
40 1     1   211 use namespace::clean;
  1         2  
  1         10  
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 CPAN INDEX
57             Starch::FakeCatalystContext;
58              
59 1     1   1371 use Moose;
  1         346270  
  1         7  
60             extends 'Catalyst::Component';
61 1     1   7882 use Class::C3::Adopt::NEXT;
  1         4228  
  1         7  
62 1     1   40 use Log::Any qw($log);
  1         3  
  1         13  
63              
64             has config => ( is=>'ro' );
65              
66             sub _session_plugin_config {
67 12     12   3432 return $_[0]->config->{session};
68             }
69              
70             sub setup_session {
71 5     5 0 37 $_[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   61 my ($self) = @_;
129              
130 5         27 my $store_class = load_prefixed_module(
131             'Catalyst::Plugin::Session::Store',
132             $self->store_class(),
133             );
134              
135 5         21188 my $class = Moose::Meta::Class->create_anon_class(
136             superclasses => [
137             'Starch::FakeCatalystContext',
138             $store_class,
139             ],
140             );
141              
142 5         17966 my $store = $class->new_object(
143             config => {
144             session => $self->session_config(),
145             'Plugin::Session' => $self->session_config(),
146             },
147             );
148              
149 5         6920 $store->setup_session();
150              
151 5         264 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             local $Carp::Internal{ (__PACKAGE__) } = 1;
174              
175             $self->store->store_session_data( "session:$id", $data );
176              
177             return;
178             }
179              
180             sub get {
181 19     19 1 14782 my ($self, $id, $namespace) = @_;
182              
183 19         64 local $Carp::Internal{ (__PACKAGE__) } = 1;
184              
185 19         389 return $self->store->get_session_data( "session:$id" );
186             }
187              
188             sub remove {
189 6     6 1 5676 my ($self, $id, $namespace) = @_;
190              
191 6         22 local $Carp::Internal{ (__PACKAGE__) } = 1;
192              
193 6         117 $self->store->delete_session_data( "session:$id" );
194              
195 6         2052 return;
196             }
197              
198             1;
199             __END__
200              
201             =head1 SUPPORT
202              
203             Please submit bugs and feature requests to the
204             Starch-Store-Catalyst-Plugin-Session GitHub issue tracker:
205              
206             L<https://github.com/bluefeet/Starch-Store-Calatlyst-Plugin-Session/issues>
207              
208             =head1 AUTHOR
209              
210             Aran Clary Deltac <bluefeetE<64>gmail.com>
211              
212             =head1 ACKNOWLEDGEMENTS
213              
214             Thanks to L<ZipRecruiter|https://www.ziprecruiter.com/>
215             for encouraging their employees to contribute back to the open
216             source ecosystem. Without their dedication to quality software
217             development this distribution would not exist.
218              
219             =head1 LICENSE
220              
221             This library is free software; you can redistribute it and/or modify
222             it under the same terms as Perl itself.
223              
224             =cut
225