File Coverage

blib/lib/Plack/Middleware/TMT.pm
Criterion Covered Total %
statement 68 70 97.1
branch 18 24 75.0
condition 7 11 63.6
subroutine 16 17 94.1
pod 3 3 100.0
total 112 125 89.6


line stmt bran cond sub pod time code
1             package Plack::Middleware::TMT;
2 2     2   22233 use strict;
  2         4  
  2         71  
3 2     2   9 use warnings;
  2         3  
  2         76  
4             BEGIN {
5 2     2   37 $Plack::Middleware::TMT::VERSION = '0.04';
6             }
7 2     2   41 use Carp qw/croak/;
  2         6  
  2         144  
8 2     2   723 use parent 'Plack::Middleware';
  2         324  
  2         12  
9 2     2   20203 use Text::MicroTemplate::File;
  2         15721  
  2         101  
10 2     2   53251 use Plack::Request;
  2         463115  
  2         79  
11 2     2   8299 use Data::Dumper;
  2         19783  
  2         736  
12 2         27 use Plack::Util::Accessor qw/
13             tmt
14             include_path default_tmpl tmpl_extension
15             use_cache
16             content_type default_content_type
17             macro package_name
18             pass_through
19 2     2   29 /;
  2         6  
20              
21             sub prepare_app {
22 10     10 1 19594 my $self = shift;
23              
24 10 50       49 $self->default_tmpl('index') if !$self->default_tmpl;
25 10 50       665 $self->default_content_type('text/html') if !$self->default_content_type;
26 10 100       244 $self->pass_through(0) if !$self->pass_through;
27              
28 10         136 $self->tmt(
29             Text::MicroTemplate::File->new(
30             include_path => $self->include_path,
31             use_cache => $self->use_cache,
32             package_name => $self->package_name,
33             )
34             );
35              
36 10 100       784 $self->macro(+{}) if !$self->macro;
37 10 50       129 warn 'macro "d" is reserved. it works as dumper.' if $self->macro->{d};
38             $self->macro->{d} = sub {
39 1     1   900 local $Data::Dumper::Terse = 1;
40 1         4 local $Data::Dumper::Indent = 1;
41 1         3 local $Data::Dumper::Sortkeys = 1;
42 1         8 Data::Dumper::Dumper(shift);
43 10         142 };
44              
45 10         57 for my $name (keys %{ $self->macro }) {
  10         25  
46 11 50       125 unless ($name =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/) {
47 0         0 croak qq{Invalid macro key name: "$name"};
48             }
49 2     2   1164 no strict 'refs'; ## no critic
  2         4  
  2         93  
50 2     2   99 no warnings 'redefine';
  2         5  
  2         1879  
51 11         268 my $code = $self->{macro}{$name};
52 11     0   29 *{ $self->tmt->package_name . "::$name" }
  0         0  
53 11 50       39 = ref $code eq 'CODE' ? $code : sub {$code};
54             }
55             }
56              
57             sub call {
58 10     10 1 37690 my ( $self, $env ) = @_;
59              
60 10         43 my $res = $self->_handle_template($env);
61              
62 10 100 66     71 if ( $res && not( $self->pass_through and $res->[0] == 404 ) ) {
      66        
63 9         197 return $res;
64             }
65 1 50       22 if ( $self->app ) {
66 1         12 $res = $self->app->($env);
67             }
68              
69 1         21 $res;
70             }
71              
72             sub _handle_template {
73 10     10   19 my ($self, $env) = @_;
74              
75 10         89 my $req = Plack::Request->new($env);
76              
77 10 100       187 my $tmpl = $req->path eq '/'
    100          
78             ? $self->default_tmpl
79             : $req->path =~ m!/$!
80             ? $req->path. $self->default_tmpl
81             : $req->path;
82 10         555 $tmpl =~ s!^/!!;
83              
84 10   100     46 my $ext = $self->tmpl_extension || '';
85              
86 10 100       846 if (!-e $self->include_path. '/'. "$tmpl$ext") {
87 2         80 return [404, ['Content-Type' => 'text/plain'], ['Not Found']];
88             }
89              
90             $self->process_template(
91 8         382 "$tmpl$ext",
92             200,
93             $req,
94             );
95             }
96              
97             sub process_template {
98 8     8 1 17 my ( $self, $template, $status_code, $vars ) = @_;
99              
100 8   33     29 my $content_type = $self->content_type || $self->default_content_type;
101 8         124 my $content = $self->tmt->render_file($template, $vars)->as_string;
102              
103 8         7250 return [ $status_code, [ 'Content-Type' => $content_type ], [$content] ];
104             }
105              
106             1;
107              
108             __END__