Config.pm 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package Trog::Config;
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental';
  5. use feature qw{signatures};
  6. use Config::Tiny;
  7. =head1 Trog::Config
  8. A thin wrapper around Config::Simple which reads the configuration from the appropriate place.
  9. =head2 Trog::Config::get() = Config::Tiny
  10. Returns a configuration object that will be used by server.psgi, the data model and Routing modules.
  11. The object will be cached in memory, so that re-run of get() will return the object fetched earlier.
  12. Pass in a truthy argument to force refetch.
  13. =cut
  14. our $home_cfg = "config/main.cfg";
  15. my $cf;
  16. sub get {
  17. my ($refetch) = @_;
  18. undef $cf if $refetch;
  19. return $cf if $cf;
  20. $cf = Config::Tiny->read($home_cfg) if -f $home_cfg;
  21. return $cf if $cf;
  22. $cf = Config::Tiny->read('config/default.cfg');
  23. return $cf;
  24. }
  25. =head2 Trog::Config::theme_dir()
  26. Returns string corresponding to the path of the theme in use per config.
  27. The string will be cached in memory, so that re-run of get() will return the object fetched earlier.
  28. Pass in a truthy argument to force refetch of both theme dir and config object.
  29. =cut
  30. my $theme_dir = '';
  31. sub theme_dir {
  32. my ($refetch) = @_;
  33. $theme_dir = '' if $refetch;
  34. return $theme_dir if $theme_dir;
  35. get($refetch) if !$cf || $refetch;
  36. $theme_dir = "themes/$cf->{'general'}{'theme'}" if $cf->{'general'}{'theme'} && -d "www/themes/$cf->{'general'}{'theme'}";
  37. return $theme_dir;
  38. }
  39. 1;