| 1 | package WWW::Garden::Design::Util::Config; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Config::Tiny; | ||
| 7 | |||
| 8 | use File::HomeDir; | ||
| 9 | use File::Spec; | ||
| 10 | |||
| 11 | use Moo; | ||
| 12 | |||
| 13 | use Path::Tiny; # For path(). | ||
| 14 | |||
| 15 | use Types::Standard qw/HashRef Str/; | ||
| 16 | |||
| 17 | has config => | ||
| 18 | ( | ||
| 19 | default => sub{return {} }, | ||
| 20 | is => 'rw', | ||
| 21 | isa => HashRef, | ||
| 22 | required => 1, | ||
| 23 | ); | ||
| 24 | |||
| 25 | has config_path => | ||
| 26 | ( | ||
| 27 | default => sub{return ''}, | ||
| 28 | is => 'rw', | ||
| 29 | isa => Str, | ||
| 30 | required => 0, | ||
| 31 | ); | ||
| 32 | |||
| 33 | our $VERSION = '0.97'; | ||
| 34 | |||
| 35 | # ----------------------------------------------- | ||
| 36 | |||
| 37 | sub BUILD | ||
| 38 | { | ||
| 39 | my($self) = @_; | ||
| 40 | my($module) = 'WWW::Garden::Design'; | ||
| 41 | my($module_dir) = $module; | ||
| 42 | $module_dir =~ s/::/-/g; | ||
| 43 | my($config_name) = 'www.garden.design.conf'; | ||
| 44 | my($path) = File::Spec -> catfile(File::HomeDir -> my_dist_config($module_dir), $config_name); | ||
| 45 | |||
| 46 | $self -> config($self -> _init_config($path) ); | ||
| 47 | |||
| 48 | } # End of BUILD. | ||
| 49 | |||
| 50 | # ----------------------------------------------- | ||
| 51 | |||
| 52 | sub _init_config | ||
| 53 | { | ||
| 54 | my($self, $path) = @_; | ||
| 55 | |||
| 56 | $self -> config_path($path); | ||
| 57 | |||
| 58 | # Check [global]. | ||
| 59 | |||
| 60 | my($config) = Config::Tiny -> read($path); | ||
| 61 | |||
| 62 | die 'Error: ' . Config::Tiny -> errstr . "\n" if (Config::Tiny -> errstr); | ||
| 63 | |||
| 64 | my($section); | ||
| 65 | |||
| 66 | for my $i (1 .. 2) | ||
| 67 | { | ||
| 68 | $section = $i == 1 ? 'global' : $$config{$section}{host}; | ||
| 69 | |||
| 70 | die "Error: Config file '$path' does not contain the section [$section]\n" if (! $$config{$section}); | ||
| 71 | } | ||
| 72 | |||
| 73 | return $$config{$section}; | ||
| 74 | |||
| 75 | } # End of _init_config. | ||
| 76 | |||
| 77 | # -------------------------------------------------- | ||
| 78 | |||
| 79 | 1; | ||
| 80 | |||
| 81 | =head1 NAME | ||
| 82 | |||
| 83 | WWW::Garden::Design::Util::Config - Manage the flowers database | ||
| 84 | |||
| 85 | =head1 Synopsis | ||
| 86 | |||
| 87 | See L<WWW::Garden::Design/Synopsis>. | ||
| 88 | |||
| 89 | See also scripts/copy.config.pl. | ||
| 90 | |||
| 91 | =head1 Description | ||
| 92 | |||
| 93 | L<WWW::Garden::Design> implements an interface to the 'flowers' database. | ||
| 94 | |||
| 95 | =head1 Distributions | ||
| 96 | |||
| 97 | See L<WWW::Garden::Design/Distributions>. | ||
| 98 | |||
| 99 | =head1 Installation | ||
| 100 | |||
| 101 | See L<WWW::Garden::Design/Installation>. | ||
| 102 | |||
| 103 | =head1 Methods | ||
| 104 | |||
| 105 | =head2 config() | ||
| 106 | |||
| 107 | Returns a hashref of options read from the config file, which defaults to | ||
| 108 | C<config/www.garden.design.conf>. | ||
| 109 | |||
| 110 | =head2 config_path() | ||
| 111 | |||
| 112 | Returns a string holding the path to the config file. | ||
| 113 | |||
| 114 | =head1 FAQ | ||
| 115 | |||
| 116 | See L<WWW::Garden::Design/FAQ>. | ||
| 117 | |||
| 118 | =head1 Support | ||
| 119 | |||
| 120 | See L<WWW::Garden::Design/Support>. | ||
| 121 | |||
| 122 | =head1 Author | ||
| 123 | |||
| 124 | C<WWW::Garden::Design> was written by Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2013. | ||
| 125 | |||
| 126 | L<Home page|https://savage.net.au/index.html>. | ||
| 127 | |||
| 128 | =head1 Copyright | ||
| 129 | |||
| 130 | Australian copyright (c) 2018, Ron Savage. | ||
| 131 | All Programs of mine are 'OSI Certified Open Source Software'; | ||
| 132 | you can redistribute them and/or modify them under the terms of | ||
| 133 | The Artistic License V 2, a copy of which is available at: | ||
| 134 | http://www.opensource.org/licenses/index.html | ||
| 135 | |||
| 136 | =cut |