File Coverage

blib/lib/DTL/Fast/Tag/Ssi.pm
Criterion Covered Total %
statement 35 38 92.1
branch 6 10 60.0
condition 2 7 28.5
subroutine 7 7 100.0
pod 0 2 0.0
total 50 64 78.1


line stmt bran cond sub pod time code
1             package DTL::Fast::Tag::Ssi;
2 2     2   988 use strict; use utf8; use warnings FATAL => 'all';
  2     2   4  
  2     2   53  
  2         8  
  2         4  
  2         9  
  2         61  
  2         4  
  2         80  
3 2     2   8 use parent 'DTL::Fast::Tag::Simple';
  2         3  
  2         11  
4              
5             $DTL::Fast::TAG_HANDLERS{'ssi'} = __PACKAGE__;
6              
7 2     2   136 use DTL::Fast::Expression;
  2         5  
  2         841  
8              
9             #@Override
10             sub parse_parameters
11             {
12 2     2 0 1 my $self = shift;
13 2 50       17 if( $self->{'parameter'} =~ /^\s*(.+?)(?:\s*(parsed))?\s*$/ )
14             {
15 2         9 @{$self}{'template', 'parsed'} = (
  2         5  
16             DTL::Fast::Variable->new($1)
17             , $2
18             );
19 2         8 warn $self->get_parse_warning('`ssi` tag is now depricated and will be removed in future versions. Please, use `include` tag');
20             }
21             else
22             {
23 0         0 die $self->get_parse_error("can't parse parameter: $self->{'parameter'}");
24             }
25            
26 2         7 return $self;
27             }
28              
29             #@Override
30             # @todo: recursion protection
31             sub render
32             {
33 2     2 0 2 my ($self, $context, $result) = @_;
34            
35 2         1 my $ssi_dirs = $context->{'ns'}->[-1]->{'_dtl_ssi_dirs'};
36            
37 2 50 33     13 if(
      50        
38             defined $ssi_dirs
39             and ref $ssi_dirs eq 'ARRAY'
40             and scalar @$ssi_dirs
41             )
42             {
43 2         6 my $template_path = $self->{'template'}->render($context);
44            
45 2         3 my $allowed = 0;
46            
47 2         2 foreach my $allowed_dir (@$ssi_dirs)
48             {
49 2 50       21 if( $template_path =~ /^\Q$allowed_dir\E/s )
50             {
51 2         2 $allowed = 1;
52 2         3 last;
53             }
54             }
55              
56 2 50       3 if( $allowed )
57             {
58 2         5 $result = DTL::Fast::__read_file($template_path);
59            
60 2 100       5 if( $self->{'parsed'} )
61             {
62 1         5 $result = DTL::Fast::Template->new($result)->render($context);
63             }
64             }
65             else
66             {
67 0   0     0 die $self-get_render_error(
68             $context,
69             sprintf(
70             "File %s is not in one of ssi_dirs:\n\t%s"
71             , $template_path // 'undef'
72             , join( "\n\t", @$ssi_dirs )
73             )
74             );
75             }
76             }
77             else
78             {
79 0         0 die $self->get_render_error($context, 'in order to use ssi tag, you must provide ssi_dirs argument to the constructor');
80             }
81            
82 2         34 return $result;
83             }
84              
85             1;