blib/lib/Catalyst/Plugin/Static/TT.pm

filter: Statementstmt 88.5% 62 / 70 Branchbran 12.5% 1 / 8 Conditioncond 47.0% 16 / 34 MC/DCmcdc 5.0% 1 / 20 Subroutinesub 92.3% 12 / 13 Podpod 100% 1 / 1 totaltotal 63.7% 93 / 146 CC 22 SCAR 23.3
CC
22
Cov
71%
_compile_static_tt_file
20.0
_setup_static_tt
17.0
_compile_static_tt_all
7.3
CRAP 10.3
↑ Summary
1 package Catalyst::Plugin::Static::TT;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use NEXT;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 Catalyst::Plugin::Static::TT - generate 'static' content with TT
15  
16 =head1 VERSION
17  
18 Version 0.002
19  
20 =cut
21  
22 3 our $VERSION = '0.002';
23  
24 =head1 SYNOPSIS
25  
26 use Catalyst qw/Static::TT Static::Simple/;
27  
28 =head1 DESCRIPTION
29  
30 Sometimes you have 'static' content that isn't really
31 static. It stays the same for the entire lifetime of your
32 application, or maybe it only changes depending on whether
33 you're in your development or production environment.
34  
35 You'd really like to generate this content only once and
36 then avoid filling it out again for each request.
37  
38 =head1 CONFIGURATION
39  
40 All these options may be set in the C<< MyApp->config->{static_tt} >>.
41  
42 =over 4
43  
44 =item stash
45  
46 stash for your templates
47  
48 =item dirs
49  
50 arrayref of directories to search for templates (default: 'static')
51  
52 =item include_path
53  
54 as TT's INCLUDE_PATH (default: C<< MyApp->config->{root} >>)
55  
56 =item extensions
57  
58 arrayref of extensions to process (default: 'tt', 'tt2')
59  
60 =item tt_config
61  
62 hashref of extra configuration for TT (default: empty)
63  
64 =item catalyst_var
65  
66 name of your Catalyst context variable (default: 'c')
67  
68 =back
69  
70 =head1 INTERNAL METHODS
71  
72 =head2 setup
73  
74 Configures and then compiles all static-TT files.
75  
76 =cut
77  
78 2 ▶sub setup {
Subroutine setup: called CC=1 SCAR=0.0
Pod documented
79 2 my $c = shift;
80 2 $c->NEXT::setup(@_);
81 2 $c->_setup_static_tt;
82 2 $c->_compile_static_tt_all;
83 }
84  
85 2 ▶sub _setup_static_tt {
Subroutine _setup_static_tt: called CC=12 SCAR=28.3
86 2 my $c = shift;
87
88 2 ▶ my $config = $c->config->{static_tt} ||= {};
Condition100%2/2
$c->config->{"static_tt"} ||= {}
l!l
11
MC/DC100%1/1
$c->config->{"static_tt"} ||= {}
$c->config->{"static_tt"}
Truth table100%2/2
A
A= $c->config->{"static_tt"}
Aresult
11
00
89 2 ▶ $config->{stash} ||= {};
Condition50%1/2
$config->{"stash"} ||= {}
l!l
02
MC/DC0%0/1
$config->{"stash"} ||= {}
$config->{"stash"}
Truth table50%1/2
A
A= $config->{"stash"}
Aresult
11
00
90 2 ▶ $config->{catalyst_var} ||= 'c';
Condition50%1/2
$config->{"catalyst_var"} ||= "c"
l!l
02
MC/DC0%0/1
$config->{"catalyst_var"} ||= "c"
$config->{"catalyst_var"}
Truth table50%1/2
A
A= $config->{"catalyst_var"}
Aresult
11
00
91 2 ▶ $config->{dirs} ||= [ qw(static) ];
Condition50%1/2
$config->{"dirs"} ||= ["static"]
l!l
02
MC/DC0%0/1
$config->{"dirs"} ||= ["static"]
$config->{"dirs"}
Truth table50%1/2
A
A= $config->{"dirs"}
Aresult
11
00
92 2 ▶ $config->{include_path} ||= [ $c->config->{root} ];
Condition50%1/2
$config->{"include_path"} ||= [$c->config->{"root"}]
l!l
02
MC/DC0%0/1
$config->{"include_path"} ||= [$c->config->{"root"}]
$config->{"include_path"}
Truth table50%1/2
A
A= $config->{"include_path"}
Aresult
11
00
93 2 ▶ $config->{extensions} ||= [ qw(tt tt2) ];
Condition50%1/2
$config->{"extensions"} ||= ["tt", "tt2"]
l!l
02
MC/DC0%0/1
$config->{"extensions"} ||= ["tt", "tt2"]
$config->{"extensions"}
Truth table50%1/2
A
A= $config->{"extensions"}
Aresult
11
00
94 2 ▶ $config->{debug} ||= $c->debug;
Condition33%1/3
$config->{"debug"} ||= $c->debug
l!l&&r!l&&!r
002
MC/DC0%0/2
$config->{"debug"} ||= $c->debug
$config->{"debug"} $c->debug
Truth table33%1/3
A ||= B
A= $config->{"debug"}B= $c->debug
ABresult
1X1
011
000
95 2 ▶ $config->{tt_config} ||= {};
Condition50%1/2
$config->{"tt_config"} ||= {}
l!l
02
MC/DC0%0/1
$config->{"tt_config"} ||= {}
$config->{"tt_config"}
Truth table50%1/2
A
A= $config->{"tt_config"}
Aresult
11
00
96 # output_root, output_name, and after_compile aren't documented yet -- hdp,
97 # 2007-07-24
98 2 ▶ $config->{output_root} ||= '';
Condition50%1/2
$config->{"output_root"} ||= ""
l!l
02
MC/DC0%0/1
$config->{"output_root"} ||= ""
$config->{"output_root"}
Truth table50%1/2
A
A= $config->{"output_root"}
Aresult
11
00
99 2 ▶ $config->{output_name} ||= sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
100 2 ▶ File::Spec->catfile($config->{output_root} || (), @_);
Condition33%1/3
$config->{'output_root'} || ()
l!l&&r!l&&!r
020
MC/DC0%0/2
$config->{'output_root'} || ()
$config->{'output_root'} ()
Truth table33%1/3
A || B
A= $config->{'output_root'}B= ()
ABresult
1X1
011
000
101 2 ▶ };
Condition66%2/3
$config->{"output_name"} ||= sub { "File::Spec"->catfile($config->{"output_root"} || (), @_); }
l!l&&r!l&&!r
110
MC/DC0%0/2
$config->{"output_name"} ||= sub { "File::Spec"->catfile($config->{"output_root"} || (), @_); }
$config->{"output_name"} sub { "File::Spec"->catfile($config->{"output_root"} || (), @_); }
Truth table66%2/3
A ||= B
A= $config->{"output_name"}B= sub { "File::Spec"->catfile($config->{"output_root"} || (), @_); }
ABresult
1X1
011
000
102 2 ▶ $config->{after_compile} ||= sub { () };
Subroutine __ANON__: called CC=1 SCAR=0.6
Condition33%1/3
$config->{"after_compile"} ||= sub { (); }
l!l&&r!l&&!r
020
MC/DC0%0/2
$config->{"after_compile"} ||= sub { (); }
$config->{"after_compile"} sub { (); }
Truth table33%1/3
A ||= B
A= $config->{"after_compile"}B= sub { (); }
ABresult
1X1
011
000
103
104 2 $config->{stash}->{$config->{catalyst_var}} = $c;
105 }
106  
107 2 ▶sub _compile_static_tt_all {
Subroutine _compile_static_tt_all: called CC=7 SCAR=19.8
108 2 my $c = shift;
109 2 my $config = $c->config->{static_tt};
110 2 ▶ return if $config->{no_compile} and not $ENV{CATALYST_STATIC_TT_COMPILE};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"no_compile"} and not $ENV{"CATALYST_STATIC_TT_COMPILE"} 0 2 2
Condition33%1/3
$config->{"no_compile"} and not $ENV{"CATALYST_STATIC_TT_COMPILE"}
!ll&&!rl&&r
200
MC/DC0%0/2
$config->{"no_compile"} and not $ENV{"CATALYST_STATIC_TT_COMPILE"}
$config->{"no_compile"} not $ENV{"CATALYST_STATIC_TT_COMPILE"}
Truth table33%1/3
A and not(B)
A= $config->{"no_compile"}B= not $ENV{"CATALYST_STATIC_TT_COMPILE"}
ABresult
0X0
100
111
111 my $tt = $config->{_tt} ||= Template->new({
112 INCLUDE_PATH => $config->{include_path},
113 2 ▶ %{ $config->{tt_config} },
Condition33%1/3
$config->{"_tt"} ||= "Template"->new({"INCLUDE_PATH", $config->{"include_path"}, %{$config->{"tt_config"};}})
l!l&&r!l&&!r
020
MC/DC0%0/2
$config->{"_tt"} ||= "Template"->new({"INCLUDE_PATH", $config->{"include_path"}, %{$config->{"tt_config"};}})
$config->{"_tt"} "Template"->new({"INCLUDE_PATH", $config->{"include_path"}, %{$config->{"tt_config"};}})
Truth table33%1/3
A ||= B
A= $config->{"_tt"}B= "Template"->new({"INCLUDE_PATH", $config->{"include_path"}, %{$config->{"tt_config"};}})
ABresult
1X1
011
000
114 });
115  
116 my $rule = File::Find::Rule->file->or(
117 2 map { File::Find::Rule->name("*.$_") } @{ $config->{extensions} }
118 )->relative;
119  
120 2 for my $ipath (@{ $config->{include_path} }) {
121 2 for my $dir (@{ $config->{dirs} }) {
122 2 my $search_dir = File::Spec->catdir($ipath, $dir);
123 2 for my $file ($rule->in($search_dir)) {
124 4 my $infile = File::Spec->catfile($dir, $file);
125 my $outfile = $config->{output_name}->(
126 $ipath, $dir,
127 (File::Basename::fileparse(
128 4 $file, map { ".$_" } @{ $config->{extensions} }
129 ))[1, 0],
130 );
131  
132 4 $c->_compile_static_tt_file($infile, $outfile);
133 }
134 }
135 }
136  
137 2 $config->{after_compile}->();
138 }
139  
140 0 ▶sub _compile_static_tt_file {
Subroutine _compile_static_tt_file: not called CC=4 SCAR=30.0
141 0 my ($c, $infile, $outfile) = @_;
142 0 my $config = $c->config->{static_tt};
143 0 my $tt = $config->{_tt};
144  
145 # have to use $^W to avoid warning about
146 # UNIVERSAL::can used as a function instead of a
147 # method; see Template and Template::Provider
148
149 {
150 0 local $^W;
151 $tt->process(
152 0 ▶ $infile, $config->{stash}, "$outfile.tmp",
Branch: 1 branch
BranchTrueFalseTotal
$tt->process($infile, $config->{"stash"}, "$outfile.tmp") or die $tt->error . "\n" 0 0 0
153 ) || die $tt->error . "\n";
154 }
155  
156 0 ▶ rename "$outfile.tmp", $outfile
Branch: 1 branch
BranchTrueFalseTotal
rename "$outfile.tmp", $outfile or die "Can't rename $outfile.tmp to ${outfile}: $!" 0 0 0
157 or die "Can't rename $outfile.tmp to $outfile: $!";
158 $config->{debug} and
159 0 ▶ $c->log->debug("Compiled $infile to static $outfile");
Branch: 1 branch
BranchTrueFalseTotal
$config->{"debug"} and $c->log->debug("Compiled $infile to static $outfile") 0 0 0
160 }
161  
162 =head1 AUTHOR
163  
164 Hans Dieter Pearcey, C<< <hdp at cpan.org> >>
165  
166 =head1 BUGS
167  
168 Please report any bugs or feature requests to
169 C<bug-catalyst-plugin-static-tt at rt.cpan.org>, or through the web interface at
170 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Plugin-Static-TT>.
171 I will be notified, and then you'll automatically be notified of progress on
172 your bug as I make changes.
173  
174 =head1 SUPPORT
175  
176 You can find documentation for this module with the perldoc command.
177  
178 perldoc Catalyst::Plugin::Static::TT
179  
180 You can also look for information at:
181  
182 =over 4
183  
184 =item * AnnoCPAN: Annotated CPAN documentation
185  
186 L<http://annocpan.org/dist/Catalyst-Plugin-Static-TT>
187  
188 =item * CPAN Ratings
189  
190 L<http://cpanratings.perl.org/d/Catalyst-Plugin-Static-TT>
191  
192 =item * RT: CPAN's request tracker
193  
194 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Plugin-Static-TT>
195  
196 =item * Search CPAN
197  
198 L<http://search.cpan.org/dist/Catalyst-Plugin-Static-TT>
199  
200 =back
201  
202 This module is hosted on the public git repository at L<http://repo.or.cz/>.
203  
204 git clone http://repo.or.cz/w/Catalyst-Plugin-Static-TT.git
205  
206 =head1 ACKNOWLEDGEMENTS
207  
208 =head1 COPYRIGHT & LICENSE
209  
210 Copyright 2006 Hans Dieter Pearcey, all rights reserved.
211  
212 This program is free software; you can redistribute it and/or modify it
213 under the same terms as Perl itself.
214  
215 =cut
216  
217 3 1; # End of Catalyst::Plugin::Static::TT
↑ Summary