OpenInteract/Template/Process.pm untested

filter: Statementstmt 0.0% 0 / 123 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 50 MC/DCmcdc 0.0% 0 / 32 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 251 CC 17 SCAR 39.5
CC
17
Cov
0%
handler
110.0
initialize
42.0
_package_template_config
12.0
CRAP 52.0
1 package OpenInteract::Template::Process;
2  
3 # $Id: Process.pm,v 1.24 2002/09/16 20:18:36 lachoy Exp $
4  
5 use strict;
6 use Data::Dumper qw( Dumper );
7 use OpenInteract::Template::Context;
8 use OpenInteract::Template::Plugin;
9 use OpenInteract::Template::Provider;
10 use Template;
11  
12 $OpenInteract::Template::Process::VERSION = sprintf("%d.%02d", q$Revision: 1.24 $ =~ /(\d+)\.(\d+)/);
13  
14 use constant DEFAULT_COMPILE_EXT => '.ttc';
15 use constant DEFAULT_CACHE_SIZE => 75;
16  
17 # Since each website gets its own template object, when we call
18 # initialize() all the website's information has been read in and
19 # setup so we should be able to ask the config object what plugin
20 # objects are defined, etc.
21  
22 sub initialize {
23 my ( $class ) = @_;
24 my $R = OpenInteract::Request->instance;
25 $R->DEBUG && $R->scrib( 1, "Starting template initialization process" );
26 my $CONFIG = $R->CONFIG;
27 my $oi_tt_config = $CONFIG->{template_info};
28  
29 $Template::Config::CONTEXT = 'OpenInteract::Template::Context';
30  
31 # Default configuration -- this can be modified by each site
32  
33 my $cache_size = ( defined $oi_tt_config->{cache_size} )
34 ? $oi_tt_config->{cache_size} : DEFAULT_CACHE_SIZE;
35 my $compile_ext = $oi_tt_config->{compile_ext} || DEFAULT_COMPILE_EXT;
36 my $compile_dir = $CONFIG->get_dir( 'cache_tt' );
37  
38 # If the compile_dir isn't specified, be sure to set it **and**
39 # the extension to undef, otherwise TT will try to compile/save
40 # the templates into the directory we find them (maybe: the custom
41 # provider might override, but whatever)
42  
43 unless ( defined $compile_dir ) {
44 $compile_ext = undef;
45 $compile_dir = undef;
46 }
47  
48 my %tt_config = ( PLUGINS => { OI => 'OpenInteract::Template::Plugin' },
49 CACHE_SIZE => $cache_size,
50 COMPILE_DIR => $compile_dir,
51 COMPILE_EXT => $compile_ext );
52  
53 # Install various template configuration items (currently plugins
54 # and blocks) as specified by packages
55  
56 $class->_package_template_config( \%tt_config );
57  
58 # Create the configuration for this TT object and give the user a
59 # chance to add to or modify it
60  
61 if ( my $custom_init_class = $CONFIG->{template_info}{custom_init_class} ) {
62 eval "require $custom_init_class";
63 if ( $@ ) {
64 $R->scrib( 0, "Custom initialize class ($custom_init_class) is not",
65 "available for me to use: ($@). I'm going to keep going." );
66 }
67 else {
68 my $method = $CONFIG->{template_info}{custom_init_method} || 'handler';
69 $R->DEBUG && $R->scrib( 1, "Running custom template init handler: ",
70 "$custom_init_class\->$method" );
71 eval { $custom_init_class->$method( \%tt_config ) };
72 if ( $@ ) {
73 $R->scrib( 0, "Custom template init handler ($custom_init_class\->$method)",
74 "failed. ($@) I'm going to keep running initialize()." );
75 }
76 else {
77 $R->DEBUG && $R->scrib( 1, "Ran custom template init handler ok" );
78 }
79 }
80 }
81  
82 # Put the configured OI provider in the mix. Note that we do this
83 # AFTER the customization process so the user can set cache size,
84 # compile directory, etc.
85  
86 my $oi_provider = OpenInteract::Template::Provider->new(
87 CACHE_SIZE => $tt_config{CACHE_SIZE},
88 COMPILE_DIR => $tt_config{COMPILE_DIR},
89 COMPILE_EXT => $tt_config{COMPILE_EXT} );
90 unshift @{ $tt_config{LOAD_TEMPLATES} }, $oi_provider;
91  
92 $R->DEBUG && $R->scrib( 3, 'Configuration before sent to Template->new():',
93 Dumper( \%tt_config ) );
94 my $template = Template->new( %tt_config ) || die Template->error();
95 return $template;
96 }
97  
98  
99 # Display an OI document
100  
101 sub handler {
102 my ( $class, $template_config, $template_vars, $template_source ) = @_;
103 my $R = OpenInteract::Request->instance;
104  
105 my ( $name, $to_process );
106 if ( $template_source->{text} ) {
107 $to_process = ( ref $template_source->{text} eq 'SCALAR' )
108 ? $template_source->{text} : \$template_source->{text};
109 $name = '_anonymous_';
110 $R->DEBUG && $R->scrib( 1, "Using raw template source (",
111 ref $template_source->{text}, ") for processing" );
112 }
113 elsif ( $template_source->{object} ) {
114 $to_process = \$template_source->{object}{contents};
115 $name = $template_source->{object}->create_name;
116 push @{ $R->{templates_used} }, $name;
117 $R->DEBUG && $R->scrib( 1, "Using template object [$name] for processing" );
118 }
119  
120 # Using 'db' will be deprecated soon...
121  
122 elsif ( $template_source->{db} ) {
123 unless ( $template_source->{package} ) {
124 die "Must give 'package' along with 'db' when processing " .
125 "template. (Given: $template_source->{db})\n";
126 }
127 $name = join( '::', $template_source->{package}, $template_source->{db} );
128 $to_process = $name;
129 push @{ $R->{templates_used} }, $name;
130 $R->DEBUG && $R->scrib( 1, "Using template name [$name] for processing" );
131 }
132  
133 elsif ( $template_source->{name} ) {
134 $name = $template_source->{name};
135 $to_process = $name;
136 push @{ $R->{templates_used} }, $name;
137 $R->DEBUG && $R->scrib( 1, "Using template name [$name] for processing" );
138 }
139  
140 # Uh oh...
141  
142 else {
143 $R->scrib( 0, "No template to process! Information given for source:\n",
144 Dumper( $template_source ) );
145 die "No template to process!\n";
146 }
147 # Grab the template object and the OI plugin, making the OI plugin
148 # available to every template
149  
150 my $template = $R->template_object;
151 $template_vars->{OI} = $template->context->plugin( 'OI' );
152  
153 # Allow websites to modify the $template_vars passed to every page
154  
155 if ( my $custom_variable_class = $R->CONFIG->{template_info}{custom_variable_class} ) {
156 eval "require $custom_variable_class";
157 if ( $@ ) {
158 $R->scrib( 0, "Custom variable class ($custom_variable_class) is not",
159 "available for me to use: ($@). I'm going to keep going." );
160 }
161 else {
162 my $method = $R->CONFIG->{template_info}{custom_variable_method} || 'handler';
163 $R->DEBUG && $R->scrib( 1, "Running custom template variable handler: ",
164 "$custom_variable_class\->$method" );
165 eval { $custom_variable_class->$method( $name, $template_vars ) };
166 if ( $@ ) {
167 $R->scrib( 0, "Custom template handler ($custom_variable_class\-\>$method) died ",
168 "with ($@). I'm going to keep processing." );
169 }
170 else {
171 $R->DEBUG && $R->scrib( 1, "Ran custom template init handler ok" );
172 }
173 }
174 }
175  
176 my ( $html );
177 $template->process( $to_process, $template_vars, \$html )
178 || die "Cannot process template!", $template->error();
179 return $html;
180 }
181  
182  
183 sub _package_template_config {
184 my ( $class, $config ) = @_;
185 my $R = OpenInteract::Request->instance;
186  
187 # Find all the packages in this website
188  
189 my $website_dir = $R->CONFIG->{dir}{base};
190 my $pkg_list = $R->repository->fetch( undef, { directory => $website_dir } )
191 ->fetch_all_packages;
192 $R->DEBUG && $R->scrib( 1, "Packages for inspection by template initialization read ok" );
193  
194 # For each package in the site, read in custom blocks and template
195 # plugins if they are available
196  
197 foreach my $pkg ( @{ $pkg_list } ) {
198 if ( ref $pkg->{template_block} eq 'HASH' ) {
199 foreach my $block_class ( keys %{ $pkg->{template_block} } ) {
200 my $block_method = $pkg->{template_block}{ $block_class };
201 my $item_blocks = eval { $block_class->$block_method() };
202 foreach my $block_name ( keys %{ $item_blocks } ) {
203 $R->DEBUG && $R->scrib( 1, "Found template block ($block_name) in",
204 "$block_class\->$block_method" );
205 $config->{BLOCKS}{ $block_name } = $item_blocks->{ $block_name };
206 }
207 }
208 }
209 if ( ref $pkg->{template_plugin} eq 'HASH' ) {
210 foreach my $plugin_tag ( keys %{ $pkg->{template_plugin} } ) {
211 $R->DEBUG && $R->scrib( 1, "Found template plugin $plugin_tag",
212 "=> $pkg->{template_plugin}{ $plugin_tag }" );
213 $config->{PLUGINS}{ $plugin_tag } = $pkg->{template_plugin}{ $plugin_tag };
214 }
215 }
216 }
217 }
218  
219  
220 1;
221  
222 __END__