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   27 use parent 'Apache::Config::Preproc::Expand';
  5         5  
  5         22  
3 5     5   197 use strict;
  5         5  
  5         72  
4 5     5   16 use warnings;
  5         6  
  5         83  
5 5     5   16 use Carp;
  5         6  
  5         3260  
6              
7             our $VERSION = '1.03';
8              
9             sub new {
10 9     9 0 18 my ($class, $conf) = @_;
11 9         32 my $self = bless $class->SUPER::new($conf), $class;
12 9         20 @{$self->{D}}{@_} = (1) x @_;
  9         47  
13 9         61 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   17 my ($self, $section, $id, $before) = @_;
31 5         10 foreach my $d (reverse $section->select) {
32 12 100       307 if ($before) {
33 5 50       65 if ($d == $before) {
34 5         122 $before = undef;
35             }
36 5         10 next;
37             }
38            
39 7 50 0     12 if ($d->type eq 'directive') {
    0          
40 7 50 33     42 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         38 return 0;
63             }
64              
65             sub is_defined {
66 10     10 0 14 my ($self,$id,$d) = @_;
67              
68 10 100       24 unless ($self->{D}{$id}) {
69 5         6 my $before = $d;
70 5         16 while ($d = $d->parent) {
71 5 50       110 last if $self->_changed_in_section($d, $id, $before);
72 5         13 $before = undef;
73             }
74             }
75 10         27 return $self->{D}{$id};
76             }
77              
78             sub define {
79 3     3 0 17 my ($self,$id) = @_;
80 3         7 $self->{D}{$id} = 1;
81             }
82              
83             sub undefine {
84 1     1 0 5 my ($self,$id) = @_;
85 1         2 delete $self->{D}{$id};
86             }
87              
88             sub expand {
89 324     324 1 355 my ($self, $d, $repl) = @_;
90 324 100       421 if ($d->type eq 'section') {
91 41 100       134 if (lc($d->name) eq 'ifdefine') {
92 10         51 my $id = $d->value;
93 10         64 my $negate = $id =~ s/^!//;
94 10         20 my $res = $self->is_defined($id, $d);
95 10 100       19 $res = !$res if $negate;
96 10 100       16 if ($res) {
97 7         24 push @$repl, $d->select;
98             }
99 10         356 return 1;
100             }
101             }
102 314 100       966 if ($d->type eq 'directive') {
103 283         793 my $name = lc($d->name);
104 283 100       919 if ($name eq 'define') {
    100          
105 3         9 $self->define($d->value);
106 3         8 return 1;
107             } elsif ($name eq 'undefine') {
108 1         2 $self->undefine($d->value);
109 1         2 return 1;
110             }
111             }
112 310         509 return 0;
113             }
114              
115             1;
116             __END__