| 1 | package Mail::MtPolicyd::PluginChain; | ||||||||||
| 2 | |||||||||||
| 3 | 4 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 4 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 4 | our $VERSION = '1.11'; # VERSION | |||||||||
| 7 | # ABSTRACT: class for a VirtualHost instance | ||||||||||
| 8 | |||||||||||
| 9 | 4 | ▶ | use Mail::MtPolicyd::Result; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | has 'plugins' => ( | ||||||||||
| 12 | is => 'ro', | ||||||||||
| 13 | isa => 'ArrayRef[Mail::MtPolicyd::Plugin]', | ||||||||||
| 14 | 1 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 4 | traits => [ 'Array' ], | |||||||||
| 16 | handles => { | ||||||||||
| 17 | 'add_plugin' => 'push', | ||||||||||
| 18 | } | ||||||||||
| 19 | ); | ||||||||||
| 20 | |||||||||||
| 21 | 4 | has 'plugin_prefix' => ( | |||||||||
| 22 | is => 'ro', isa => 'Str', default => 'Mail::MtPolicyd::Plugin::', | ||||||||||
| 23 | ); | ||||||||||
| 24 | |||||||||||
| 25 | 4 | has 'vhost_name' => ( is => 'rw', isa => 'Maybe[Str]' ); | |||||||||
| 26 | |||||||||||
| 27 | sub run { | ||||||||||
| 28 | 2 | ▶ | my ( $self, $r ) = @_; | ||||||||
|
Subroutine
run: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||
| 29 | 2 | my $result = Mail::MtPolicyd::Result->new; | |||||||||
| 30 | |||||||||||
| 31 | 2 | foreach my $plugin ( @{$self->plugins} ) { | |||||||||
| 32 | 2 | my $abort = 0; | |||||||||
| 33 | 2 | my @plugin_results = $plugin->run($r); | |||||||||
| 34 | 2 | foreach my $plugin_result ( @plugin_results ) { | |||||||||
| 35 | 2 | $result->add_plugin_result($plugin_result); | |||||||||
| 36 | 2 | ▶ | if( $plugin_result->abort ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 2 | $abort = 1; | |||||||||
| 38 | } | ||||||||||
| 39 | } | ||||||||||
| 40 | 2 | ▶ | if( $abort ) { last; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 2 | return $result; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | sub load_plugin { | ||||||||||
| 47 | 1 | ▶ | my ( $self, $plugin_name, $params ) = @_; | ||||||||
|
Subroutine
load_plugin: called CC=4 SCAR=14.6
Pod
undocumented
| |||||||||||
| 48 | 1 | ▶ | if( ! defined $params->{'module'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 0 | die('no module defined for plugin '.$plugin_name.'!'); | |||||||||
| 50 | } | ||||||||||
| 51 | 1 | my $module = $params->{'module'}; | |||||||||
| 52 | 1 | my $plugin_class = $self->plugin_prefix.$module; | |||||||||
| 53 | 1 | my $plugin; | |||||||||
| 54 | |||||||||||
| 55 | 1 | my $code = "require ".$plugin_class.";"; | |||||||||
| 56 | 1 | eval $code; ## no critic (ProhibitStringyEval) | |||||||||
| 57 | 1 | ▶ | if($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 0 | die('could not load module '.$module.' for plugin '.$plugin_name.': '.$@); | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 1 | eval { | |||||||||
| 62 | 1 | $plugin = $plugin_class->new( | |||||||||
| 63 | name => $plugin_name, | ||||||||||
| 64 | vhost_name => $self->vhost_name, | ||||||||||
| 65 | %$params, | ||||||||||
| 66 | ); | ||||||||||
| 67 | }; | ||||||||||
| 68 | 1 | ▶ | if($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 0 | die('could not initialize plugin '.$plugin_name.': '.$@); | |||||||||
| 70 | } | ||||||||||
| 71 | 1 | $self->add_plugin($plugin); | |||||||||
| 72 | 1 | return; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | sub new_from_config { | ||||||||||
| 76 | 1 | ▶ | my ( $class, $vhost_name, $config ) = @_; | ||||||||
|
Subroutine
new_from_config: called CC=4 SCAR=14.8
Pod
undocumented
| |||||||||||
| 77 | |||||||||||
| 78 | 1 | my $self = $class->new( vhost_name => $vhost_name ); | |||||||||
| 79 | |||||||||||
| 80 | 1 | ▶ | if( ! defined $config ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 0 | return( $self ); | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 1 | ▶ | if( ref($config) ne 'HASH' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 0 | die('config must be an hashref!'); | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 1 | foreach my $plugin_name ( keys %{$config} ) { | |||||||||
| 89 | $self->load_plugin($plugin_name, | ||||||||||
| 90 | 1 | $config->{$plugin_name} ); | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 1 | return $self; | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | 4 | __PACKAGE__->meta->make_immutable; | |||||||||
| 97 | |||||||||||
| 98 | 4 | 1; | |||||||||
| 99 | |||||||||||
| 100 | |||||||||||
| 101 | __END__ | ||||||||||
| 102 | =pod | ||||||||||
| 103 | |||||||||||
| 104 | =head1 NAME | ||||||||||
| 105 | |||||||||||
| 106 | Mail::MtPolicyd::PluginChain - class for a VirtualHost instance | ||||||||||
| 107 | |||||||||||
| 108 | =head1 VERSION | ||||||||||
| 109 | |||||||||||
| 110 | version 1.11 | ||||||||||
| 111 | |||||||||||
| 112 | =head1 AUTHOR | ||||||||||
| 113 | |||||||||||
| 114 | Markus Benning <ich@markusbenning.de> | ||||||||||
| 115 | |||||||||||
| 116 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 117 | |||||||||||
| 118 | This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>. | ||||||||||
| 119 | |||||||||||
| 120 | This is free software, licensed under: | ||||||||||
| 121 | |||||||||||
| 122 | The GNU General Public License, Version 2, June 1991 | ||||||||||
| 123 | |||||||||||
| 124 | =cut | ||||||||||
| 125 | |||||||||||