From: Sven Schöling Date: Mon, 2 Oct 2017 09:29:40 +0000 (+0200) Subject: SL::Version - Versionsbehandlung aus Form ausgelagert X-Git-Tag: release-3.5.4~591 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=6627c9eba396d61e9338a7d012451e93db184658;p=kivitendo-erp.git SL::Version - Versionsbehandlung aus Form ausgelagert --- diff --git a/SL/Form.pm b/SL/Form.pm index cd73fa09d..48a03887d 100644 --- a/SL/Form.pm +++ b/SL/Form.pm @@ -79,6 +79,7 @@ use SL::Request; use SL::Template; use SL::User; use SL::Util; +use SL::Version; use SL::X; use Template; use URI; @@ -91,14 +92,7 @@ use SL::Helper::CreatePDF qw(merge_pdfs); use strict; sub read_version { - my ($self) = @_; - - open VERSION_FILE, "VERSION"; # New but flexible code reads version from VERSION-file - my $version = ; - $version =~ s/[^0-9A-Za-z\.\_\-]//g; # only allow numbers, letters, points, underscores and dashes. Prevents injecting of malicious code. - close VERSION_FILE; - - return $version; + SL::Version->get_version; } sub new { diff --git a/SL/Version.pm b/SL/Version.pm new file mode 100644 index 000000000..f515721c5 --- /dev/null +++ b/SL/Version.pm @@ -0,0 +1,82 @@ +package SL::Version; + +use strict; + +our $instance; + +sub new { + bless \my $instace, __PACKAGE__; +} + +sub get_instance { + $instance //= $_[0]->new; +} + +sub get_version { + $$instance //= do { + open my $version_file, '<', "VERSION" or die 'can not open VERSION file'; + my $version = <$version_file>; + close $version_file; + + if ( -f "BUILD" ) { + open my $build_file, '<', "BUILD" or die 'can not open BUILD file'; + my $build = <$build_file>; + close $build_file; + $version .= '-' . $build; + } + + # only allow numbers, letters, points, underscores and dashes. Prevents injecting of malicious code. + $version =~ s/[^0-9A-Za-z\.\_\-]//g; + + $version; + } +} + +1; + +__END__ + +=encoding utf-8 + +=head1 NAME + +SL::Version + +=head1 SYNOPSIS + + use SL::Version; + + my $version = SL::Version->get_version + +=head1 DESCRIPTION + +This module is a singleton for the sole reason that SL::Form doesn't have to +cache the version. + +=head1 FUNCTIONS + +=head2 C + +Creates a new object. Should never be called. + +=head2 C + +Creates a singleton instance if none exists and returns. + +=head2 C + +Parses the version from the C file. + +If the file C exists, appends its contents as a build number. + +Returns a sanitized version string. + +=head1 BUGS + +None yet :) + +=head1 AUTHOR + +Sven Schöling Es.schoeling@linet-services.deE + +=cut