aboutsummaryrefslogtreecommitdiff
path: root/lib/App/Acmeman/Apache/Layout.pm
blob: 39e40d5d9f3b195866bf6008b45f4097f3344a5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package App::Acmeman::Apache::Layout;

use strict;
use warnings;
use Carp;
use File::Basename;
use feature 'state';
use App::Acmeman::Log qw(:all);

sub _find_httpd {
    my $httpd;
    foreach my $d (split /:/, $ENV{PATH}) {
	foreach my $n (glob "$d/apachectl $d/httpd") {
	    if (-x $n) {
		return $n;
	    }
	}
    }
}

sub modules {
    my $class = shift;
    my @path = split /::/, $class;
    
    state $loaders //=
	[map { $_->[1] }
	 sort { $a->[0] <=> $b->[0] }
	 map {
	     my ($modname) = $class . '::' . fileparse($_, '.pm');
	     eval {
		 no strict 'refs';
		 if (scalar %{ $modname.'::' }) {
		     die "INCLUDED $modname";
		 };
		 require $_;
		 my $prio = ${$modname.'::PRIORITY'} // 0;
		 [ $prio, $modname ]
	     };
	 }
	 map { glob File::Spec->catfile($_, '*.pm') }
	 grep { -d $_ }
	 map { File::Spec->catfile($_, @path) } @INC];
    @$loaders;
}

# new(NAME)
# new()
sub new {
    my $class = shift;
    
    if (@_ == 0) {
	# Autodetect	
	my $ap = new Apache::Defaults(server => $class->_find_httpd,
				      on_error => 'return');
	if ($ap->status) {
	    croak "unable to get Apache defaults: " . $ap->error;
	}
	
	foreach my $mod ($class->modules) {
	    my $obj;
	    eval {
		$obj = $mod->new($ap);
	    };
	    if ($obj) {
		return $obj;
	    }
#	    print "A $mod: $@\n"; 
	}

	if ($ap) {
	    return new App::Acmeman::Apache::Layout::auto($ap);
	}

	croak "unrecognized Apache layout";
    }

    my ($name, %args) = @_;
    if (ref($name) eq '') {
	my $mod = "$class::$name";
	my $obj;
	eval {
	    require $mod;
	};
	croak "undefined Apache layout $name" if ($@);
	return $mod->new;
    }

    unless ($name->isa('Apache::Defaults')) {
	croak "unrecognized argument";
    }

    my $self = bless { _defaults => $name }, $class;
    foreach my $kw (qw(layout_name incdir restart_command)) {
	if (defined(my $v = delete $args{$kw})) {
	    $self->{$kw} = $v;
	}
    }

    return $self;
}    

sub apache { shift->{_defaults} }

sub name {
    my $self = shift;
    unless ($self->{layout_name}) {
	$self->{layout_name} = ref($self);
	$self->{layout_name} =~ s/.*:://;
	$self->{layout_name} =~ s/\.pm$//;
    }
    return $self->{layout_name};
}

sub config_file { shift->apache->server_config }

sub incdir {
    my $self = shift;
    unless ($self->{incdir}) {
	$self->{incdir} = dirname($self->config_file);
    }
    return $self->{incdir};
}

sub server_command { join(' ', shift->apache->server_command) }

sub restart_command {
    my $self = shift;

    unless (exists($self->{restart_command})) {
	if ($self->server_command =~ m{/apachectl$}) {
	    $self->{restart_command} = $self->server_command . ' restart';
	} else {
	    error("no postrenew command defined", prefix => 'warning');
	    $self->{restart_command} = undef
	}
    }
    return $self->{restart_command};
}

sub post_setup {}

package App::Acmeman::Apache::Layout::auto;
use parent 'App::Acmeman::Apache::Layout';
use App::Acmeman::Log qw(:all);

sub new {
    my ($class, $ap) = @_;
    return $class->SUPER::new($ap);
}

1;

Return to:

Send suggestions and report system problems to the System administrator.