View | Details | Raw Unified | Return to bug 28172
Collapse All | Expand All

(-)a/Koha/Plugins.pm (-33 / +35 lines)
Lines 64-84 Calls a plugin method for all enabled plugins Link Here
64
sub call {
64
sub call {
65
    my ($class, $method, @args) = @_;
65
    my ($class, $method, @args) = @_;
66
66
67
    my @responses;
67
    return unless C4::Context->config('enable_plugins');
68
    if (C4::Context->config('enable_plugins')) {
69
        my @plugins = $class->new({ enable_plugins => 1 })->GetPlugins({ method => $method });
70
        @plugins = grep { $_->can($method) } @plugins;
71
        foreach my $plugin (@plugins) {
72
            my $response = eval { $plugin->$method(@args) };
73
            if ($@) {
74
                warn sprintf("Plugin error (%s): %s", $plugin->get_metadata->{name}, $@);
75
                next;
76
            }
77
68
78
            push @responses, $response;
69
    my @responses;
70
    my @plugins = $class->new()->GetPlugins({ method => $method });
71
    foreach my $plugin (@plugins) {
72
        my $response = eval { $plugin->$method(@args) };
73
        if ($@) {
74
            warn sprintf("Plugin error (%s): %s", $plugin->get_metadata->{name}, $@);
75
            next;
79
        }
76
        }
80
77
78
        push @responses, $response;
81
    }
79
    }
80
82
    return @responses;
81
    return @responses;
83
}
82
}
84
83
Lines 103-139 sub GetPlugins { Link Here
103
102
104
    my $method       = $params->{method};
103
    my $method       = $params->{method};
105
    my $req_metadata = $params->{metadata} // {};
104
    my $req_metadata = $params->{metadata} // {};
106
105
    my $all          = $params->{all} // 0;
107
    my $filter = ( $method ) ? { plugin_method => $method } : undef;
106
    my $nocache      = 0;
108
107
109
    my $plugin_classes = Koha::Plugins::Methods->search(
108
    my @plugin_classes;
110
        $filter,
109
    if ($all) {
111
        {   columns  => 'plugin_class',
110
        @plugin_classes = $self->plugins();
112
            distinct => 1
111
113
        }
112
        # Do not use Module::Load::Conditional cache if we want all plugins
114
    )->_resultset->get_column('plugin_class');
113
        # as $self->plugins can return uninstalled plugins
114
        $nocache = 1;
115
    } else {
116
        my $rs = Koha::Database->schema->resultset('PluginData');
117
        $rs = $rs->search({ plugin_key => '__ENABLED__', plugin_value => 1 });
118
        @plugin_classes = $rs->get_column('plugin_class')->all();
119
    }
115
120
116
    my @plugins;
121
    my @plugins;
122
    foreach my $plugin_class (@plugin_classes) {
123
        if ( can_load( modules => { $plugin_class => undef }, nocache => $nocache ) ) {
124
            next unless $plugin_class->isa('Koha::Plugins::Base');
125
            next if $method && !$plugin_class->can($method);
117
126
118
    # Loop through all plugins that implement at least a method
119
    while ( my $plugin_class = $plugin_classes->next ) {
120
121
        if ( can_load( modules => { $plugin_class => undef }, nocache => 1 ) ) {
122
            my $plugin = $plugin_class->new({
127
            my $plugin = $plugin_class->new({
123
                enable_plugins => $self->{'enable_plugins'}
128
                enable_plugins => $self->{'enable_plugins'}
124
                    # loads even if plugins are disabled
129
                    # loads even if plugins are disabled
125
                    # FIXME: is this for testing without bothering to mock config?
130
                    # FIXME: is this for testing without bothering to mock config?
126
            });
131
            });
127
132
128
            next unless $plugin->is_enabled or
129
                        defined($params->{all}) && $params->{all};
130
131
            # filter the plugin out by metadata
133
            # filter the plugin out by metadata
132
            my $plugin_metadata = $plugin->get_metadata;
134
            if (%$req_metadata) {
133
            next
135
                my $plugin_metadata = $plugin->get_metadata;
134
                if $plugin_metadata
136
                next
135
                and %$req_metadata
137
                    if $plugin_metadata
136
                and any { !$plugin_metadata->{$_} || $plugin_metadata->{$_} ne $req_metadata->{$_} } keys %$req_metadata;
138
                    and any { !$plugin_metadata->{$_} || $plugin_metadata->{$_} ne $req_metadata->{$_} } keys %$req_metadata;
139
            }
137
140
138
            push @plugins, $plugin;
141
            push @plugins, $plugin;
139
        } elsif ( defined($params->{errors}) && $params->{errors} ){
142
        } elsif ( defined($params->{errors}) && $params->{errors} ){
140
- 

Return to bug 28172