« Crop/Object/Warehouse/Lang/SQL/Query/Select/Node/Root.pm
↑ Summary
Crop/Object/Warehouse/Lang/SQL/Query.pm »
| 1 | package Crop::Config; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Crop::Config - Configuration management for the Crop framework | ||
| 8 | |||
| 9 | =head1 VERSION | ||
| 10 | |||
| 11 | 0.1.26 | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | use Crop::Config; | ||
| 16 | my $config = Crop::Config->data; | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | Crop::Config provides configuration data for the Crop framework. It loads, validates, and parses the main configuration XML file, supporting environment-based overrides and schema validation. | ||
| 21 | |||
| 22 | =head1 CONSTANTS | ||
| 23 | |||
| 24 | =over 4 | ||
| 25 | |||
| 26 | =item * CONF_PATH | ||
| 27 | Directory containing configuration files. Defaults to C<~/.crop>, can be overridden by the C<CROP_CONFIG> environment variable. | ||
| 28 | |||
| 29 | =item * CONFIG_FILE | ||
| 30 | Name of the main configuration file (default: C<global.xml>). | ||
| 31 | |||
| 32 | =item * SCHEMA_FILE | ||
| 33 | Name of the schema file for validating the main config (default: C<global.xsd>). | ||
| 34 | |||
| 35 | =item * SCHEMA_ORIGIN | ||
| 36 | URL to the original schema. This is the only constant you MUST set to the actual value. | ||
| 37 | |||
| 38 | =back | ||
| 39 | |||
| 40 | =head1 METHODS | ||
| 41 | |||
| 42 | =head2 data | ||
| 43 | |||
| 44 | my $config = Crop::Config->data; | ||
| 45 | |||
| 46 | Returns a hash reference containing all configuration parameters from the config file. | ||
| 47 | |||
| 48 | =head1 INSTALLATION | ||
| 49 | |||
| 50 | To install this module, run the following commands: | ||
| 51 | |||
| 52 | perl Makefile.PL | ||
| 53 | make | ||
| 54 | make test | ||
| 55 | make install | ||
| 56 | |||
| 57 | =head1 DEPENDENCIES | ||
| 58 | |||
| 59 | =over 4 | ||
| 60 | |||
| 61 | =item * XML::LibXML | ||
| 62 | =item * Time::Stamp | ||
| 63 | =item * Clone | ||
| 64 | =item * XML::LibXSLT | ||
| 65 | =item * JSON | ||
| 66 | =item * CGI::Cookie | ||
| 67 | =item * CGI::Fast | ||
| 68 | |||
| 69 | =back | ||
| 70 | |||
| 71 | =head1 AUTHORS | ||
| 72 | |||
| 73 | Euvgenio (Core Developer) | ||
| 74 | |||
| 75 | Alex (Contributor) | ||
| 76 | |||
| 77 | =head1 COPYRIGHT AND LICENSE | ||
| 78 | |||
| 79 | Apache 2.0 | ||
| 80 | |||
| 81 | See also: L<https://creazilla.com/pages/creazilla-on-perl> | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | use v5.14; | ||
| 86 | use warnings; | ||
| 87 | |||
| 88 | use XML::LibXML; | ||
| 89 | use XML::LibXML::XPathContext; | ||
| 90 | |||
| 91 | sub the; | ||
| 92 | |||
| 93 | =begin nd | ||
| 94 | Constant: CONF_PATH | ||
| 95 | Directory name contains configuration files. | ||
| 96 | |||
| 97 | Default value is '~/.crop'. | ||
| 98 | Could be redefined by enviroment variable CROP_CONFIG to run several projects. | ||
| 99 | |||
| 100 | To use the Apache Server You need to set 'FcgidInitialEnv CROP_CONFIG /home/ms/back/.crop' in | ||
| 101 | Apache config file. | ||
| 102 | |||
| 103 | Constant: CONFIG_FILE | ||
| 104 | Name of the main configuration file. | ||
| 105 | |||
| 106 | Constant: SCHEMA_FILE | ||
| 107 | Name of the Schema file for check XML of main config. | ||
| 108 | |||
| 109 | Constant: SHEMA_ORIGIN | ||
| 110 | The URL to original Shema. | ||
| 111 | |||
| 112 | This is ONLY constant what you MUST set to the actual value. | ||
| 113 | =cut | ||
| 114 | use constant { | ||
| 115 | CONF_PATH => $ENV{CROP_CONFIG} || "$ENV{HOME}/.crop", | ||
| 116 | CONFIG_FILE => '/global.xml', | ||
| 117 | SCHEMA_FILE => '/global.xsd', | ||
| 118 | SCHEMA_ORIGIN => 'http://example.org/schema/MSC', # SET TO THE ACTUAL VALUE !!! | ||
| 119 | }; | ||
| 120 | |||
| 121 | =begin nd | ||
| 122 | Variable: my $Schema_ns; | ||
| 123 | Namespace of the given schema for use in the config.xml. | ||
| 124 | |||
| 125 | It is evaluated by lowercasing last part of <SCHEMA_ORIGIN>. | ||
| 126 | =cut | ||
| 127 | my ($Schema_ns) = SCHEMA_ORIGIN =~ /(\w+)$/; | ||
| 128 | $Schema_ns = lc $Schema_ns; | ||
| 129 | |||
| 130 | my $conf_path = CONF_PATH . CONFIG_FILE; | ||
| 131 | -e $conf_path or die "No main config '$conf_path' file found."; | ||
| 132 | |||
| 133 | # read config, validate | ||
| 134 | my $dom = XML::LibXML->load_xml(location => $conf_path) or die "Can not load xml"; | ||
| 135 | my $schema = XML::LibXML::Schema->new(location => CONF_PATH . SCHEMA_FILE); | ||
| 136 | eval {$schema->validate($dom)}; | ||
| 137 | if ($@) { | ||
| 138 | print STDERR "File global.xml does not match a given schema. Error message: $@\n"; | ||
| 139 | exit; | ||
| 140 | } | ||
| 141 | |||
| 142 | my $xpc = XML::LibXML::XPathContext->new($dom); | ||
| 143 | $xpc->registerNs($Schema_ns, SCHEMA_ORIGIN); | ||
| 144 | |||
| 145 | =begin nd | ||
| 146 | Variable: my $Data | ||
| 147 | A prepared hash contains all the configuration parameters from the config file. | ||
| 148 | =cut | ||
| 149 | my %Data = ( | ||
| 150 | install => { | ||
| 151 | path => the("$Schema_ns:project/$Schema_ns:install/$Schema_ns:path")->to_literal, | ||
| 152 | url => the("$Schema_ns:project/$Schema_ns:install/$Schema_ns:url") ->to_literal, | ||
| 153 | mode => the("$Schema_ns:project/$Schema_ns:install/$Schema_ns:mode")->to_literal, | ||
| 154 | }, | ||
| 155 | warehouse => { | ||
| 156 | db => {}, | ||
| 157 | relation => {}, | ||
| 158 | }, | ||
| 159 | upload => { | ||
| 160 | dir => the("$Schema_ns:project/$Schema_ns:upload/$Schema_ns:dir") ->to_literal, | ||
| 161 | path => the("$Schema_ns:project/$Schema_ns:upload/$Schema_ns:path")->to_literal, | ||
| 162 | url => the("$Schema_ns:project/$Schema_ns:upload/$Schema_ns:url") ->to_literal, | ||
| 163 | }, | ||
| 164 | logLevel => the("/$Schema_ns:project/$Schema_ns:logLevel")->to_literal, | ||
| 165 | debug => { | ||
| 166 | output => the("/$Schema_ns:project/$Schema_ns:debug/$Schema_ns:output")->to_literal, | ||
| 167 | layer => [], | ||
| 168 | }, | ||
| 169 | ); | ||
| 170 | |||
| 171 | # build perl-structure | ||
| 172 | for my $db ($xpc->findnodes("/$Schema_ns:project/$Schema_ns:warehouse/$Schema_ns:db")) { | ||
| 173 | my $id = the './@id', $db; | ||
| 174 | my $name = the "./$Schema_ns:name", $db; | ||
| 175 | my $driver = the "./$Schema_ns:driver", $db; | ||
| 176 | |||
| 177 | my $server = the "./$Schema_ns:server", $db; | ||
| 178 | my $host = the "./$Schema_ns:host", $server; | ||
| 179 | my $port = the "./$Schema_ns:port", $server; | ||
| 180 | |||
| 181 | my $cur_db = $Data{warehouse}{db}{$id->to_literal} = { | ||
| 182 | name => $name->to_literal, | ||
| 183 | server => { | ||
| 184 | host => $host->to_literal, | ||
| 185 | port => $port->to_literal, | ||
| 186 | }, | ||
| 187 | driver => $driver->to_literal, | ||
| 188 | role => { | ||
| 189 | user => undef, | ||
| 190 | admin => undef, | ||
| 191 | }, | ||
| 192 | }; | ||
| 193 | |||
| 194 | for (qw/ user admin /) { | ||
| 195 | my $role = the "./$Schema_ns:role/$Schema_ns:$_", $db; | ||
| 196 | |||
| 197 | my $login = the "./$Schema_ns:login", $role; | ||
| 198 | my $pass = the "./$Schema_ns:pass", $role; | ||
| 199 | |||
| 200 | $cur_db->{role}{$_}{login} = $login->to_literal; | ||
| 201 | $cur_db->{role}{$_}{pass} = $pass->to_literal; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | for (@{$xpc->findnodes("/$Schema_ns:project/$Schema_ns:warehouse/$Schema_ns:relation")}) { | ||
| 206 | my $name = the './@name', $_; | ||
| 207 | my $db = the "./$Schema_ns:db", $_; | ||
| 208 | |||
| 209 | $Data{warehouse}{relation}{$name->to_literal} = $db->to_literal; | ||
| 210 | } | ||
| 211 | |||
| 212 | for (@{$xpc->findnodes("/$Schema_ns:project/$Schema_ns:test/$Schema_ns:url")}) { | ||
| 213 | my $name = the './@name', $_; | ||
| 214 | my $db = the "./$Schema_ns:path", $_; | ||
| 215 | |||
| 216 | $Data{test}{url}{$name->to_literal} = $db->to_literal; | ||
| 217 | } | ||
| 218 | |||
| 219 | push @{$Data{debug}{layer}}, $_->to_literal for $xpc->findnodes("/$Schema_ns:project/$Schema_ns:debug/$Schema_ns:layer"); | ||
| 220 | |||
| 221 | =begin nd | ||
| 222 | Function: the ($item, $base) | ||
| 223 | Get single value from XML-struct. | ||
| 224 | =cut | ||
| 225 | sub the { | ||
| 226 | my ($item, $base) = @_; | ||
| 227 | |||
| 228 | if (defined $base) { | ||
| 229 | shift @{$xpc->findnodes($item, $base)}; | ||
| 230 | } else { | ||
| 231 | shift @{$xpc->findnodes($item)}; | ||
| 232 | } | ||
| 233 | } | ||
| 234 | |||
| 235 | =begin nd | ||
| 236 | Function: data ( ) | ||
| 237 | Get Singlton. | ||
| 238 | =cut | ||
| 239 | sub data { | ||
| 240 | return \%Data; | ||
| 241 | } | ||
| 242 | |||
| 243 | 1; |