Tmojo/SimpleHandler.pm untested

filter: Statementstmt 0.0% 0 / 33 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 51 CC 7 SCAR 35.9
CC
7
Cov
0%
handler
56.0
BEGIN
2.0
BEGIN
2.0
CRAP 36.4
1 ###########################################################################
2 # Copyright 2004 Lab-01 LLC <http://lab-01.com/>
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 # Tmojo(tm) is a trademark of Lab-01 LLC.
17 ###########################################################################
18  
19 package HTML::Tmojo::SimpleHandler;
20  
21 =head1 NAME
22  
23 HTML::Tmojo::SimpleHandler
24  
25 =head1 SYNOPSIS
26  
27 # IN YOUR APACHE CONFIG
28 DirectoryIndex index.tmojo index.html
29  
30 <LocationMatch "\.tmojo$">
31 SetHandler perl-script
32 PerlHandler HTML::Tmojo::SimpleHandler
33 PerlSetEnv TMOJO_CACHE_DIR /tmp/mojo
34 </LocationMatch>
35  
36 =head1 ABSTRACT
37  
38 The SimpleHandler can be used to quickly deploy
39 Tmojo to your apache/mod_perl environment. Simply
40 adding the above code to your httpd.conf will allow
41 Apache to serve any .tmojo documents in your htdocs
42 as Tmojo templates.
43
44 =head1 OPTIONS
45  
46 The SimpleHandler obeys the following environment
47 variables:
48  
49 =over
50  
51 =item TMOJO_TEMPLATE_DIR
52  
53 SimpleHandler will look for its templates in this directory.
54 If not specified, SimpleHandler will look for its templates
55 in the apache document root.
56  
57 =item TMOJO_CACHE_DIR
58  
59 This required setting tells SimpleHandler where to
60 store compiled Tmojo templates. The directory must
61 be writable by apache.
62  
63 =item TMOJO_DEFAULT_CONTAINER
64  
65 If specified, SimpleHandler will use this path to determine
66 the container template on each requested template. This
67 option will override any $TMOJO_CONTAINER specified within
68 templates.
69  
70 Normally, you would use this option to create a directory
71 level containment mechanism. For instance, by setting
72 TMOJO_DEFAULT_CONTAINER to C<container.tmojo^>, you can
73 make the SimpleHandler use the deepest template named
74 F<container.tmojo> as the container for the requested
75 template.
76  
77 =back
78  
79 =head1 AUTHOR
80  
81 Will Conant <will@willconant.com>
82  
83 =cut
84  
85  
86 use strict;
87  
88 use Apache::Constants qw(OK NOT_FOUND);
89  
90 use HTML::Tmojo;
91 use HTML::Tmojo::HttpArgParser;
92  
93 sub handler {
94 my $apache_request = shift;
95
96 # DECIDE ON OUR TEMPLATE DIR
97 my $template_dir = $ENV{TMOJO_TEMPLATE_DIR};
98 if ($template_dir eq '') {
99 $template_dir = $ENV{DOCUMENT_ROOT};
100 }
101
102 # PRIME OUR COOL OBJECTS
103 my $arg_parser = HTML::Tmojo::HttpArgParser->new();
104 my $tmojo = HTML::Tmojo->new(template_dir => $template_dir);
105
106 # DECIDE ON THE DEFAULT CONTAINER
107 my $default_container = $ENV{TMOJO_DEFAULT_CONTAINER};
108
109 # GET THE ARGUMENTS
110 my %args = $arg_parser->args();
111
112 # GET THE TMOJO TEMPLATE PATH
113 my $template_id = $ENV{REQUEST_URI};
114 $template_id =~ s/\?.*$//;
115
116 if (substr($template_id, -1) eq '/') {
117 if ($ENV{TMOJO_DIR_INDEX} ne '') {
118 $template_id .= $ENV{TMOJO_DIR_INDEX};
119 }
120 elsif ($ENV{SCRIPT_FILENAME} =~ m/\/([^\/]+)$/) {
121 $template_id .= $1;
122 }
123 }
124
125 # CHECK TO SEE IF THE TEMPLATE IS THERE
126 unless ($tmojo->template_exists($template_id)) {
127 return NOT_FOUND;
128 }
129
130 # OUTPUT THE APACHE HEADER
131 $apache_request->send_http_header('text/html; charset=utf8');
132
133 # CALL THE TMOJO TEMPLATE
134 if ($default_container ne '') {
135 print $tmojo->call_with_container($template_id, $default_container, %args);
136 } else {
137 print $tmojo->call($template_id, %args);
138 }
139
140 # AND WE'RE DONE
141 return OK;
142 }
143  
144 1;