OpenInteract2/ContentGenerator/TT2Process.pm

filter: Statementstmt 93.3% 28 / 30 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 95.0% 38 / 40 CC 1 SCAR 0.3
CC
1
Cov
93%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« OpenInteract2/Brick/Widgets.pm ↑ Summary OpenInteract2/ResultsManage.pm »
1 package OpenInteract2::ContentGenerator::TT2Process;
2  
3 # $Id: TT2Process.pm,v 1.21 2005/03/18 04:09:50 lachoy Exp $
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use base qw( OpenInteract2::ContentGenerator );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Data::Dumper qw( Dumper );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Log::Log4perl qw( get_logger );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use OpenInteract2::Constants qw( :log );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use OpenInteract2::ContentGenerator::TemplateSource;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use OpenInteract2::Context qw( CTX );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use OpenInteract2::Exception qw( oi_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use OpenInteract2::TT2::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 0 ▶use OpenInteract2::TT2::Plugin;
Subroutine BEGIN: called CC=1 SCAR=2.6
16 use OpenInteract2::TT2::Provider;
17 use Template;
18  
19 $OpenInteract2::ContentGenerator::TT2Process::VERSION = sprintf("%d.%02d", q$Revision: 1.21 $ =~ /(\d+)\.(\d+)/);
20  
21 my ( $log );
22  
23 use constant DEFAULT_COMPILE_EXT => '.ttc';
24 use constant DEFAULT_CACHE_SIZE => 75;
25  
26 sub template { return $_[0]->{_template} }
27  
28 ########################################
29 # GENERATE CONTENT
30  
31 sub generate {
32 my ( $self, $template_config, $template_vars, $template_source ) = @_;
33 $log ||= get_logger( LOG_TEMPLATE );
34  
35 my ( $source_type, $source ) =
36 OpenInteract2::ContentGenerator::TemplateSource->identify( $template_source );
37  
38 my ( $template_name );
39  
40 # TODO: We're losing information (name) if an object is passed in,
41 # but that might be ok because (a) it's not done often (ever?) and
42 # (b) it's only necessary for the custom_variable_class
43  
44 if ( $source_type eq 'NAME' ) {
45 $template_name = $source;
46 my ( $text, $filename, $modtime ) =
47 OpenInteract2::ContentGenerator::TemplateSource->load_source( $template_name );
48 $source = \$text;
49 }
50 else {
51 $template_name = '_anonymous_';
52 }
53  
54 $log->is_debug &&
55 $log->debug( "Processing template '$template_name'" );
56  
57 my $template = $self->template;
58  
59 # Make all declared plugins available to every template
60  
61 foreach my $plugin_name ( keys %{ $self->{_plugin_class} } ) {
62 unless ( $self->{_plugin}{ $plugin_name } ) {
63 $self->{_plugin}{ $plugin_name } =
64 $template->context->plugin( $plugin_name );
65 }
66 $template_vars->{ $plugin_name } = $self->{_plugin}{ $plugin_name };
67 $log->is_debug &&
68 $log->debug( "Adding plugin '$plugin_name' to template vars" );
69 }
70  
71 # Create a 'MSG' method and add the Locale::Maketext object
72 # available:
73  
74 my $lh = CTX->request->language_handle;
75 $template_vars->{MSG} = sub {
76 return ( $lh ) ? $lh->maketext( @_ ) : "$_[0]: no language handle";
77 };
78 $template_vars->{LH} = $lh;
79  
80 $self->_customize_template_variables( $template_name, $template_vars );
81  
82 my ( $html );
83 eval {
84 $template->process( $source, $template_vars, \$html )
85 || die "Cannot process template '$template_name': ",
86 $template->error(), "\n";
87 };
88 if ( $@ ) {
89 $log->error( "Failed to process template '$template_name': $@" );
90 return $@;
91 }
92 $log->is_info &&
93 $log->info( "Processed template '$template_name' ok" );
94 return $html;
95 }
96  
97 sub _customize_template_variables {
98 my ( $self, $template_name, $template_vars ) = @_;
99 return unless ( $self->{_customize_variables} );
100 $log ||= get_logger( LOG_TEMPLATE );
101  
102 $log->is_debug &&
103 $log->debug( "Running custom template variable handler" );
104 my $class = $self->{_customize_variables};
105 eval {
106 $class->customize_template_vars( $template_name, $template_vars )
107 };
108 if ( $@ ) {
109 $log->error( "Custom template handler '$class' died; I'm going ",
110 "to keep processing but clear out the handler for ",
111 "later calls. Error: $@" );
112 $self->{_customize_variables} = undef;
113 }
114 else {
115 $log->is_debug &&
116 $log->debug( "Ran custom template variable handler ok" );
117 }
118 }
119  
120 ########################################
121 # INITIALIZATION
122  
123 # Since each website gets its own template object, when we call
124 # initialize() all the website's information has been read in and
125 # setup so we should be able to ask the config object what plugin
126 # objects are defined, etc.
127  
128 sub initialize {
129 my ( $self, $init_params ) = @_;
130 $log ||= get_logger( LOG_TEMPLATE );
131 $log->is_debug &&
132 $log->debug( "Starting TT2 template object init" );
133  
134 # Set the TT2 Context to an amenable value -- this just allows us
135 # to use the 'pkg::template' INCLUDE syntax
136  
137 $Template::Config::CONTEXT = 'OpenInteract2::TT2::Context';
138  
139 # This will be the configuration passed to the TT2 object; later
140 # actions modify it in-place
141  
142 my $tt_config = $self->_init_tt_config( $init_params );
143  
144 # Install various template configuration items (currently plugins)
145 # as specified by packages
146  
147 $self->_package_template_config( $tt_config );
148  
149 # Allow initial configuration customizations
150  
151 $self->_custom_init_tt_config( $init_params, $tt_config );
152  
153 # Initialize per-template-process code for changing template
154 # variables
155  
156 $self->_setup_variable_customizer( $init_params );
157  
158 # Put the configured OI provider in the mix. Note that we do this
159 # AFTER the customization process so the user can fool around with
160 # cache size, compile directory, etc.
161  
162 my $oi_provider = OpenInteract2::TT2::Provider->new(
163 CACHE_SIZE => $tt_config->{CACHE_SIZE},
164 COMPILE_DIR => $tt_config->{COMPILE_DIR},
165 COMPILE_EXT => $tt_config->{COMPILE_EXT}, );
166 unshift @{ $tt_config->{LOAD_TEMPLATES} }, $oi_provider;
167  
168 $log->is_debug &&
169 $log->debug( "Passing the following to the TT2 new() call: ",
170 Dumper( $tt_config ) );
171 my $template = Template->new( %{ $tt_config } );
172 unless ( $template ) {
173 oi_error "Template object not created: ", Template->error();
174 }
175  
176 $self->{_template} = $template;
177 $log->is_info &&
178 $log->info( "Template Toolkit object created properly" );
179 }
180  
181  
182 sub _init_tt_config {
183 my ( $self, $init_params ) = @_;
184  
185 # Default configuration -- this can be modified by each site
186  
187 my $cache_size = ( defined $init_params->{cache_size} )
188 ? $init_params->{cache_size}
189 : DEFAULT_CACHE_SIZE;
190 my $compile_ext = $init_params->{compile_ext} || DEFAULT_COMPILE_EXT;
191  
192 # Since we've moved the cache configuration give a default
193  
194 my $base_compile_dir = $init_params->{compile_dir} || 'cache/tt';
195 my $compile_dir = File::Spec->catdir(
196 CTX->lookup_directory( 'website' ), $base_compile_dir
197 );
198  
199 # If the compile_dir isn't specified, be sure to set it **and**
200 # the extension to undef, otherwise TT will try to compile/save
201 # the templates into the directory we find them (maybe: the custom
202 # provider might override, but whatever)
203  
204 unless ( defined $compile_dir ) {
205 $compile_ext = undef;
206 $compile_dir = undef;
207 }
208  
209 $self->{_plugin_class}{OI} = 'OpenInteract2::TT2::Plugin';
210 return {
211 PLUGINS => { OI => 'OpenInteract2::TT2::Plugin' },
212 CACHE_SIZE => $cache_size,
213 COMPILE_DIR => $compile_dir,
214 COMPILE_EXT => $compile_ext
215 };
216 }
217  
218  
219 sub _package_template_config {
220 my ( $self, $tt_config ) = @_;
221 $log ||= get_logger( LOG_TEMPLATE );
222  
223 $log->is_debug &&
224 $log->debug( "Scanning all installed packages for template plugins" );
225  
226 my $pkg_list = CTX->packages;
227  
228 # For each package in the site...
229  
230 foreach my $package ( @{ $pkg_list } ) {
231 my $pkg_name = $package->name;
232 my $plugins = $package->config->template_plugin;
233 unless ( ref( $plugins ) eq 'HASH' ) {
234 $log->is_debug &&
235 $log->debug( "Package $pkg_name has no template ",
236 "plugins; skipping" );
237 next;
238 }
239  
240 # ... read in the template plugins and if found assign to the
241 # \%tt_config that eventually gets passed to Template->new()
242  
243 while ( my ( $plugin_tag, $plugin_class ) = each %{ $plugins } ) {
244 my $full_name = "$plugin_tag > $plugin_class";
245 $log->is_debug &&
246 $log->debug( "Evaluating template plugin '$full_name' ",
247 "from package '$pkg_name'" );
248 eval "require $plugin_class";
249 if ( $@ ) {
250 $log->error( "Plugin '$full_name' from package ",
251 "'$pkg_name' failed: $@" );
252 }
253 else {
254 $tt_config->{PLUGINS}{ $plugin_tag } = $plugin_class;
255 $self->{_plugin_class}{ $plugin_tag } = $plugin_class;
256 }
257 }
258 }
259 }
260  
261  
262 sub _custom_init_tt_config {
263 my ( $self, $init_params, $tt_config ) = @_;
264 $log ||= get_logger( LOG_TEMPLATE );
265  
266 my $init_class = $init_params->{custom_init_class};
267 return unless ( $init_class );
268  
269 eval "require $init_class";
270 if ( $@ ) {
271 $log->error( "Custom init class '$init_class' not available; ",
272 "continuing... Error: $@" );
273 }
274 else {
275 $log->is_debug &&
276 $log->debug( "Running custom template init for '$init_class'" );
277 eval {
278 $init_class->custom_template_initialize( $tt_config, $init_params )
279 };
280 if ( $@ ) {
281 $log->error( "Failed custom template init '$init_class'; ",
282 "continuing... Error: $@" );
283 }
284 else {
285 $log->is_debug &&
286 $log->debug( "Custom template init ok" );
287 }
288 }
289 }
290  
291 # Allow websites to modify the template variables passed to every page
292 # -- initialize the class here
293  
294 sub _setup_variable_customizer {
295 my ( $self, $init_params ) = @_;
296 $log ||= get_logger( LOG_TEMPLATE );
297 my $custom_variable_class = $init_params->{custom_variable_class};
298 return unless ( $custom_variable_class );
299 $log->is_debug &&
300 $log->debug( "Reading TT2 variable customizer: '$custom_variable_class'" );
301 eval "require $custom_variable_class";
302 if ( $@ ) {
303 $log->error( "Custom variable class '$custom_variable_class' ",
304 "not available. Error: $@" );
305 }
306 else {
307 $self->{_customize_variables} = $custom_variable_class;
308 }
309 }
310  
311 1;
312  
313 __END__
« OpenInteract2/Brick/Widgets.pm ↑ Summary OpenInteract2/ResultsManage.pm »