Bug 25273: (follow-up) Don't die on unknown authtype
[koha.git] / C4 / Installer / PerlModules.pm
blob01ccc53fea9a4b0b3adf01c684a6805d50125d9c
1 package C4::Installer::PerlModules;
3 use warnings;
4 use strict;
6 use File::Spec;
7 use File::Basename;
8 use Module::CPANfile;
10 sub new {
11 my $invocant = shift;
12 my $self = {
13 missing_pm => [],
14 upgrade_pm => [],
15 current_pm => [],
18 my $type = ref($invocant) || $invocant;
19 bless ($self, $type);
20 return $self;
23 sub prereqs {
24 my $self = shift;
26 unless (defined $self->{prereqs}) {
27 my $filename = $INC{'C4/Installer/PerlModules.pm'};
28 my $path = dirname(dirname(dirname($filename)));
29 $self->{prereqs} = Module::CPANfile->load("$path/cpanfile")->prereqs;
32 return $self->{prereqs};
35 sub prereq_pm {
36 my $self = shift;
38 my $prereq_pm = {};
39 my $reqs = $self->prereqs->merged_requirements;
40 foreach my $module ($reqs->required_modules) {
41 $prereq_pm->{$module} = $reqs->requirements_for_module($module);
44 return $prereq_pm;
47 sub versions_info {
48 my $self = shift;
50 # Reset these arrayref each pass through to ensure current information
51 $self->{'missing_pm'} = [];
52 $self->{'upgrade_pm'} = [];
53 $self->{'current_pm'} = [];
55 foreach my $phase ($self->prereqs->phases) {
56 foreach my $type ($self->prereqs->types_in($phase)) {
57 my $reqs = $self->prereqs->requirements_for($phase, $type);
58 foreach my $module ($reqs->required_modules) {
59 no warnings; # perl throws warns for invalid $VERSION numbers which some modules use
61 my $module_infos = {
62 cur_ver => 0,
63 required => $type eq 'requires',
66 my $vers = $reqs->structured_requirements_for_module($module);
67 for my $req (@$vers) {
68 if ( $req->[0] eq '>=' || $req->[0] eq '>' ) {
69 $module_infos->{min_ver} = $req->[1];
70 } elsif ( $req->[0] eq '<=' || $req->[0] eq '<' ) {
71 $module_infos->{max_ver} = $req->[1];
72 } else {
73 push @{$module_infos->{exc_ver}}, $req->[1];
77 my $attr;
79 $Readonly::XS::MAGIC_COOKIE="Do NOT use or require Readonly::XS unless you're me.";
80 eval "require $module";
81 if ($@) {
82 $attr = 'missing_pm';
83 } else {
84 my $pkg_version = $module->can("VERSION") ? $module->VERSION : 0;
85 $module_infos->{cur_ver} = $pkg_version;
86 if ($reqs->accepts_module($module => $pkg_version)) {
87 $attr = 'current_pm';
88 } else {
89 $attr = 'upgrade_pm';
93 push @{ $self->{$attr} }, { $module => $module_infos };
99 sub get_attr {
100 return $_[0]->{$_[1]};
104 __END__
106 =head1 NAME
108 C4::Installer::PerlModules
110 =head1 ABSTRACT
112 A module for manipulating Koha Perl dependency list objects.
114 =head1 METHODS
116 =head2 new()
118 Creates a new PerlModules object
120 example:
121 C<my $perl_modules = C4::Installer::PerlModules->new;>
123 =head2 prereq_pm()
125 Returns a hashref of a hash of module information suitable for use in Makefile.PL
127 example:
128 C<my $perl_modules = C4::Installer::PerlModules->new;
132 PREREQ_PM => $perl_modules->prereq_pm,>
135 =head2 versions_info
137 C<$perl_modules->versions_info;>
139 This loads info of required modules into three accessors: missing_pm,
140 upgrade_pm, and current_pm. Each of these may be accessed by using the
141 C<get_attr> method. Each accessor returns an anonymous array who's
142 elements are anonymous hashes. They follow this format (NOTE: Upgrade
143 status is indicated by the accessor name.):
147 'Text::CSV::Encoded' => {
148 'required' => 1,
149 'cur_ver' => 0.09,
150 'min_ver' => '0.09'
154 'Biblio::EndnoteStyle' => {
155 'required' => 1,
156 'cur_ver' => 0,
157 'min_ver' => '0.05'
162 =head2 get_attr(attr_name)
164 Returns an anonymous array containing the contents of the passed in accessor. Valid accessors are:
166 missing_pm - Perl modules used by Koha but not currently installed.
168 upgrade_pm - Perl modules currently installed but below the minimum version required by Koha.
170 current_pm - Perl modules currently installed and up to date as required by Koha.
172 example:
173 C<my $missing_pm = $perl_modules->get_attr('missing_pm');>
176 =head1 AUTHOR
178 Chris Nighswonger <cnighswonger AT foundations DOT edu>
180 =head1 COPYRIGHT
182 Copyright 2010 Foundations Bible College.
184 =head1 LICENSE
186 This file is part of Koha.
188 Koha is free software; you can redistribute it and/or modify it
189 under the terms of the GNU General Public License as published by
190 the Free Software Foundation; either version 3 of the License, or
191 (at your option) any later version.
193 Koha is distributed in the hope that it will be useful, but
194 WITHOUT ANY WARRANTY; without even the implied warranty of
195 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
196 GNU General Public License for more details.
198 You should have received a copy of the GNU General Public License
199 along with Koha; if not, see <http://www.gnu.org/licenses>.
201 =head1 DISCLAIMER OF WARRANTY
203 Koha is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
204 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
206 =cut