| 1 | package Template::Provider::PerContextDBIC; | ||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Load templates using DBIx::Class with per-context resultsets | ||||||||||||||||||||||||||||||||||
| 3 | 9 | $Template::Provider::PerContextDBIC::VERSION = '0.000001'; | |||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use parent 'Template::Provider'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||
| 10 | 9 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | 9 | ▶ | use Date::Parse (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | # use Data::Printer; | ||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||
| 15 | sub _init { | ||||||||||||||||||||||||||||||||||
| 16 | 9 | ▶ | my ( $self, $options ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_init: called CC=12 SCAR=25.0
| |||||||||||||||||||||||||||||||||||
| 17 | # $self->debug("_init for PerContextDBIC") if $self->{ DEBUG }; | ||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||
| 19 | # Provide defaults as necessary. | ||||||||||||||||||||||||||||||||||
| 20 | 9 | ▶ | $self->{COLUMN_NAME} = $options->{COLUMN_NAME} || 'tmpl_name'; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options->{'COLUMN_NAME'} || 'tmpl_name'
MC/DC100%1/1
$options->{'COLUMN_NAME'} || 'tmpl_name'
$options->{'COLUMN_NAME'}
Truth table100%2/2
A
A= $options->{'COLUMN_NAME'}
| |||||||||||||||||||||||||||||||||||
| 21 | 9 | ▶ | $self->{COLUMN_MODIFIED} = $options->{COLUMN_MODIFIED} || 'modified'; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options->{'COLUMN_MODIFIED'} || 'modified'
MC/DC100%1/1
$options->{'COLUMN_MODIFIED'} || 'modified'
$options->{'COLUMN_MODIFIED'}
Truth table100%2/2
A
A= $options->{'COLUMN_MODIFIED'}
| |||||||||||||||||||||||||||||||||||
| 22 | 9 | ▶ | $self->{COLUMN_CONTENT} = $options->{COLUMN_CONTENT} || 'content'; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options->{'COLUMN_CONTENT'} || 'content'
MC/DC100%1/1
$options->{'COLUMN_CONTENT'} || 'content'
$options->{'COLUMN_CONTENT'}
Truth table100%2/2
A
A= $options->{'COLUMN_CONTENT'}
| |||||||||||||||||||||||||||||||||||
| 23 | 9 | ▶ | $self->{SEARCH_CRITERIA} = $options->{SEARCH_CRITERIA} || {}; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$options->{'SEARCH_CRITERIA'} || {}
MC/DC0%0/1
$options->{'SEARCH_CRITERIA'} || {}
$options->{'SEARCH_CRITERIA'}
Truth table50%1/2
A
A= $options->{'SEARCH_CRITERIA'}
| |||||||||||||||||||||||||||||||||||
| 24 | 9 | ▶ | $self->{TEMPLATE_EXTENSION} = $options->{TEMPLATE_EXTENSION} || ''; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$options->{'TEMPLATE_EXTENSION'} || ''
MC/DC0%0/1
$options->{'TEMPLATE_EXTENSION'} || ''
$options->{'TEMPLATE_EXTENSION'}
Truth table50%1/2
A
A= $options->{'TEMPLATE_EXTENSION'}
| |||||||||||||||||||||||||||||||||||
| 25 | 9 | ▶ | $self->{RESTRICTBY_NAME} = $options->{RESTRICTBY_NAME} || ''; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options->{'RESTRICTBY_NAME'} || ''
MC/DC100%1/1
$options->{'RESTRICTBY_NAME'} || ''
$options->{'RESTRICTBY_NAME'}
Truth table100%2/2
A
A= $options->{'RESTRICTBY_NAME'}
| |||||||||||||||||||||||||||||||||||
| 26 | 9 | ▶ | $self->{RESULTSET_METHOD} = $options->{RESULTSET_METHOD} || undef; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options->{'RESULTSET_METHOD'} || undef
MC/DC100%1/1
$options->{'RESULTSET_METHOD'} || undef
$options->{'RESULTSET_METHOD'}
Truth table100%2/2
A
A= $options->{'RESULTSET_METHOD'}
| |||||||||||||||||||||||||||||||||||
| 27 | 9 | ▶ | $self->{TOLERANT_QUERY} = $options->{TOLERANT_QUERY} || 0; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$options->{'TOLERANT_QUERY'} || 0
MC/DC0%0/1
$options->{'TOLERANT_QUERY'} || 0
$options->{'TOLERANT_QUERY'}
Truth table50%1/2
A
A= $options->{'TOLERANT_QUERY'}
| |||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||
| 29 | 9 | ▶ | $self->resultset( $options->{RESULTSET}, $self->{RESTRICTBY_NAME} ) if $options->{RESULTSET}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||
| 31 | 9 | ▶ | if ( $options->{SCHEMA} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 32 | 1 | return $self->error( __PACKAGE__ . ' does not support the SCHEMA option' ); | |||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||
| 35 | # Use Template::Provider's ->_init() to create the COMPILE_DIR... | ||||||||||||||||||||||||||||||||||
| 36 | 8 | $self->SUPER::_init($options); | |||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||
| 38 | 8 | ▶ | $self->{DEBUG} = $options->{DEBUG} || 0; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$options->{'DEBUG'} || 0
MC/DC0%0/1
$options->{'DEBUG'} || 0
$options->{'DEBUG'}
Truth table50%1/2
A
A= $options->{'DEBUG'}
| |||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||
| 40 | 8 | return $self; | |||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||
| 42 | } ## end sub _init | ||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||
| 44 | sub resultset { | ||||||||||||||||||||||||||||||||||
| 45 | 17 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
resultset: called CC=11 SCAR=28.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||
| 47 | 17 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 48 | 17 | $self->{RESULTSET} = shift; | |||||||||||||||||||||||||||||||||
| 49 | 17 | ▶ | my $restrictby_name = shift || ''; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() || ''
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||
| 50 | # $self->debug("Setting resultset: ". p($self->{ RESULTSET })) if $self->{ DEBUG }; | ||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||
| 52 | 17 | my $storage = $self->{RESULTSET}->result_source->schema->storage; | |||||||||||||||||||||||||||||||||
| 53 | 17 | my $connect_info = $storage->connect_info->[0]; | |||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||
| 55 | # The connection DSN will be used when caching templates. | ||||||||||||||||||||||||||||||||||
| 56 | 17 | ▶ | $self->{DSN} = ref $connect_info ? $connect_info->{dsn} : $connect_info; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 57 | 17 | $self->{DSN} = ( split( ';', $self->{DSN} ) )[0]; | |||||||||||||||||||||||||||||||||
| 58 | 17 | $self->{DSN} =~ s/[\/:]/-/g; | |||||||||||||||||||||||||||||||||
| 59 | # $self->debug("Setting DSN: $self->{ DSN }") if $self->{ DEBUG }; | ||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||
| 61 | # The resultset could be restricted, so add name-id of restricting object to cache_name so it is guaranteed unique | ||||||||||||||||||||||||||||||||||
| 62 | my $restricting_object = | ||||||||||||||||||||||||||||||||||
| 63 | $self->{RESULTSET}->result_source->schema->can('restricting_object') | ||||||||||||||||||||||||||||||||||
| 64 | ? $self->{RESULTSET}->result_source->schema->restricting_object | ||||||||||||||||||||||||||||||||||
| 65 | 17 | ▶ | : undef; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 66 | 17 | ▶ | if ($restricting_object) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 67 | 2 | ▶ | $restrictby_name = | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 68 | $restrictby_name | ||||||||||||||||||||||||||||||||||
| 69 | ? sprintf( '%s-%s/%s', $restricting_object->result_source->name, $restricting_object->id, $restrictby_name ) | ||||||||||||||||||||||||||||||||||
| 70 | : sprintf( '%s-%s', $restricting_object->result_source->name, $restricting_object->id ); | ||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||
| 72 | 17 | $self->{RESTRICTBY_NAME} = $restrictby_name; | |||||||||||||||||||||||||||||||||
| 73 | 17 | ▶ | $self->debug("RESTRICTBY_NAME: $self->{RESTRICTBY_NAME} ") if $self->{DEBUG}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||
| 75 | # ...and add a directory for templates cached by this provider. | ||||||||||||||||||||||||||||||||||
| 76 | 17 | ▶ | if ( $self->{COMPILE_DIR} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 77 | # Adapted from Template::Provider 2.94 | ||||||||||||||||||||||||||||||||||
| 78 | 0 | require File::Spec; | |||||||||||||||||||||||||||||||||
| 79 | 0 | require File::Path; | |||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||
| 81 | 0 | my $wdir = $self->{DSN}; | |||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | $wdir =~ s[:][]g if $^O eq 'MSWin32'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 83 | 0 | $wdir =~ /(.*)/; # untaint | |||||||||||||||||||||||||||||||||
| 84 | 0 | $wdir = "$1"; # quotes work around bug in Strawberry Perl | |||||||||||||||||||||||||||||||||
| 85 | 0 | $wdir = File::Spec->catfile( $self->{COMPILE_DIR}, $wdir ); | |||||||||||||||||||||||||||||||||
| 86 | # $self->debug("Checking wdir: $wdir") if $self->{ DEBUG }; | ||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | File::Path::mkpath($wdir) unless -d $wdir; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 88 | } ## end if ( $self->{COMPILE_DIR} ) | ||||||||||||||||||||||||||||||||||
| 89 | } ## end if (@_) | ||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||
| 91 | 17 | return $self->{RESULTSET}; | |||||||||||||||||||||||||||||||||
| 92 | } ## end sub resultset | ||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||
| 95 | sub _rs_table_name { | ||||||||||||||||||||||||||||||||||
| 96 | 63 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
_rs_table_name: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||
| 97 | 63 | ▶ | return $self->{RESULTSET} ? $self->{RESULTSET}->result_source->name : ''; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||
| 100 | sub cache_name { | ||||||||||||||||||||||||||||||||||
| 101 | 22 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
cache_name: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 102 | 22 | ▶ | my $name = shift || ''; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||
| 103 | 22 | ▶ | $self->debug("Making cache_name from: $name") if $self->{DEBUG}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||
| 105 | # Determine the name of the table we're dealing with. | ||||||||||||||||||||||||||||||||||
| 106 | 22 | my $table = $self->_rs_table_name; | |||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||
| 108 | return $self->{RESTRICTBY_NAME} | ||||||||||||||||||||||||||||||||||
| 109 | 22 | ▶ | ? "$self->{ RESTRICTBY_NAME }/$table/$name" | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 110 | : "$table/$name"; | ||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||
| 112 | } ## end sub cache_name | ||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||
| 114 | sub lookup_name { | ||||||||||||||||||||||||||||||||||
| 115 | 36 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
lookup_name: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 116 | 36 | ▶ | my $name = shift || ''; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||
| 117 | 36 | ▶ | $self->debug("Making lookup_name from: $name") if $self->{DEBUG}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||
| 119 | 36 | my $restrictby_name = $self->{RESTRICTBY_NAME}; | |||||||||||||||||||||||||||||||||
| 120 | 36 | my $table = $self->_rs_table_name; | |||||||||||||||||||||||||||||||||
| 121 | 36 | my $tmpl_ext = $self->{TEMPLATE_EXTENSION}; | |||||||||||||||||||||||||||||||||
| 122 | # $self->debug("Removing: $restrictby_name") if $self->{DEBUG}; | ||||||||||||||||||||||||||||||||||
| 123 | # $self->debug("Removing: $table") if $self->{DEBUG}; | ||||||||||||||||||||||||||||||||||
| 124 | # $self->debug("Removing: $tmpl_ext") if $self->{DEBUG}; | ||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||
| 126 | 36 | ▶ | $name =~ s/$restrictby_name\/// if $restrictby_name; # Don't want to remove just slash by itself if $restrictby_name is empty | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 127 | 36 | ▶ | $name =~ s/$table\/// if $table; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 128 | 36 | $name =~ s/$tmpl_ext//; | |||||||||||||||||||||||||||||||||
| 129 | # $self->debug("Using lookup_name: $name") if $self->{DEBUG}; | ||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||
| 131 | 36 | return $name; | |||||||||||||||||||||||||||||||||
| 132 | } ## end sub lookup_name | ||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||
| 135 | sub fetch { | ||||||||||||||||||||||||||||||||||
| 136 | 22 | ▶ | my ( $self, $name ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
fetch: called CC=23 SCAR=44.4
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 137 | 22 | my $stat_ttl = $self->{STAT_TTL}; | |||||||||||||||||||||||||||||||||
| 138 | # $self->debug("fetch($name)") if $self->{ DEBUG }; | ||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||
| 140 | # We're not interested in GLOBs or file handles. | ||||||||||||||||||||||||||||||||||
| 141 | 22 | ▶ | if ( ref $name ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 142 | 0 | return ( undef, Template::Constants::STATUS_DECLINED ); | |||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||
| 145 | 22 | ▶ | if ( $self->{RESULTSET_METHOD} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 146 | 2 | ▶ | if ( ref $self->{RESULTSET_METHOD} eq 'CODE' ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 147 | 2 | my ( $rs, $restrict_name ) = $self->{RESULTSET_METHOD}->($name); | |||||||||||||||||||||||||||||||||
| 148 | 2 | $self->resultset( | |||||||||||||||||||||||||||||||||
| 149 | $rs, | ||||||||||||||||||||||||||||||||||
| 150 | $restrict_name | ||||||||||||||||||||||||||||||||||
| 151 | ); | ||||||||||||||||||||||||||||||||||
| 152 | } else { | ||||||||||||||||||||||||||||||||||
| 153 | 0 | return $self->error("You must provide a valid coderef for RESULTSET_METHOD"); | |||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||
| 155 | } ## end if ( $self->{RESULTSET_METHOD} ) | ||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||
| 157 | 22 | ▶ | unless ( $self->{RESULTSET} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 158 | 0 | return $self->error("You must provide a DBIx::Class::ResultSet before calling fetch"); | |||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||
| 161 | 22 | my $cache_name = $self->cache_name($name); | |||||||||||||||||||||||||||||||||
| 162 | # $self->debug("Using cache_name: $cache_name") if $self->{DEBUG}; | ||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||
| 164 | # Determine the path this template would be cached to. | ||||||||||||||||||||||||||||||||||
| 165 | 22 | my $compiled_filename = $self->_compiled_filename( $self->{DSN} . "/$cache_name" ); | |||||||||||||||||||||||||||||||||
| 166 | # $self->debug("compiled_filename: $compiled_filename") if $self->{ DEBUG }; | ||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||
| 168 | 22 | my ( $data, $error, $slot ); | |||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||
| 170 | # Is caching enabled? | ||||||||||||||||||||||||||||||||||
| 171 | 22 | my $size = $self->{SIZE}; | |||||||||||||||||||||||||||||||||
| 172 | 22 | ▶ | my $caching = !defined $size || $size; | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
!defined($size) || $size
MC/DC0%0/2
!defined($size) || $size
!defined($size)
$size
Truth table33%1/3
not(A) || B
A= !defined($size)B= $size
| |||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||
| 174 | # Otherwise, see if we already know the template is not found | ||||||||||||||||||||||||||||||||||
| 175 | 22 | ▶ | if ( my $last_stat_time = $self->{NOTFOUND}->{$cache_name} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 176 | #$self->debug("In NOTFOUND: $cache_name") if $self->{ DEBUG }; | ||||||||||||||||||||||||||||||||||
| 177 | 0 | my $expires_in = $last_stat_time + $stat_ttl - time; | |||||||||||||||||||||||||||||||||
| 178 | 0 | ▶ | if ( $expires_in > 0 ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 179 | $self->debug(" file [$cache_name] in negative cache. Expires in $expires_in seconds") | ||||||||||||||||||||||||||||||||||
| 180 | 0 | ▶ | if $self->{DEBUG}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 181 | 0 | return ( undef, Template::Constants::STATUS_DECLINED ); | |||||||||||||||||||||||||||||||||
| 182 | } else { | ||||||||||||||||||||||||||||||||||
| 183 | 0 | delete $self->{NOTFOUND}->{$name}; | |||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||
| 185 | } ## end if ( my $last_stat_time = $self->...) | ||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||
| 187 | # If caching is enabled and an entry already exists, refresh its cache | ||||||||||||||||||||||||||||||||||
| 188 | # slot and extract the data... | ||||||||||||||||||||||||||||||||||
| 189 | 22 | ▶ | if ( $caching && ( $slot = $self->{LOOKUP}->{$cache_name} ) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
$caching and $slot = $self->{"LOOKUP"}{$cache_name}
MC/DC0%0/2
$caching and $slot = $self->{"LOOKUP"}{$cache_name}
$caching
$slot = $self->{"LOOKUP"}{$cache_name}
Truth table33%1/3
A and B
A= $cachingB= $slot = $self->{"LOOKUP"}{$cache_name}
| |||||||||||||||||||||||||||||||||||
| 190 | 0 | ( $data, $error ) = $self->_refresh($slot); | |||||||||||||||||||||||||||||||||
| 191 | 0 | ▶ | $data = $slot->[Template::Provider::DATA] unless $error; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 192 | # $self->debug("fetch - lookup from memory") if $self->{ DEBUG }; | ||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||
| 194 | # ...otherwise if this template has already been compiled and cached (but | ||||||||||||||||||||||||||||||||||
| 195 | # not by this object) try to load it from the disk, providing it hasn't | ||||||||||||||||||||||||||||||||||
| 196 | # been modified... | ||||||||||||||||||||||||||||||||||
| 197 | ▶ | elsif ($compiled_filename | |||||||||||||||||||||||||||||||||
|
Condition33%2/6
$compiled_filename and -f $compiled_filename and not $self->_modified($cache_name, (stat _)[9])
$compiled_filename and -f $compiled_filename
MC/DC0%0/3
$compiled_filename and -f $compiled_filename and not $self->_modified($cache_name, (stat _)[9])
$compiled_filename
-f $compiled_filename
not $self->_modified($cache_name, (stat _)[9])
Truth table25%1/4
A and B and not(C)
A= $compiled_filenameB= -f $compiled_filenameC= not $self->_modified($cache_name, (stat _)[9])
| |||||||||||||||||||||||||||||||||||
| 198 | && -f $compiled_filename | ||||||||||||||||||||||||||||||||||
| 199 | && !$self->_modified( $cache_name, ( stat(_) )[9] ) ) { | ||||||||||||||||||||||||||||||||||
| 200 | # $self->debug("fetch - load from cache disk") if $self->{ DEBUG }; | ||||||||||||||||||||||||||||||||||
| 201 | 0 | $data = $self->_load_compiled($compiled_filename); | |||||||||||||||||||||||||||||||||
| 202 | 0 | ▶ | $error = $self->error() unless $data; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||
| 204 | # Save the new data where caching is enabled. | ||||||||||||||||||||||||||||||||||
| 205 | 0 | ▶ | $self->store( $cache_name, $data ) if $caching && !$error; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$caching and not $error
MC/DC0%0/2
$caching and not $error
$caching
not $error
Truth table0%0/3
A and not(B)
A= $cachingB= not $error
| |||||||||||||||||||||||||||||||||||
| 206 | } ## end elsif ( $compiled_filename && -f $compiled_filename...) | ||||||||||||||||||||||||||||||||||
| 207 | # ...else there is nothing already cached for this template so load it | ||||||||||||||||||||||||||||||||||
| 208 | # from the database. | ||||||||||||||||||||||||||||||||||
| 209 | else { | ||||||||||||||||||||||||||||||||||
| 210 | # $self->debug("fetch - lookup from database") if $self->{ DEBUG }; | ||||||||||||||||||||||||||||||||||
| 211 | 22 | ( $data, $error ) = $self->_load("$name"); | |||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||
| 213 | 22 | ▶ | if ($error) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 214 | # Template could not be fetched. Add to the negative/notfound cache. | ||||||||||||||||||||||||||||||||||
| 215 | #$self->debug("Adding to NOTFOUND: $cache_name") if $self->{ DEBUG }; | ||||||||||||||||||||||||||||||||||
| 216 | 8 | $self->{NOTFOUND}->{$cache_name} = time; | |||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||
| 219 | 22 | ▶ | if ( !$error ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 220 | 14 | ( $data, $error ) = $self->_compile( $data, $compiled_filename ); | |||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||
| 223 | # Save the new data where caching is enabled. | ||||||||||||||||||||||||||||||||||
| 224 | 22 | ▶ | if ( !$error ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 225 | 14 | ▶ | $self->debug("Storing in cache as: $cache_name") if $self->{ DEBUG }; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 226 | $data = | ||||||||||||||||||||||||||||||||||
| 227 | $caching | ||||||||||||||||||||||||||||||||||
| 228 | ? $self->_store( $cache_name, $data ) | ||||||||||||||||||||||||||||||||||
| 229 | 14 | ▶ | : $data->{data}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||
| 231 | } ## end else [ if ( $caching && ( $slot = $self...))] | ||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||
| 233 | 22 | return ( $data, $error ); | |||||||||||||||||||||||||||||||||
| 234 | } ## end sub fetch | ||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||
| 236 | sub get_template { | ||||||||||||||||||||||||||||||||||
| 237 | 36 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
get_template: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 238 | 36 | my $lookup_name = shift; | |||||||||||||||||||||||||||||||||
| 239 | 36 | my @columns = @_; | |||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||
| 241 | my $find_crit = { | ||||||||||||||||||||||||||||||||||
| 242 | $self->{COLUMN_NAME} => $lookup_name, | ||||||||||||||||||||||||||||||||||
| 243 | 36 | %{ $self->{SEARCH_CRITERIA} } | |||||||||||||||||||||||||||||||||
| 244 | }; | ||||||||||||||||||||||||||||||||||
| 245 | 36 | my $find_attr = { columns => [map { $self->{$_} } @columns] }; | |||||||||||||||||||||||||||||||||
| 246 | # $self->debug("find_crit: " . p($find_crit)) if $self->{ DEBUG }; | ||||||||||||||||||||||||||||||||||
| 247 | # $self->debug("find_attr: " . p($find_attr)) if $self->{ DEBUG }; | ||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||
| 249 | ## I prefer to use `find`, but that means user is responsible for | ||||||||||||||||||||||||||||||||||
| 250 | ## passing a $resultset that will only match *one* template name. | ||||||||||||||||||||||||||||||||||
| 251 | ## And we should return error (STATUS_ERROR?? STATUS_DECLINED??) | ||||||||||||||||||||||||||||||||||
| 252 | ## if more than one template is found | ||||||||||||||||||||||||||||||||||
| 253 | # my $template = $self->{RESULTSET}->find( | ||||||||||||||||||||||||||||||||||
| 254 | # $find_crit, | ||||||||||||||||||||||||||||||||||
| 255 | # $find_attr, | ||||||||||||||||||||||||||||||||||
| 256 | # ); | ||||||||||||||||||||||||||||||||||
| 257 | my $template_rs = $self->{RESULTSET}->search( | ||||||||||||||||||||||||||||||||||
| 258 | 36 | $find_crit, | |||||||||||||||||||||||||||||||||
| 259 | $find_attr, | ||||||||||||||||||||||||||||||||||
| 260 | ); | ||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||
| 262 | 36 | ▶ | if ($template_rs->count > 1) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 263 | 3 | ▶ | $self->debug("get_template for $lookup_name has more than one record") if $self->{ DEBUG }; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 264 | 3 | return (undef, "More then one template matching '$lookup_name' was found in the resultset"); | |||||||||||||||||||||||||||||||||
| 265 | } elsif($template_rs->count == 1) { | ||||||||||||||||||||||||||||||||||
| 266 | 28 | ▶ | $self->debug("get_template for $lookup_name found one record") if $self->{ DEBUG }; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 267 | 28 | return ($template_rs->first, 0) | |||||||||||||||||||||||||||||||||
| 268 | } else { | ||||||||||||||||||||||||||||||||||
| 269 | 5 | ▶ | $self->debug("get_template for $lookup_name didn't find any records") if $self->{ DEBUG }; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 270 | 5 | return (undef, 0) | |||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||
| 275 | sub _load { | ||||||||||||||||||||||||||||||||||
| 276 | 22 | ▶ | my ( $self, $name ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_load: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||||||||
| 277 | 22 | my ( $data, $error ); | |||||||||||||||||||||||||||||||||
| 278 | # $self->debug("_load($name)") if $self->{DEBUG}; | ||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||
| 280 | 22 | my $lookup_name = $self->lookup_name($name); | |||||||||||||||||||||||||||||||||
| 281 | # $self->debug("_load for $lookup_name") if $self->{ DEBUG }; | ||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||
| 283 | # Try to retrieve the template from the database. | ||||||||||||||||||||||||||||||||||
| 284 | 22 | my ( $template, $tmpl_error ) = $self->get_template($lookup_name, qw/COLUMN_CONTENT COLUMN_MODIFIED/); | |||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||
| 286 | 22 | ▶ | if ($tmpl_error) { | ||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||
| 287 | ( $data, $error ) = $self->{TOLERANT_QUERY} | ||||||||||||||||||||||||||||||||||
| 288 | ? ( | ||||||||||||||||||||||||||||||||||
| 289 | undef, | ||||||||||||||||||||||||||||||||||
| 290 | 3 | ▶ | Template::Constants::STATUS_DECLINED | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 291 | ) | ||||||||||||||||||||||||||||||||||
| 292 | : ( | ||||||||||||||||||||||||||||||||||
| 293 | $tmpl_error, | ||||||||||||||||||||||||||||||||||
| 294 | Template::Constants::STATUS_ERROR | ||||||||||||||||||||||||||||||||||
| 295 | ); | ||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||
| 297 | } elsif ($template) { | ||||||||||||||||||||||||||||||||||
| 298 | $data = { | ||||||||||||||||||||||||||||||||||
| 299 | name => $lookup_name, | ||||||||||||||||||||||||||||||||||
| 300 | text => $template->get_column( $self->{COLUMN_CONTENT} ), | ||||||||||||||||||||||||||||||||||
| 301 | time => Date::Parse::str2time( | ||||||||||||||||||||||||||||||||||
| 302 | $template->get_column( $self->{COLUMN_MODIFIED} ) | ||||||||||||||||||||||||||||||||||
| 303 | 14 | ), | |||||||||||||||||||||||||||||||||
| 304 | load => time, | ||||||||||||||||||||||||||||||||||
| 305 | }; | ||||||||||||||||||||||||||||||||||
| 306 | 14 | ▶ | $self->debug("LOADED $lookup_name") if $self->{ DEBUG }; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||
| 308 | } elsif ( $self->{TOLERANT} ) { | ||||||||||||||||||||||||||||||||||
| 309 | 0 | ( $data, $error ) = ( undef, Template::Constants::STATUS_DECLINED ); | |||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||
| 311 | } else { | ||||||||||||||||||||||||||||||||||
| 312 | 5 | ( $data, $error ) = ( | |||||||||||||||||||||||||||||||||
| 313 | "Could not retrieve '$lookup_name' from the resultset '" . $self->_rs_table_name . "'", | ||||||||||||||||||||||||||||||||||
| 314 | Template::Constants::STATUS_ERROR | ||||||||||||||||||||||||||||||||||
| 315 | ); | ||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||
| 318 | 22 | return ( $data, $error ); | |||||||||||||||||||||||||||||||||
| 319 | } ## end sub _load | ||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||
| 322 | sub _modified { | ||||||||||||||||||||||||||||||||||
| 323 | 14 | ▶ | my ( $self, $name, $time ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_modified: called CC=6 SCAR=18.7
| |||||||||||||||||||||||||||||||||||
| 324 | # $self->debug("_modified($name)") if $self->{DEBUG}; | ||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||
| 326 | 14 | my $lookup_name = $self->lookup_name($name); | |||||||||||||||||||||||||||||||||
| 327 | # $self->debug("Check modified for $lookup_name") if $self->{DEBUG}; | ||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||
| 329 | # Try to retrieve the template from the database. | ||||||||||||||||||||||||||||||||||
| 330 | 14 | my ( $template, $tmpl_error ) = $self->get_template($lookup_name, qw/COLUMN_MODIFIED/); | |||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||
| 332 | 14 | ▶ | return 0 if $tmpl_error; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 333 | 14 | ▶ | return 0 unless $template; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||
| 335 | 14 | ▶ | my $modified = Date::Parse::str2time( $template->get_column( $self->{COLUMN_MODIFIED} ) ) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
Date::Parse::str2time($template->get_column($self->{'COLUMN_MODIFIED'})) || (return $time ? 1 : 0)
MC/DC100%1/1
Date::Parse::str2time($template->get_column($self->{'COLUMN_MODIFIED'})) || (return $time ? 1 : 0)
Date::Parse::str2time($template->get_column($self->{'COLUMN_MODIFIED'}))
Truth table100%2/2
A
A= Date::Parse::str2time($template->get_column($self->{'COLUMN_MODIFIED'}))
| |||||||||||||||||||||||||||||||||||
| 336 | || return $time ? 1 : 0; | ||||||||||||||||||||||||||||||||||
| 337 | # $self->debug("Modified $lookup_name at $modified") if $self->{DEBUG}; | ||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||
| 339 | 1 | ▶ | return $time ? $modified > $time : $modified; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 340 | } ## end sub _modified | ||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||
| 343 | 9 | 1; | |||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||
| 345 | __END__ | ||||||||||||||||||||||||||||||||||