blib/lib/Template/Provider/CustomDBIC.pm

filter: Statementstmt 22.0% 17 / 77 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 41 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 55.5% 5 / 9 Podpod 100% 1 / 1 totaltotal 12.4% 23 / 185 CC 34 SCAR 52.6
CC
34
Cov
11%
fetch
380.0
_init
72.0
_load
30.0
CRAP 192.9
↑ Summary
1 package Template::Provider::CustomDBIC;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use base qw/ Template::Provider /;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Date::Parse ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = '0.05';
12  
13 =head1 NAME
14  
15 Template::Provider::CustomDBIC - Load templates using DBIx::Class
16  
17  
18 =head1 SYNOPSIS
19  
20 use My::CustomDBIC::Schema;
21 use Template;
22 use Template::Provider::CustomDBIC;
23  
24 my $schema = My::CustomDBIC::Schema->connect(
25 $dsn, $user, $password, \%options
26 );
27 my $resultset = $schema->resultset('Template');
28  
29 If all of your templates are stored in a single table the most convenient
30 method is to pass the provider a L<DBIx::Class::ResultSet>.
31  
32 my $template = Template->new({
33 LOAD_TEMPLATES => [
34 Template::Provider::CustomDBIC->new({
35 RESULTSET => $resultset,
36 # Other template options like COMPILE_EXT...
37 }),
38 ],
39 });
40  
41 # Process the template in 'column' referred by reference from resultset 'Template'.
42 $template->process('table/reference/column');
43  
44  
45 =head1 DESCRIPTION
46  
47 Template::Provider::CustomDBIC allows a L<Template> object to fetch its data using
48 L<DBIx::Class> instead of, or in addition to, the default filesystem-based
49 L<Template::Provider>.
50  
51  
52 =head2 SCHEMA
53  
54 This provider requires a schema containing at least the following:
55  
56 =over
57  
58 =item
59  
60 A column containing the template name. When C<$template-E<gt>provider($name)>
61 is called the provider will search this column for the corresponding C<$name>.
62 For this reason the column must be a unique key, else an exception will be
63 raised.
64  
65 =item
66  
67 A column containing the actual template content itself. This is what will be
68 compiled and returned when the template is processed.
69  
70 =item
71  
72 A column containing the time the template was last modified. This must return
73 - or be inflated to - a date string recognisable by L<Date::Parse>.
74  
75 =back
76  
77  
78 =head2 OPTIONS
79  
80 In addition to supplying a RESULTSET or SCHEMA and the standard
81 L<Template::Provider> options, you may set the following preferences:
82  
83 =over 4
84  
85 =item COLUMN_NAME
86  
87 The table column that contains the template name. This will default to 'name'.
88  
89 =item COLUMN_CONTENT
90  
91 The table column that contains the template data itself. This will default to
92 'content'.
93  
94 =item COLUMN_MODIFIED
95  
96 The table column that contains the date that the template was last modified.
97 This will default to 'modified'.
98  
99 =back
100  
101  
102 =head1 METHODS
103  
104 =begin comment
105  
106 ->_init( \%options )
107  
108 Check that valid Template::Provider::CustomDBIC-specific arguments have been
109 supplied and store the appropriate values. See above for the available
110 options.
111  
112 =end comment
113  
114 =cut
115  
116 0 ▶sub _init {
Subroutine _init: not called CC=8 SCAR=42.8
117 0 my ( $self, $options ) = @_;
118  
119 # Provide defaults as necessary.
120 0 ▶ $self->{COLUMN_NAME} = $options->{COLUMN_NAME} || 'name';
Condition0%0/2
$options->{'COLUMN_NAME'} || 'name'
l!l
00
MC/DC0%0/1
$options->{'COLUMN_NAME'} || 'name'
$options->{'COLUMN_NAME'}
Truth table0%0/2
A
A= $options->{'COLUMN_NAME'}
Aresult
11
00
121 0 ▶ $self->{COLUMN_MODIFIED} = $options->{COLUMN_MODIFIED} || 'modified';
Condition0%0/2
$options->{'COLUMN_MODIFIED'} || 'modified'
l!l
00
MC/DC0%0/1
$options->{'COLUMN_MODIFIED'} || 'modified'
$options->{'COLUMN_MODIFIED'}
Truth table0%0/2
A
A= $options->{'COLUMN_MODIFIED'}
Aresult
11
00
122 0 ▶ $self->{COLUMN_CONTENT} = $options->{COLUMN_CONTENT} || 'content';
Condition0%0/2
$options->{'COLUMN_CONTENT'} || 'content'
l!l
00
MC/DC0%0/1
$options->{'COLUMN_CONTENT'} || 'content'
$options->{'COLUMN_CONTENT'}
Truth table0%0/2
A
A= $options->{'COLUMN_CONTENT'}
Aresult
11
00
123  
124 # Ensure that a RESULTSET or SCHEMA has been specified. In the case of
125 # both RESULTSET takes precedence.
126 0 my $storage;
127  
128 0 ▶ if ( defined $options->{RESULTSET} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{'RESULTSET'}) { } 0 0 0
129  
130 0 $self->{RESULTSET} = $options->{RESULTSET};
131 0 $storage = $self->{RESULTSET}->result_source->schema->storage;
132  
133 } else { # neither specified
134  
135 0 return $self->error('A valid DBIx::Class::ResultSet is required');
136 }
137  
138 # The connection DSN will be used when caching templates.
139 0 $self->{DSN} = $storage->connect_info->[0];
140  
141 # Use Template::Provider's ->_init() to create the COMPILE_DIR...
142 0 $self->SUPER::_init($options);
143  
144 # ...and add a directory for templates cached by this provider.
145 0 ▶ if ( $self->{COMPILE_DIR} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"COMPILE_DIR"}) 0 0 0
146  
147 # Adapted from Template::Provider 2.91
148 0 require File::Spec;
149 0 require File::Path;
150  
151 0 my $wdir = $self->{DSN};
152 0 ▶ $wdir =~ s/://g if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 0 0
153 0 $wdir =~ /(.*)/; # untaint
154 0 $wdir = File::Spec->catfile( $self->{COMPILE_DIR}, $1 );
155 0 ▶ File::Path::mkpath($wdir) unless -d $wdir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $wdir 0 0 0
156 }
157  
158 0 return $self;
159 }
160  
161 =head2 ->fetch( $name )
162  
163 This method is called automatically during L<Template>'s C<-E<gt>process()>
164 and returns a compiled template for the given C<$name>, using the cache where
165 possible.
166  
167 =cut
168  
169 0 ▶sub fetch {
Subroutine fetch: not called CC=19 SCAR=59.4
Pod documented
170 0 my ( $self, $name ) = @_;
171  
172 # We're not interested in GLOBs or file handles.
173 0 ▶ if ( ref $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $name) 0 0 0
174 0 return ( undef, Template::Constants::STATUS_DECLINED );
175 }
176  
177 # Determine the name of the table we're dealing with.
178 0 my ( $table, $reference, $column ) = split( "/", $name );
179
180 0 my ( $data, $error, $slot );
181  
182 0 ▶ if ( $table && $reference && $column && scalar split("/", $name) == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table and $reference and $column and scalar split(m[/], $name, 0) == 3) { } 0 0 0
Condition0%0/9
$table and $reference and $column and scalar split(m[/], $name, 0) == 3
!ll&&!rl&&r
000
$table and $reference and $column
!ll&&!rl&&r
000
$table and $reference
!ll&&!rl&&r
000
MC/DC0%0/4
$table and $reference and $column and scalar split(m[/], $name, 0) == 3
$table $reference $column scalar split(m[/], $name, 0) == 3
Truth table0%0/5
A and B and C and D
A= $tableB= $referenceC= $columnD= scalar split(m[/], $name, 0) == 3
ABCDresult
0XXX0
10XX0
110X0
11100
11111
183
184  
185 # Determine the path this template would be cached to.
186 0 my $compiled_filename = $self->_compiled_filename( $self->{DSN} . "/$table/$reference/$column" );
187  
188 # Is caching enabled?
189 0 my $size = $self->{SIZE};
190 0 ▶ my $caching = !defined $size || $size;
Condition0%0/3
!defined($size) || $size
l!l&&r!l&&!r
000
MC/DC0%0/2
!defined($size) || $size
!defined($size) $size
Truth table0%0/3
not(A) || B
A= !defined($size)B= $size
ABresult
1X1
011
000
191  
192 # If caching is enabled and an entry already exists, refresh its cache
193 # slot and extract the data...
194 0 ▶ if ( $caching && ( $slot = $self->{LOOKUP}->{"$table/$reference/$column"} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($caching and $slot = $self->{'LOOKUP'}{"$table/$reference/$column"}) { } 0 0 0
elsif ($compiled_filename and -f $compiled_filename and not $self->_modified("$table/$reference/$column", (stat _)[9])) { } 0 0 0
Condition0%0/3
$caching and $slot = $self->{"LOOKUP"}{"$table/$reference/$column"}
!ll&&!rl&&r
000
MC/DC0%0/2
$caching and $slot = $self->{"LOOKUP"}{"$table/$reference/$column"}
$caching $slot = $self->{"LOOKUP"}{"$table/$reference/$column"}
Truth table0%0/3
A and B
A= $cachingB= $slot = $self->{"LOOKUP"}{"$table/$reference/$column"}
ABresult
0X0
100
111
195 0 ( $data, $error ) = $self->_refresh($slot);
196 0 ▶ $data = $slot->[Template::Provider::DATA] unless $error;
Branch: 1 branch
BranchTrueFalseTotal
unless $error 0 0 0
197 }
198  
199 # ...otherwise if this template has already been compiled and cached (but
200 # not by this object) try to load it from the disk, providing it hasn't
201 # been modified...
202 ▶ elsif ( $compiled_filename
Condition0%0/6
$compiled_filename and -f $compiled_filename and not $self->_modified("$table/$reference/$column", (stat _)[9])
!ll&&!rl&&r
000
$compiled_filename and -f $compiled_filename
!ll&&!rl&&r
000
MC/DC0%0/3
$compiled_filename and -f $compiled_filename and not $self->_modified("$table/$reference/$column", (stat _)[9])
$compiled_filename -f $compiled_filename not $self->_modified("$table/$reference/$column", (stat _)[9])
Truth table0%0/4
A and B and not(C)
A= $compiled_filenameB= -f $compiled_filenameC= not $self->_modified("$table/$reference/$column", (stat _)[9])
ABCresult
0XX0
10X0
1100
1111
203 && -f $compiled_filename
204 && !$self->_modified( "$table/$reference/$column", ( stat(_) )[9] ) )
205 {
206 0 $data = $self->_load_compiled($compiled_filename);
207 0 ▶ $error = $self->error() unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
208  
209 # Save the new data where caching is enabled.
210 0 ▶ $self->store( "$table/$reference/$column", $data ) if $caching && !$error;
Branch: 1 branch
BranchTrueFalseTotal
if $caching and not $error 0 0 0
Condition0%0/3
$caching and not $error
!ll&&!rl&&r
000
MC/DC0%0/2
$caching and not $error
$caching not $error
Truth table0%0/3
A and not(B)
A= $cachingB= not $error
ABresult
0X0
100
111
211 }
212  
213 # ...else there is nothing already cached for this template so load it
214 # from the database.
215 else {
216  
217 0 ( $data, $error ) = $self->_load("$table/$reference/$column");
218  
219 0 ▶ if ( !$error ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($error) 0 0 0
220
221 0 ( $data, $error ) = $self->_compile( $data, $compiled_filename );
222 }
223  
224 # Save the new data where caching is enabled.
225 0 ▶ if ( !$error ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($error) 0 0 0
226  
227 0 ▶ $data = $caching ? $self->_store( "$table/$reference/$column", $data ) : $data->{data};
Branch: 1 branch
BranchTrueFalseTotal
$caching ? : 0 0 0
228 }
229 }
230
231 0 return ( $data, $error );
232
233 } else {
234  
235 0 return ( undef, Template::Constants::STATUS_DECLINED );
236
237 }
238 }
239  
240 =begin comment
241  
242 ->_load( $name )
243  
244 Load the template from the database and return a hash containing its name,
245 content, the time it was last modified, and the time it was loaded (now).
246  
247 =end comment
248  
249 =cut
250  
251 0 ▶sub _load {
Subroutine _load: not called CC=5 SCAR=34.0
252 0 my ( $self, $name ) = @_;
253 0 my ( $data, $error );
254  
255 0 my ( $table, $reference, $column ) = split( "/", $name );
256  
257 0 ▶ if ( $table && $reference && $column ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table and $reference and $column) { } 0 0 0
Condition0%0/6
$table and $reference and $column
!ll&&!rl&&r
000
$table and $reference
!ll&&!rl&&r
000
MC/DC0%0/3
$table and $reference and $column
$table $reference $column
Truth table0%0/4
A and B and C
A= $tableB= $referenceC= $column
ABCresult
0XX0
10X0
1100
1111
258  
259 0 my $resultset = $self->{RESULTSET};
260  
261 # Try to retrieve the template from the database.
262 0 my $template = $resultset->find( $reference, { key => $self->{COLUMN_NAME} } );
263  
264 0 ▶ if ($template) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template) { } 0 0 0
265  
266 $data = {
267 name => "$table/$reference/$column",
268 text => $template->get_column($column),
269 0 time => Date::Parse::str2time( $template->get_column( $self->{COLUMN_MODIFIED} ) ),
270 load => time,
271 };
272  
273 } else {
274  
275 # Not found in RESULTSET
276 0 ( $data, $error ) = ( "Could not retrieve '$reference' from the result set '$table'", Template::Constants::STATUS_ERROR );
277 }
278  
279 } else {
280  
281 0 ( $data, $error ) = ( undef, Template::Constants::STATUS_DECLINED );
282  
283 }
284  
285 0 return ( $data, $error );
286 }
287  
288 =begin comment
289  
290 ->_modified( $name, $time )
291  
292 When called with a single argument, returns the modification time of the
293 given template. When called with a second argument it returns true if $name
294 has been modified since $time.
295  
296 =end comment
297  
298 =cut
299  
300 0 ▶sub _modified {
Subroutine _modified: not called CC=5 SCAR=34.0
301 0 my ( $self, $name, $time ) = @_;
302  
303 0 my ( $table, $reference, $column ) = split( "/", $name );
304  
305 0 my $resultset = $self->{RESULTSET};
306  
307 # Try to retrieve the template from the database...
308 0 my $template = $resultset->find( $reference, { key => $self->{COLUMN_NAME} } );
309  
310 0 require Date::Parse;
311  
312 0 ▶ my $modified = $template && Date::Parse::str2time( $template->{COLUMN_MODIFIED} ) || return $time ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$time ? : 0 0 0
Condition0%0/5
$template && Date::Parse::str2time($template->{'COLUMN_MODIFIED'}) || (return $time ? 1 : 0)
l!l
00
$template and Date::Parse::str2time($template->{"COLUMN_MODIFIED"})
!ll&&!rl&&r
000
MC/DC0%0/2
$template && Date::Parse::str2time($template->{'COLUMN_MODIFIED'}) || (return $time ? 1 : 0)
$template Date::Parse::str2time($template->{"COLUMN_MODIFIED"})
Truth table0%0/3
A and B
A= $templateB= Date::Parse::str2time($template->{"COLUMN_MODIFIED"})
ABresult
111
0X0
100
313  
314 0 ▶ return $time ? $modified > $time : $modified;
Branch: 1 branch
BranchTrueFalseTotal
$time ? : 0 0 0
315 }
316  
317 2 1; # End of the module code; everything from here is documentation...
318 __END__
↑ Summary