blib/lib/MojoX/Renderer/CTPP2.pm

filter: Statementstmt 90.4% 19 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.8% 26 / 28 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package MojoX::Renderer::CTPP2;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use base 'Mojo::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Path qw/make_path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 0 ▶use HTML::CTPP2 ();
Subroutine BEGIN: called CC=1 SCAR=2.6
13  
14 our $VERSION = '0.02';
15  
16 __PACKAGE__->attr('ctpp2');
17  
18 my %ctpp2_allow_params = map { $_, 1 }
19 qw/arg_stack_size code_stack_size steps_limit max_functions source_charset destination_charset/;
20  
21 sub build {
22 my $self = shift->SUPER::new(@_);
23  
24 $self->_init_ctpp2(@_);
25  
26 return sub { $self->_render(@_) };
27 }
28  
29 sub _init_ctpp2 {
30 my $self = shift;
31 my %args = @_;
32  
33 my $mojo = $args{mojo};
34 my %config = %{delete $args{template_options} || {}};
35  
36 $self->{COMPILE_EXT} ||= '.ctpp2c';
37  
38 $self->{CACHE_ENABLE} = 1 if not exists $self->{CACHE_ENABLE};
39  
40 unless (exists $self->{COMPILE_DIR}) {
41 $self->{COMPILE_DIR} = $mojo ? $mojo->home->rel_dir('tmp/ctpp2') : File::Spec->tmpdir;
42 }
43  
44 for (keys %config) {
45 exists $ctpp2_allow_params{$_} or delete $config{$_};
46 }
47  
48 $self->ctpp2(HTML::CTPP2->new(%config)) or Carp::croak 'Could not initialize CTPP2 object';
49  
50 if (!(ref $args{INCLUDE_PATH} eq 'ARRAY')) {
51 my @include_path = _coerce_paths($args{INCLUDE_PATH}, $args{DELIMITER});
52 if (!@include_path) {
53 @include_path = $mojo ? $mojo->home->rel_dir('templates') : ();
54 }
55 $args{INCLUDE_PATH} = \@include_path;
56 }
57  
58 $self->ctpp2->include_dirs(\@{$args{INCLUDE_PATH}});
59  
60 return $self;
61 }
62  
63 sub _coerce_paths {
64 my ($paths, $dlim) = @_;
65  
66 return () if (!$paths);
67 return @{$paths} if (ref $paths eq 'ARRAY');
68  
69 $dlim = ($^O eq 'MSWin32') ? ':(?!\\/)' : ':' unless (defined $dlim);
70  
71 return split(/$dlim/, $paths);
72 }
73  
74 sub _render {
75 my ($self, $renderer, $c, $output, $options) = @_;
76  
77 my $ctpp2 = $self->ctpp2;
78 my $template_path = $c->stash->{'template_path'} || $renderer->template_path($options);
79 my $bytecode = $self->_get_bytecode($renderer->root, $template_path);
80  
81 $ctpp2->param({%{$c->stash}, base => $c->tx->req->url->base->to_string});
82  
83 unless ($$output = $ctpp2->output($bytecode)) {
84 Carp::carp 'Template error: ['
85 . $ctpp2->get_last_error->{'template_name'} . '] - '
86 . $ctpp2->get_last_error->{'error_str'};
87 return 0;
88 }
89 else {
90 return 1;
91 }
92 }
93  
94 sub _get_bytecode {
95 my ($self, $templates_rootdir, $template) = @_;
96  
97 my $ctpp2 = $self->ctpp2;
98  
99 if ($self->{CACHE_ENABLE}) {
100 my $mojo = $self->{mojo};
101 my $compile_rootdir = $self->{COMPILE_DIR};
102  
103 my $template_relpath = File::Spec->abs2rel($template, $templates_rootdir);
104 $template_relpath =~ s{\.[^\.]+$}{};
105 $template_relpath .= $self->{COMPILE_EXT};
106  
107 my $ctemplate = File::Spec->catfile($compile_rootdir, $template_relpath);
108  
109 my $bytecode;
110 if (-e $ctemplate) {
111 if ((stat($ctemplate))[9] < (stat($template))[9]) {
112 $bytecode = $ctpp2->parse_template($template);
113 $bytecode->save($ctemplate) if $bytecode;
114 }
115 else {
116 return $ctpp2->load_bytecode($ctemplate);
117 }
118 }
119 else {
120 $bytecode = $ctpp2->parse_template($template);
121  
122 my $save_path = $ctemplate;
123 $save_path =~ s{/[^/]+$}{};
124  
125 make_path($save_path) if !-d $save_path;
126  
127 $bytecode->save($ctemplate) if $bytecode;
128 }
129 return $bytecode;
130 }
131 else {
132 $ctpp2->parse_template($template);
133 }
134 }
135  
136 1;
137  
138 __END__
↑ Summary