File Coverage

lib/Apache/Config/Preproc/ifdefine.pm
Criterion Covered Total %
statement 55 66 83.3
branch 22 34 64.7
condition 1 6 16.6
subroutine 10 11 90.9
pod 1 6 16.6
total 89 123 72.3


line stmt bran cond sub pod time code
1             package Apache::Config::Preproc::ifdefine;
2 5     5   35 use parent 'Apache::Config::Preproc::Expand';
  5         9  
  5         30  
3 5     5   250 use strict;
  5         9  
  5         94  
4 5     5   23 use warnings;
  5         8  
  5         107  
5 5     5   22 use Carp;
  5         7  
  5         4049  
6              
7             our $VERSION = '1.03';
8              
9             sub new {
10 9     9 0 21 my ($class, $conf) = @_;
11 9         43 my $self = $class->SUPER::new($conf);
12 9         28 @{$self->{D}}{@_} = (1) x @_;
  9         56  
13 9         70 return $self;
14             }
15              
16             sub find_define {
17 0     0 0 0 my ($self,$elt,$id) = @_;
18              
19 0         0 while (my $p = $elt->parent) {
20 0 0       0 if (grep {
21 0         0 (split /\s+/, $_->value)[0] eq $id
22             } $p->directive('define')) {
23 0         0 return 1;
24             }
25             }
26 0         0 return;
27             }
28              
29             sub _changed_in_section {
30 5     5   9 my ($self, $section, $id, $before) = @_;
31 5         24 foreach my $d (reverse $section->select) {
32 12 100       364 if ($before) {
33 5 50       82 if ($d == $before) {
34 5         153 $before = undef;
35             }
36 5         11 next;
37             }
38            
39 7 50 0     15 if ($d->type eq 'directive') {
    0          
40 7 50 33     49 if ($d->name =~ m/^(un)?define$/i && $d->value eq $id) {
41 0 0       0 if ($1) {
42 0         0 $self->undefine($id);
43             } else {
44 0         0 $self->define($id);
45             }
46 0         0 return 1;
47             }
48             } elsif ($d->type eq 'section' && lc($d->name) eq 'virtualhost') {
49             # Need to descend into VirtualHost, because accorind to the
50             # Apache docs
51             # see (https://httpd.apache.org/docs/2.4/mod/core.html#define):
52             #
53             # While [the Define] directive is supported in virtual host
54             # context, the changes it makes are visible to any later
55             # configuration directives, beyond any enclosing virtual host.
56             #
57             # The same is said about the UnDefine directive.
58            
59 0 0       0 return 1 if $self->_changed_in_section($d, $id);
60             }
61             }
62 5         57 return 0;
63             }
64              
65             sub is_defined {
66 10     10 0 23 my ($self,$id,$d) = @_;
67              
68 10 100       26 unless ($self->{D}{$id}) {
69 5         16 my $before = $d;
70 5         23 while ($d = $d->parent) {
71 5 50       145 last if $self->_changed_in_section($d, $id, $before);
72 5         13 $before = undef;
73             }
74             }
75 10         45 return $self->{D}{$id};
76             }
77              
78             sub define {
79 3     3 0 22 my ($self,$id) = @_;
80 3         9 $self->{D}{$id} = 1;
81             }
82              
83             sub undefine {
84 1     1 0 5 my ($self,$id) = @_;
85 1         3 delete $self->{D}{$id};
86             }
87              
88             sub expand {
89 324     324 1 462 my ($self, $d, $repl) = @_;
90 324 100       860 if ($d->type eq 'section') {
91 41 100       159 if (lc($d->name) eq 'ifdefine') {
92 10         70 my $id = $d->value;
93 10         78 my $negate = $id =~ s/^!//;
94 10         23 my $res = $self->is_defined($id, $d);
95 10 100       27 $res = !$res if $negate;
96 10 100       19 if ($res) {
97 7         32 push @$repl, $d->select;
98             }
99 10         437 return 1;
100             }
101             }
102 314 100       1243 if ($d->type eq 'directive') {
103 283         959 my $name = lc($d->name);
104 283 100       1099 if ($name eq 'define') {
    100          
105 3         10 $self->define($d->value);
106 3         11 return 1;
107             } elsif ($name eq 'undefine') {
108 1         3 $self->undefine($d->value);
109 1         3 return 1;
110             }
111             }
112 310         694 return 0;
113             }
114              
115             1;
116             __END__