| 1 | |||||||||||
| 2 | package Clio::Config; | ||||||||||
| 3 | 2 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | $Clio::Config::AUTHORITY = 'cpan:AJGB'; | |||||||||
| 5 | } | ||||||||||
| 6 | { | ||||||||||
| 7 | 2 | $Clio::Config::VERSION = '0.02'; | |||||||||
| 8 | } | ||||||||||
| 9 | # ABSTRACT: Config loader | ||||||||||
| 10 | |||||||||||
| 11 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 2 | ▶ | use Config::Any; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 2 | ▶ | use Config::General; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 2 | ▶ | use Carp qw( croak ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 2 | ▶ | use Class::Load (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 2 | ▶ | use Role::Tiny (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | |||||||||||
| 22 | |||||||||||
| 23 | 2 | has 'config_file' => ( | |||||||||
| 24 | is => 'ro', | ||||||||||
| 25 | required => 1, | ||||||||||
| 26 | ); | ||||||||||
| 27 | |||||||||||
| 28 | 2 | has '_config' => ( | |||||||||
| 29 | is => 'rw', | ||||||||||
| 30 | init_arg => undef, | ||||||||||
| 31 | ); | ||||||||||
| 32 | |||||||||||
| 33 | |||||||||||
| 34 | 2 | has 'run_as_user_group' => ( | |||||||||
| 35 | is => 'rw', | ||||||||||
| 36 | init_arg => undef, | ||||||||||
| 37 | ); | ||||||||||
| 38 | |||||||||||
| 39 | |||||||||||
| 40 | 2 | has 'pid_file' => ( | |||||||||
| 41 | is => 'rw', | ||||||||||
| 42 | init_arg => undef, | ||||||||||
| 43 | ); | ||||||||||
| 44 | |||||||||||
| 45 | |||||||||||
| 46 | 2 | has 'server_class' => ( | |||||||||
| 47 | is => 'rw', | ||||||||||
| 48 | init_arg => undef, | ||||||||||
| 49 | ); | ||||||||||
| 50 | |||||||||||
| 51 | |||||||||||
| 52 | 2 | has 'logger_class' => ( | |||||||||
| 53 | is => 'rw', | ||||||||||
| 54 | init_arg => undef, | ||||||||||
| 55 | ); | ||||||||||
| 56 | |||||||||||
| 57 | |||||||||||
| 58 | |||||||||||
| 59 | 2 | has 'server_client_class' => ( | |||||||||
| 60 | is => 'rw', | ||||||||||
| 61 | init_arg => undef, | ||||||||||
| 62 | ); | ||||||||||
| 63 | |||||||||||
| 64 | |||||||||||
| 65 | 2 | has 'server_host_port' => ( | |||||||||
| 66 | is => 'rw', | ||||||||||
| 67 | init_arg => undef, | ||||||||||
| 68 | ); | ||||||||||
| 69 | |||||||||||
| 70 | |||||||||||
| 71 | |||||||||||
| 72 | 2 | has 'ServerConfig' => ( | |||||||||
| 73 | is => 'rw', | ||||||||||
| 74 | init_arg => undef, | ||||||||||
| 75 | ); | ||||||||||
| 76 | |||||||||||
| 77 | |||||||||||
| 78 | 2 | has 'CommandConfig' => ( | |||||||||
| 79 | is => 'rw', | ||||||||||
| 80 | init_arg => undef, | ||||||||||
| 81 | ); | ||||||||||
| 82 | |||||||||||
| 83 | |||||||||||
| 84 | 2 | has 'ServerClientConfig' => ( | |||||||||
| 85 | is => 'rw', | ||||||||||
| 86 | init_arg => undef, | ||||||||||
| 87 | ); | ||||||||||
| 88 | |||||||||||
| 89 | |||||||||||
| 90 | 2 | has 'LogConfig' => ( | |||||||||
| 91 | is => 'rw', | ||||||||||
| 92 | init_arg => undef, | ||||||||||
| 93 | ); | ||||||||||
| 94 | |||||||||||
| 95 | 0 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 96 | 0 | my $self = shift; | |||||||||
| 97 | |||||||||||
| 98 | 0 | eval { | |||||||||
| 99 | 0 | my $conf = Config::Any->load_files( | |||||||||
| 100 | { | ||||||||||
| 101 | files => [ $self->config_file ], | ||||||||||
| 102 | use_ext => 1, | ||||||||||
| 103 | flatten_to_hash => 1, | ||||||||||
| 104 | driver_args => { | ||||||||||
| 105 | General => { | ||||||||||
| 106 | -UseApacheInclude => 1, | ||||||||||
| 107 | -IncludeRelative => 1, | ||||||||||
| 108 | -IncludeDirectories => 1, | ||||||||||
| 109 | -IncludeGlob => 1, | ||||||||||
| 110 | }, | ||||||||||
| 111 | }, | ||||||||||
| 112 | }, | ||||||||||
| 113 | ); | ||||||||||
| 114 | |||||||||||
| 115 | 0 | $self->_config( $conf->{ $self->config_file } ); | |||||||||
| 116 | }; | ||||||||||
| 117 | 0 | ▶ | if (my $e = $@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | 0 | $e =~ s/ at .+? line \d+\.//; | |||||||||
| 119 | 0 | die "$e"; | |||||||||
| 120 | } | ||||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | 0 | ▶ | sub _validate { | ||||||||
|
Subroutine
_validate: not called CC=8 SCAR=42.8
| |||||||||||
| 124 | 0 | my $self = shift; | |||||||||
| 125 | |||||||||||
| 126 | 0 | my $cfile = $self->config_file; | |||||||||
| 127 | |||||||||||
| 128 | die "<Command> configuration required in $cfile\n" | ||||||||||
| 129 | 0 | ▶ | unless exists $self->_config->{Command}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | |||||||||||
| 131 | die "<Server> configuration required in $cfile\n" | ||||||||||
| 132 | 0 | ▶ | unless exists $self->_config->{Server}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | |||||||||||
| 134 | die "<Server><Client> configuration required in $cfile\n" | ||||||||||
| 135 | 0 | ▶ | unless exists $self->_config->{Server}->{Client}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | |||||||||||
| 137 | die "Multiple <Server> is not supported in $cfile\n" | ||||||||||
| 138 | 0 | ▶ | unless ref $self->_config->{Server} eq 'HASH'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 139 | |||||||||||
| 140 | die "Multiple <Command> is not supported in $cfile\n" | ||||||||||
| 141 | 0 | ▶ | unless ref $self->_config->{Command} eq 'HASH'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 142 | |||||||||||
| 143 | die "Multiple <Server><Client> is not supported in $cfile\n" | ||||||||||
| 144 | 0 | ▶ | unless ref $self->_config->{Server}->{Client} eq 'HASH'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 145 | |||||||||||
| 146 | die "<Command>Exec is required in $cfile\n" | ||||||||||
| 147 | 0 | ▶ | unless defined $self->_config->{Command}->{Exec}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 148 | } | ||||||||||
| 149 | |||||||||||
| 150 | |||||||||||
| 151 | 0 | ▶ | sub process { | ||||||||
|
Subroutine
process: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 152 | 0 | my $self = shift; | |||||||||
| 153 | |||||||||||
| 154 | 0 | $self->_validate(); | |||||||||
| 155 | |||||||||||
| 156 | 0 | $self->_process_daemon(); | |||||||||
| 157 | 0 | $self->_process_command(); | |||||||||
| 158 | 0 | $self->_process_server(); | |||||||||
| 159 | 0 | $self->_process_log(); | |||||||||
| 160 | } | ||||||||||
| 161 | |||||||||||
| 162 | 0 | ▶ | sub _class2package { | ||||||||
|
Subroutine
_class2package: not called CC=2 SCAR=17.9
| |||||||||||
| 163 | 0 | my ($self, $prefix, $class) = @_; | |||||||||
| 164 | |||||||||||
| 165 | 0 | ▶ | if ( $class =~ /^\+(.*)$/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 166 | 0 | return "$1"; | |||||||||
| 167 | } | ||||||||||
| 168 | |||||||||||
| 169 | 0 | return "$prefix\::$class"; | |||||||||
| 170 | } | ||||||||||
| 171 | |||||||||||
| 172 | 0 | ▶ | sub _load_class { | ||||||||
|
Subroutine
_load_class: not called CC=1 SCAR=6.9
| |||||||||||
| 173 | 0 | my ($self, $prefix, $class) = @_; | |||||||||
| 174 | |||||||||||
| 175 | 0 | my $package = $self->_class2package($prefix, $class); | |||||||||
| 176 | 0 | Class::Load::load_class( $package ); | |||||||||
| 177 | |||||||||||
| 178 | 0 | return $package; | |||||||||
| 179 | } | ||||||||||
| 180 | |||||||||||
| 181 | 0 | ▶ | sub _process_log { | ||||||||
|
Subroutine
_process_log: not called CC=1 SCAR=6.9
| |||||||||||
| 182 | 0 | my $self = shift; | |||||||||
| 183 | |||||||||||
| 184 | 0 | my $config = $self->_config->{Log}; | |||||||||
| 185 | |||||||||||
| 186 | 0 | $self->LogConfig( $config ); | |||||||||
| 187 | |||||||||||
| 188 | my $logger_class = $self->_load_class( | ||||||||||
| 189 | 'Clio::Log', $config->{Class} | ||||||||||
| 190 | 0 | ); | |||||||||
| 191 | |||||||||||
| 192 | 0 | $self->logger_class( $logger_class ); | |||||||||
| 193 | } | ||||||||||
| 194 | |||||||||||
| 195 | |||||||||||
| 196 | 0 | ▶ | sub _process_server { | ||||||||
|
Subroutine
_process_server: not called CC=1 SCAR=6.9
| |||||||||||
| 197 | 0 | my $self = shift; | |||||||||
| 198 | |||||||||||
| 199 | 0 | my $config = $self->_config->{Server}; | |||||||||
| 200 | |||||||||||
| 201 | 0 | $self->ServerConfig( $config ); | |||||||||
| 202 | |||||||||||
| 203 | my $server_class = $self->_load_class( | ||||||||||
| 204 | 'Clio::Server', $config->{Class} | ||||||||||
| 205 | 0 | ); | |||||||||
| 206 | |||||||||||
| 207 | 0 | $self->server_class( $server_class ); | |||||||||
| 208 | |||||||||||
| 209 | 0 | my $listen = $config->{Listen}; | |||||||||
| 210 | |||||||||||
| 211 | 0 | my ($host, $port) = split(/:/, $listen); | |||||||||
| 212 | |||||||||||
| 213 | 0 | $self->server_host_port({ | |||||||||
| 214 | host => $host, | ||||||||||
| 215 | port => $port, | ||||||||||
| 216 | }); | ||||||||||
| 217 | |||||||||||
| 218 | 0 | $self->_process_server_client(); | |||||||||
| 219 | } | ||||||||||
| 220 | |||||||||||
| 221 | 0 | ▶ | sub _arrayify { | ||||||||
|
Subroutine
_arrayify: not called CC=3 SCAR=24.8
| |||||||||||
| 222 | 0 | my ($self, $val) = @_; | |||||||||
| 223 | |||||||||||
| 224 | 0 | ▶ | return () unless defined $val; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 225 | 0 | ▶ | return ref $val eq 'ARRAY' ? @{ $val } : ( $val ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 226 | } | ||||||||||
| 227 | |||||||||||
| 228 | 0 | ▶ | sub _load_io_filters { | ||||||||
|
Subroutine
_load_io_filters: not called CC=3 SCAR=24.8
| |||||||||||
| 229 | 0 | my ($self, %args) = @_; | |||||||||
| 230 | |||||||||||
| 231 | 0 | my @input_filters = $self->_arrayify( $args{config}->{InputFilter} ); | |||||||||
| 232 | 0 | for ( @input_filters ) { | |||||||||
| 233 | 0 | my $role = $self->_load_class( "$args{filters}InputFilter", $_ ); | |||||||||
| 234 | 0 | Role::Tiny->apply_role_to_package($args{target}, $role ); | |||||||||
| 235 | } | ||||||||||
| 236 | 0 | my @output_filters = $self->_arrayify( $args{config}->{OutputFilter} ); | |||||||||
| 237 | 0 | for ( @output_filters ) { | |||||||||
| 238 | 0 | my $role = $self->_load_class( "$args{filters}OutputFilter", $_ ); | |||||||||
| 239 | 0 | Role::Tiny->apply_role_to_package($args{target}, $role ); | |||||||||
| 240 | } | ||||||||||
| 241 | }; | ||||||||||
| 242 | |||||||||||
| 243 | 0 | ▶ | sub _process_daemon { | ||||||||
|
Subroutine
_process_daemon: not called CC=1 SCAR=6.9
| |||||||||||
| 244 | 0 | my $self = shift; | |||||||||
| 245 | |||||||||||
| 246 | 0 | my $config = $self->_config->{Daemon}; | |||||||||
| 247 | |||||||||||
| 248 | $self->run_as_user_group( | ||||||||||
| 249 | 0 | [ @{$config}{qw(User Group)} ] | |||||||||
| 250 | ); | ||||||||||
| 251 | |||||||||||
| 252 | 0 | $self->pid_file( $config->{PidFile} ); | |||||||||
| 253 | } | ||||||||||
| 254 | |||||||||||
| 255 | |||||||||||
| 256 | |||||||||||
| 257 | 0 | ▶ | sub _process_command { | ||||||||
|
Subroutine
_process_command: not called CC=1 SCAR=6.9
| |||||||||||
| 258 | 0 | my $self = shift; | |||||||||
| 259 | |||||||||||
| 260 | 0 | my $config = $self->_config->{Command}; | |||||||||
| 261 | 0 | $self->CommandConfig( $config ); | |||||||||
| 262 | |||||||||||
| 263 | 0 | $self->_load_io_filters( | |||||||||
| 264 | filters => 'Clio::Process', | ||||||||||
| 265 | target => 'Clio::Process', | ||||||||||
| 266 | config => $config | ||||||||||
| 267 | ); | ||||||||||
| 268 | } | ||||||||||
| 269 | |||||||||||
| 270 | 0 | ▶ | sub _process_server_client { | ||||||||
|
Subroutine
_process_server_client: not called CC=1 SCAR=6.9
| |||||||||||
| 271 | 0 | my ($self) = @_; | |||||||||
| 272 | |||||||||||
| 273 | 0 | my $config = $self->_config->{Server}->{Client}; | |||||||||
| 274 | 0 | $self->ServerClientConfig( $config ); | |||||||||
| 275 | |||||||||||
| 276 | my $client_class = $self->_load_class( | ||||||||||
| 277 | $self->server_class .'::Client', $config->{Class} | ||||||||||
| 278 | 0 | ); | |||||||||
| 279 | 0 | $self->server_client_class( $client_class ); | |||||||||
| 280 | |||||||||||
| 281 | 0 | $self->_load_io_filters( | |||||||||
| 282 | filters => 'Clio::Client', | ||||||||||
| 283 | target => $client_class, | ||||||||||
| 284 | config => $config | ||||||||||
| 285 | ); | ||||||||||
| 286 | } | ||||||||||
| 287 | |||||||||||
| 288 | |||||||||||
| 289 | 2 | 1; | |||||||||
| 290 | |||||||||||
| 291 | |||||||||||
| 292 | __END__ | ||||||||||