1 package Koha::REST::Plugin::PluginRoutes;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Mojo::Base 'Mojolicious::Plugin';
22 use Koha::Exceptions::Plugin;
30 Koha::REST::Plugin::PluginRoutes
41 my ( $self, $app, $config ) = @_;
43 my $spec = $config->{spec};
44 my $validator = $config->{validator};
48 if ( C4::Context->config("enable_plugins") )
50 # plugin needs to define a namespace
51 @plugins = Koha::Plugins->new()->GetPlugins(
53 method => 'api_namespace',
58 foreach my $plugin ( @plugins ) {
59 $spec = inject_routes( $spec, $plugin, $validator );
70 my ( $spec, $plugin, $validator ) = @_;
74 my $backup_spec = merge_spec( clone($spec), $plugin );
75 if ( spec_ok( $backup_spec, $validator ) ) {
76 $spec = merge_spec( $spec, $plugin );
79 Koha::Exceptions::Plugin->throw(
80 "The resulting spec is invalid. Skipping " . $plugin->get_metadata->{name}
97 my ( $spec, $plugin ) = @_;
99 if($plugin->can('api_routes')) {
100 my $plugin_spec = $plugin->api_routes;
102 foreach my $route ( keys %{ $plugin_spec } ) {
103 my $THE_route = '/contrib/' . $plugin->api_namespace . $route;
104 if ( exists $spec->{ $THE_route } ) {
105 # Route exists, overwriting is forbidden
106 Koha::Exceptions::Plugin::ForbiddenAction->throw(
107 "Attempted to overwrite $THE_route"
111 $spec->{'paths'}->{ $THE_route } = $plugin_spec->{ $route };
115 if($plugin->can('static_routes')) {
116 my $plugin_spec = $plugin->static_routes;
118 foreach my $route ( keys %{ $plugin_spec } ) {
120 my $THE_route = '/contrib/' . $plugin->api_namespace . '/static'.$route;
121 if ( exists $spec->{ $THE_route } ) {
122 # Route exists, overwriting is forbidden
123 Koha::Exceptions::Plugin::ForbiddenAction->throw(
124 "Attempted to overwrite $THE_route"
128 $spec->{'paths'}->{ $THE_route } = $plugin_spec->{ $route };
139 my ( $spec, $validator ) = @_;
142 $validator->load_and_validate_schema(
145 allow_invalid_ref => 1,