line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Template::Provider::CustomDBIC; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
11012
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
59
|
|
4
|
2
|
|
|
2
|
|
12
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
64
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
10
|
use base qw/ Template::Provider /; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1336
|
|
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
29859
|
use Carp qw( croak ); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
106
|
|
9
|
2
|
|
|
2
|
|
998
|
use Date::Parse (); |
|
2
|
|
|
|
|
15590
|
|
|
2
|
|
|
|
|
2004
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
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. |
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 object to fetch its data using |
48
|
|
|
|
|
|
|
L instead of, or in addition to, the default filesystem-based |
49
|
|
|
|
|
|
|
L. |
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-Eprovider($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. |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=back |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head2 OPTIONS |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
In addition to supplying a RESULTSET or SCHEMA and the standard |
81
|
|
|
|
|
|
|
L 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
|
|
|
|
|
|
|
sub _init { |
117
|
0
|
|
|
0
|
|
|
my ( $self, $options ) = @_; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# Provide defaults as necessary. |
120
|
0
|
|
0
|
|
|
|
$self->{COLUMN_NAME} = $options->{COLUMN_NAME} || 'name'; |
121
|
0
|
|
0
|
|
|
|
$self->{COLUMN_MODIFIED} = $options->{COLUMN_MODIFIED} || 'modified'; |
122
|
0
|
|
0
|
|
|
|
$self->{COLUMN_CONTENT} = $options->{COLUMN_CONTENT} || 'content'; |
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
|
0
|
|
|
|
|
if ( defined $options->{RESULTSET} ) { |
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
|
0
|
|
|
|
|
if ( $self->{COMPILE_DIR} ) { |
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
|
0
|
|
|
|
|
$wdir =~ s/://g if $^O eq 'MSWin32'; |
153
|
0
|
|
|
|
|
|
$wdir =~ /(.*)/; # untaint |
154
|
0
|
|
|
|
|
|
$wdir = File::Spec->catfile( $self->{COMPILE_DIR}, $1 ); |
155
|
0
|
0
|
|
|
|
|
File::Path::mkpath($wdir) unless -d $wdir; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
0
|
|
|
|
|
|
return $self; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head2 ->fetch( $name ) |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
This method is called automatically during L's C<-Eprocess()> |
164
|
|
|
|
|
|
|
and returns a compiled template for the given C<$name>, using the cache where |
165
|
|
|
|
|
|
|
possible. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=cut |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
sub fetch { |
170
|
0
|
|
|
0
|
1
|
|
my ( $self, $name ) = @_; |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
# We're not interested in GLOBs or file handles. |
173
|
0
|
0
|
|
|
|
|
if ( ref $name ) { |
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
|
0
|
0
|
|
|
|
if ( $table && $reference && $column && scalar split("/", $name) == 3 ) { |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
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
|
|
0
|
|
|
|
my $caching = !defined $size || $size; |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
# If caching is enabled and an entry already exists, refresh its cache |
193
|
|
|
|
|
|
|
# slot and extract the data... |
194
|
0
|
0
|
0
|
|
|
|
if ( $caching && ( $slot = $self->{LOOKUP}->{"$table/$reference/$column"} ) ) { |
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
195
|
0
|
|
|
|
|
|
( $data, $error ) = $self->_refresh($slot); |
196
|
0
|
0
|
|
|
|
|
$data = $slot->[Template::Provider::DATA] unless $error; |
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 |
203
|
|
|
|
|
|
|
&& -f $compiled_filename |
204
|
|
|
|
|
|
|
&& !$self->_modified( "$table/$reference/$column", ( stat(_) )[9] ) ) |
205
|
|
|
|
|
|
|
{ |
206
|
0
|
|
|
|
|
|
$data = $self->_load_compiled($compiled_filename); |
207
|
0
|
0
|
|
|
|
|
$error = $self->error() unless $data; |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
# Save the new data where caching is enabled. |
210
|
0
|
0
|
0
|
|
|
|
$self->store( "$table/$reference/$column", $data ) if $caching && !$error; |
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
|
0
|
|
|
|
|
if ( !$error ) { |
220
|
|
|
|
|
|
|
|
221
|
0
|
|
|
|
|
|
( $data, $error ) = $self->_compile( $data, $compiled_filename ); |
222
|
|
|
|
|
|
|
} |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
# Save the new data where caching is enabled. |
225
|
0
|
0
|
|
|
|
|
if ( !$error ) { |
226
|
|
|
|
|
|
|
|
227
|
0
|
0
|
|
|
|
|
$data = $caching ? $self->_store( "$table/$reference/$column", $data ) : $data->{data}; |
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
|
|
|
|
|
|
|
sub _load { |
252
|
0
|
|
|
0
|
|
|
my ( $self, $name ) = @_; |
253
|
0
|
|
|
|
|
|
my ( $data, $error ); |
254
|
|
|
|
|
|
|
|
255
|
0
|
|
|
|
|
|
my ( $table, $reference, $column ) = split( "/", $name ); |
256
|
|
|
|
|
|
|
|
257
|
0
|
0
|
0
|
|
|
|
if ( $table && $reference && $column ) { |
|
|
|
0
|
|
|
|
|
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
|
0
|
|
|
|
|
if ($template) { |
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
|
|
|
|
|
|
|
sub _modified { |
301
|
0
|
|
|
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
|
|
0
|
|
|
|
my $modified = $template && Date::Parse::str2time( $template->{COLUMN_MODIFIED} ) || return $time ? 1 : 0; |
313
|
|
|
|
|
|
|
|
314
|
0
|
0
|
|
|
|
|
return $time ? $modified > $time : $modified; |
315
|
|
|
|
|
|
|
} |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
1; # End of the module code; everything from here is documentation... |
318
|
|
|
|
|
|
|
__END__ |