Crop-Config

 view release on metacpan or  search on metacpan

lib/Crop/Config.pm  view on Meta::CPAN

package Crop::Config;

our $VERSION = '0.1.25';

=begin nd
Class: Crop::Config
	Configuration data.
	
	SCHEMA_ORIGIN is ONLY constant what you MUST set to the actual value.
=cut

use v5.14;
use warnings;

use XML::LibXML;
use XML::LibXML::XPathContext;

sub the;

=begin nd
Constant: CONF_PATH
	Directory name contains configuration files.
	
	Default value is '~/.crop'.
	Could be redefined by enviroment variable CROP_CONFIG to run several projects.
	
	To use the Apache Server You need to set 'FcgidInitialEnv CROP_CONFIG /home/ms/back/.crop' in
	Apache config file.

Constant: CONFIG_FILE
	Name of the main configuration file.

Constant: SCHEMA_FILE
	Name of the Schema file for check XML of main config.

Constant: SHEMA_ORIGIN
	The URL to original Shema.
	
	This is ONLY constant what you MUST set to the actual value.
=cut
use constant {
	CONF_PATH     => $ENV{CROP_CONFIG} || "$ENV{HOME}/.crop",
	CONFIG_FILE   => '/global.xml',
	SCHEMA_FILE   => '/global.xsd',
	SCHEMA_ORIGIN => 'http://example.org/schema/MSC',  # SET TO THE ACTUAL VALUE !!!
};

=begin nd
Variable: my $Schema_ns;
	Namespace of the given schema for use in the config.xml.
	
	It is evaluated by lowercasing last part of <SCHEMA_ORIGIN>.
=cut
my ($Schema_ns) = SCHEMA_ORIGIN =~ /(\w+)$/;
$Schema_ns = lc $Schema_ns;

my $conf_path = CONF_PATH . CONFIG_FILE;
-e $conf_path or die "No main config '$conf_path' file found.";

# read config, validate
my $dom = XML::LibXML->load_xml(location => $conf_path) or die "Can not load xml";  
my $schema = XML::LibXML::Schema->new(location => CONF_PATH . SCHEMA_FILE);
eval {$schema->validate($dom)};
if ($@) {
	print STDERR "File global.xml does not match a given schema. Error message: $@\n";
	exit;
}

my $xpc = XML::LibXML::XPathContext->new($dom);
$xpc->registerNs($Schema_ns, SCHEMA_ORIGIN);

=begin nd
Variable: my $Data
	A prepared hash contains all the configuration parameters from the config file.
=cut
my %Data = (
	install   => {
		path => the("$Schema_ns:project/$Schema_ns:install/$Schema_ns:path")->to_literal,
		url  => the("$Schema_ns:project/$Schema_ns:install/$Schema_ns:url") ->to_literal,
		mode => the("$Schema_ns:project/$Schema_ns:install/$Schema_ns:mode")->to_literal,
	},
	warehouse => {
		db       => {},
		relation => {},
	},
	upload   => {
		dir  => the("$Schema_ns:project/$Schema_ns:upload/$Schema_ns:dir") ->to_literal,



( run in 2.105 seconds using v1.01-cache-2.11-cpan-acf6aa7dc9e )