Config.pm 1.3 KB

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