File Coverage

blib/lib/Test/Instance/Apache/Modules.pm
Criterion Covered Total %
statement 22 29 75.8
branch 0 2 0.0
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 31 40 77.5


line stmt bran cond sub pod time code
1             package Test::Instance::Apache::Modules;
2              
3 5     5   43663 use Moo;
  5         9418  
  5         21  
4 5     5   2073 use IO::All;
  5         6  
  5         35  
5              
6             =head1 NAME
7              
8             Test::Instance::Apache::Modules - Apache module management for T::I::A
9              
10             =head1 SYNOPSIS
11              
12             use FindBin qw/ $Bin /;
13             use Test::Instance::Apache::Modules;
14              
15             my $modules = Test::Instance::Apache::Modules->new(
16             server_root => "$Bin/conf",
17             modules => [ qw/ mpm_prefork authz_core mome / ],
18             );
19              
20             # get include paths for config
21             my $paths = $modules->include_modules;
22              
23             =head1 DESCRIPTION
24              
25             Test::Instance::Apache::Modules sets up the required modules for Apache
26             according to an array of module names. This functions similarly to C
27             which comes as part of the Apache distribution, however is much more simplified
28             to only do what is necessary for T::I::A.
29              
30             The module creates a C and C folder inside the
31             L directory, and then copies the contents of
32             C into the new C folder. Then,
33             symlinks are created across to the C folder, ready for Apache to
34             include from the L list.
35              
36             =head2 Attributes
37              
38             These are the available attributes on Test::Instance::Apache::Modules
39              
40             =head3 modules
41              
42             The arrayref of modules to symlink into C folder. This is
43             required. Note that any modules specified here will need to be installed on
44             your local machine, and you will have to specify ALL modules required - there
45             are no assumptions made for modules to include.
46              
47             =cut
48              
49             has modules => (
50             is => 'ro',
51             required => 1,
52             isa => sub { die "modules must be an array!\n" unless ref $_[0] eq 'ARRAY' },
53             );
54              
55             =head3 server_root
56              
57             The root directory of the server config. This directory is where
58             C and C directories will be created. This
59             attribute is required.
60              
61             =cut
62              
63             has server_root => (
64             is => 'ro',
65             required => 1,
66             );
67              
68             has _available_mods_folder => (
69             is => 'lazy',
70             builder => sub {
71 3     3   35860 my $self = shift;
72 3         13 return $self->make_server_dir( 'mods-available' );
73             },
74             );
75              
76             has _enabled_mods_folder => (
77             is => 'lazy',
78             builder => sub {
79 2     2   594 my $self = shift;
80 2         7 return $self->make_server_dir( 'mods-enabled' );
81             },
82             );
83              
84             =head3 include_modules
85              
86             This creates the include paths for the C and C files as required by
87             Apache.
88              
89             =cut
90              
91             has include_modules => (
92             is => 'lazy',
93             builder => sub {
94 2     2   601 my $self = shift;
95 2         3 my @include;
96 2         6 foreach ( qw/ load conf / ) {
97 4         7 push @include, 'Include';
98 4         43 push @include, sprintf( '%s/*.%s', $self->_enabled_mods_folder, $_ );
99             }
100 2         18 return \@include;
101             },
102             );
103              
104             =head2 Methods
105              
106             These are the methods available on Test::Instance::Apache::Modules.
107              
108             =head3 load_modules
109              
110             This function performs the main part of this module. This copies all the
111             current mods from C to the C
112             directory, and then symlinks all the required modules across to the
113             C folder.
114              
115             =cut
116              
117             sub load_modules {
118 3     3 1 240 my $self = shift;
119              
120 3         11 io->dir( '/etc/apache2/mods-available' )->copy( $self->_available_mods_folder );
121              
122 0         0 for my $module ( @{ $self->modules } ) {
  0         0  
123 0         0 for my $suffix ( qw/ conf load / ) {
124 0         0 my $source_filename = File::Spec->catfile(
125             $self->_available_mods_folder,
126             sprintf( '%s.%s', $module, $suffix )
127             );
128 0         0 my $target_filename = File::Spec->catfile(
129             $self->_enabled_mods_folder,
130             sprintf( '%s.%s', $module, $suffix )
131             );
132 0 0       0 if ( -f $source_filename ) {
133             # if the file does not exist, just ignore it as not all mods have config files
134 0         0 symlink( $source_filename, $target_filename );
135             }
136             }
137             }
138             }
139              
140             =head3 make_server_dir
141              
142             Utility function to create a new directory in the L.
143              
144             =cut
145              
146             sub make_server_dir {
147 5     5 1 10 my ( $self, @dirnames ) = @_;
148 5         63 my $dir = File::Spec->catdir( $self->server_root, @dirnames );
149 5         397 mkdir $dir;
150 5         38 return $dir;
151             }
152              
153             =head1 AUTHOR
154              
155             Tom Bloor Et.bloor@shadowcat.co.ukE
156              
157             =head1 COPYRIGHT
158              
159             Copyright 2016 Tom Bloor
160              
161             =head1 LICENCE
162              
163             This library is free software; you can redistribute it and/or modify
164             it under the same terms as Perl itself.
165              
166             =head1 SEE ALSO
167              
168             =over
169              
170             =item * L
171              
172             =back
173              
174             =cut
175              
176             1;