| 1 | package CGI::Application::Plugin::TT::LastModified; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | ############################################################################### | ||||||||||||||||||||
| 4 | # Required inclusions. | ||||||||||||||||||||
| 5 | ############################################################################### | ||||||||||||||||||||
| 6 | 4 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 4 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 4 | ▶ | use CGI::Util qw(expires); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 4 | ▶ | use List::Util qw(max); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | ############################################################################### | ||||||||||||||||||||
| 12 | # Version numbering. | ||||||||||||||||||||
| 13 | ############################################################################### | ||||||||||||||||||||
| 14 | 4 | our $VERSION = '1.02'; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | ############################################################################### | ||||||||||||||||||||
| 17 | # Export our methods. | ||||||||||||||||||||
| 18 | ############################################################################### | ||||||||||||||||||||
| 19 | 4 | our @EXPORT = qw( | |||||||||||||||||||
| 20 | tt_last_modified | ||||||||||||||||||||
| 21 | tt_set_last_modified_header | ||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | ############################################################################### | ||||||||||||||||||||
| 25 | # Subroutine: import() | ||||||||||||||||||||
| 26 | ############################################################################### | ||||||||||||||||||||
| 27 | # Custom import routine, which allows for 'tt_set_last_modified_header()' to be | ||||||||||||||||||||
| 28 | # auto-added in as a TT post process hook. | ||||||||||||||||||||
| 29 | ############################################################################### | ||||||||||||||||||||
| 30 | sub import { | ||||||||||||||||||||
| 31 | 3 | ▶ | my $pkg = shift; | ||||||||||||||||||
|
Subroutine
import: called CC=6 SCAR=18.5
| |||||||||||||||||||||
| 32 | 3 | my $auto = shift; | |||||||||||||||||||
| 33 | 3 | my $caller = scalar caller; | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | # manually export our symbols | ||||||||||||||||||||
| 36 | 3 | foreach my $sym (@EXPORT) { | |||||||||||||||||||
| 37 | 4 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 38 | 6 | *{"${caller}::$sym"} = \&{$sym}; | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | # sanity check caller package, and set up auto-header functionality | ||||||||||||||||||||
| 42 | 3 | ▶ | if (not UNIVERSAL::isa($caller, 'CGI::Application')) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 43 | 0 | warn "Calling package is not a CGI::Application module.\n"; | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | elsif (not UNIVERSAL::can($caller, 'tt_obj')) { | ||||||||||||||||||||
| 46 | 0 | warn "Calling package hasn't imported CGI::Application::Plugin::TT.\n"; | |||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | ▶ | elsif ($auto and ($auto eq ':auto')) { | |||||||||||||||||||
|
Condition66%2/3
$auto and $auto eq ":auto"
MC/DC50%1/2
$auto and $auto eq ":auto"
$auto
$auto eq ":auto"
Truth table66%2/3
A and B
A= $autoB= $auto eq ":auto"
| |||||||||||||||||||||
| 49 | 1 | $caller->add_callback( tt_post_process => \&tt_set_last_modified_header ); | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | ############################################################################### | ||||||||||||||||||||
| 54 | # Subroutine: tt_last_modified() | ||||||||||||||||||||
| 55 | ############################################################################### | ||||||||||||||||||||
| 56 | # Returns the most recent modification time for any component of the most | ||||||||||||||||||||
| 57 | # recently processed template (via 'tt_process()'). Time is returned back to | ||||||||||||||||||||
| 58 | # the caller as "the number of seconds since the epoch". | ||||||||||||||||||||
| 59 | ############################################################################### | ||||||||||||||||||||
| 60 | sub tt_last_modified { | ||||||||||||||||||||
| 61 | 2 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
tt_last_modified: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 62 | 2 | my $ctx = $self->tt_obj->context(); | |||||||||||||||||||
| 63 | 2 | my $mtime = 0; | |||||||||||||||||||
| 64 | 2 | foreach my $provider (@{$ctx->{'LOAD_TEMPLATES'}}) { | |||||||||||||||||||
| 65 | 2 | foreach my $file (keys %{$provider->{'LOOKUP'}}) { | |||||||||||||||||||
| 66 | 6 | my $c_mtime = $provider->{'LOOKUP'}{$file}[3]; | |||||||||||||||||||
| 67 | 6 | $mtime = max( $mtime, $c_mtime ); | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | 2 | return $mtime; | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | ############################################################################### | ||||||||||||||||||||
| 74 | # Subroutine: tt_set_last_modified_header() | ||||||||||||||||||||
| 75 | ############################################################################### | ||||||||||||||||||||
| 76 | # Sets a "Last-Modified" header in the HTTP response, equivalent to the last | ||||||||||||||||||||
| 77 | # modification time of the template components as returned by | ||||||||||||||||||||
| 78 | # 'tt_last_modified()'. | ||||||||||||||||||||
| 79 | ############################################################################### | ||||||||||||||||||||
| 80 | sub tt_set_last_modified_header { | ||||||||||||||||||||
| 81 | 2 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
tt_set_last_modified_header: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 82 | 2 | my $mtime = $self->tt_last_modified(); | |||||||||||||||||||
| 83 | 2 | ▶ | if ($mtime) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | 2 | my $lastmod = expires( $mtime, 'http' ); | |||||||||||||||||||
| 85 | 2 | $self->header_add( '-last-modified' => $lastmod ); | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 4 | 1; | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | =head1 NAME | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | CGI::Application::Plugin::TT::LastModified - Set "Last-Modified" header based on TT template | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | =head1 SYNOPSIS | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | # when you want to set the "Last-Modified" header manually | ||||||||||||||||||||
| 98 | use base qw(CGI::Application); | ||||||||||||||||||||
| 99 | use CGI::Application::Plugin::TT; | ||||||||||||||||||||
| 100 | use CGI::Application::Plugin::TT::LastModified; | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | sub my_runmode { | ||||||||||||||||||||
| 103 | my $self = shift; | ||||||||||||||||||||
| 104 | my %params = ( | ||||||||||||||||||||
| 105 | ... | ||||||||||||||||||||
| 106 | ); | ||||||||||||||||||||
| 107 | my $html = $self->tt_process( 'template.html', \%params ); | ||||||||||||||||||||
| 108 | $self->tt_set_last_modified_header(); | ||||||||||||||||||||
| 109 | return $html; | ||||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | # when you want the "Last-Modified" header set automatically | ||||||||||||||||||||
| 113 | use base qw(CGI::Application); | ||||||||||||||||||||
| 114 | use CGI::Application::Plugin::TT; | ||||||||||||||||||||
| 115 | use CGI::Application::Plugin::TT::LastModified qw(:auto); | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | sub my_runmode { | ||||||||||||||||||||
| 118 | my $self = shift; | ||||||||||||||||||||
| 119 | my %params = ( | ||||||||||||||||||||
| 120 | ... | ||||||||||||||||||||
| 121 | ); | ||||||||||||||||||||
| 122 | return $self->tt_process( 'template.html', \%params ); | ||||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | =head1 DESCRIPTION | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | C<CGI::Application::Plugin::TT::LastModified> adds support to | ||||||||||||||||||||
| 128 | C<CGI::Application> for setting a "Last-Modified" header based on the most | ||||||||||||||||||||
| 129 | recent modification time of I<any> of the components of a template that was | ||||||||||||||||||||
| 130 | processed with TT. | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | Normally you'll want to call it manually, on as "as needed" basis; if you're | ||||||||||||||||||||
| 133 | processing templates with TT you're most likely dealing with dynamic content | ||||||||||||||||||||
| 134 | (in which case you probably don't even want a "Last-Modified" header). The odd | ||||||||||||||||||||
| 135 | time you'll want to set a "Last-Modified" header, though, this plugin helps | ||||||||||||||||||||
| 136 | make that easier. | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | B<If> you have a desire to have the "Last-Modified" header set automatically | ||||||||||||||||||||
| 139 | for you, though, C<CGI::Application::Plugin::TT::LastModified> does have an | ||||||||||||||||||||
| 140 | C<:auto> import tag which auto-registers L</tt_set_last_modified_header()> as a | ||||||||||||||||||||
| 141 | "tt_post_process" hook for you. If you've got an app that just processes | ||||||||||||||||||||
| 142 | static TT pages and generates output, this'll be useful for you. | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | =head1 METHODS | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | =over | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | =item import() | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | Custom import routine, which allows for C<tt_set_last_modified_header()> to | ||||||||||||||||||||
| 151 | be auto-added in as a TT post process hook. | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | =item tt_last_modified() | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | Returns the most recent modification time for any component of the most | ||||||||||||||||||||
| 156 | recently processed template (via C<tt_process()>). Time is returned back to | ||||||||||||||||||||
| 157 | the caller as "the number of seconds since the epoch". | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | =item tt_set_last_modified_header() | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | Sets a "Last-Modified" header in the HTTP response, equivalent to the last | ||||||||||||||||||||
| 162 | modification time of the template components as returned by | ||||||||||||||||||||
| 163 | C<tt_last_modified()>. | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | =back | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | =head1 AUTHOR | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | Graham TerMarsch (cpan@howlingfrog.com) | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | =head1 COPYRIGHT | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | Copyright (C) 2007, Graham TerMarsch. All Rights Reserved. | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | This is free software; you can redistribute it and/or modify it under the same | ||||||||||||||||||||
| 176 | terms as Perl itself. | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =head1 SEE ALSO | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | L<CGI::Application::Plugin::TT>, | ||||||||||||||||||||
| 181 | L<CGI::Application>, | ||||||||||||||||||||
| 182 | L<Template>. | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | =cut | ||||||||||||||||||||