From 6627c9eba396d61e9338a7d012451e93db184658 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Mon, 2 Oct 2017 11:29:40 +0200 Subject: [PATCH] SL::Version - Versionsbehandlung aus Form ausgelagert --- SL/Form.pm | 10 ++----- SL/Version.pm | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 SL/Version.pm 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 -- 2.20.1