Guest User

Untitled

a guest
Jun 13th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. commit 0601acc9b4ae5cfe949415c3cb501596fa8417f2
  2. Author: Marcus Ramberg <marcus@nordaaker.com>
  3. Date: Sun Oct 3 18:07:52 2010 +0200
  4.  
  5. Support .mode. json config file
  6.  
  7. diff --git a/lib/Mojolicious/Plugin/JsonConfig.pm b/lib/Mojolicious/Plugin/JsonConfig.pm
  8. index 9ceb878..e314447 100644
  9. --- a/lib/Mojolicious/Plugin/JsonConfig.pm
  10. +++ b/lib/Mojolicious/Plugin/JsonConfig.pm
  11. @@ -21,6 +21,7 @@ sub register {
  12.  
  13. # File
  14. my $file = $conf->{file};
  15. + my $mode_file;
  16. unless ($file) {
  17.  
  18. # Basename
  19. @@ -29,6 +30,7 @@ sub register {
  20. # Remove .pl, .p6 and .t extentions
  21. $file =~ s/(?:\.p(?:l|6))|\.t$//i;
  22.  
  23. + $mode_file=$file.'.' . $app->mode . '.' . ($conf->{ext} || 'json');
  24. # Default extension
  25. $file .= '.' . ($conf->{ext} || 'json');
  26. }
  27. @@ -36,10 +38,13 @@ sub register {
  28. # Absolute path
  29. $file = $app->home->rel_file($file)
  30. unless File::Spec->file_name_is_absolute($file);
  31. + $mode_file = $app->home->rel_file($mode_file)
  32. + if defined $mode_file && ! File::Spec->file_name_is_absolute($mode_file);
  33.  
  34. # Read config file
  35. my $config = {};
  36. my $template = $conf->{template} || {};
  37. +
  38. if (-e $file) { $config = $self->_read_config($file, $template, $app) }
  39.  
  40. # Check for default
  41. @@ -54,6 +59,11 @@ sub register {
  42. qq/Config file "$file" missing, using default config./);
  43. }
  44.  
  45. + if( defined $mode_file && -e $mode_file ) {
  46. + my $mode_config = $self->_read_config($mode_file, $template, $app);
  47. + $config = {%$config, %$mode_config};
  48. + }
  49. +
  50. # Stash key
  51. my $stash_key = $conf->{stash_key} || 'config';
  52.  
  53. @@ -157,6 +167,11 @@ preprocesses it's input with L<Mojo::Template>.
  54.  
  55. The application object can be accessed via C<$app> or the C<app> helper.
  56.  
  57. +=head2 Mode specific config
  58. +
  59. +You can override the default config on a per key basis by adding a
  60. +myapp.$mode.json config file.
  61. +
  62. =head2 Options
  63.  
  64. =over 4
  65. diff --git a/t/mojolicious/json_config_lite_app_mode.json b/t/mojolicious/json_config_lite_app_mode.json
  66. new file mode 100644
  67. index 0000000..db85acb
  68. --- /dev/null
  69. +++ b/t/mojolicious/json_config_lite_app_mode.json
  70. @@ -0,0 +1,3 @@
  71. +{
  72. + "foo" : "bar"
  73. +}
  74. diff --git a/t/mojolicious/json_config_lite_app_mode.t b/t/mojolicious/json_config_lite_app_mode.t
  75. new file mode 100644
  76. index 0000000..914a3e8
  77. --- /dev/null
  78. +++ b/t/mojolicious/json_config_lite_app_mode.t
  79. @@ -0,0 +1,41 @@
  80. +#!/usr/bin/env perl
  81. +
  82. +use strict;
  83. +use warnings;
  84. +
  85. +use utf8;
  86. +
  87. +# Disable epoll, kqueue and IPv6
  88. +BEGIN { $ENV{MOJO_POLL} = $ENV{MOJO_NO_IPV6} = 1; $ENV{MOJO_MODE}='testing'; }
  89. +
  90. +use Mojo::IOLoop;
  91. +use Test::More;
  92. +
  93. +# Make sure sockets are working
  94. +plan skip_all => 'working sockets required for this test!'
  95. + unless Mojo::IOLoop->new->generate_port;
  96. +plan tests => 3;
  97. +
  98. +# QUOTE GOES HERE
  99. +
  100. +use Mojolicious::Lite;
  101. +use Test::Mojo;
  102. +
  103. +# Silence
  104. +app->log->level('error');
  105. +
  106. +# Load plugin
  107. +plugin 'json_config';
  108. +
  109. +# GET /
  110. +get '/' => 'index';
  111. +
  112. +my $t = Test::Mojo->new;
  113. +
  114. +# GET /
  115. +$t->get_ok('/')->status_is(200)->content_like(qr/baz/);
  116. +
  117. +
  118. +__DATA__
  119. +@@ index.html.ep
  120. +<%= $config->{foo} %>
  121. diff --git a/t/mojolicious/json_config_lite_app_mode.testing.json b/t/mojolicious/json_config_lite_app_mode.testing.json
  122. new file mode 100644
  123. index 0000000..cbd0afe
  124. --- /dev/null
  125. +++ b/t/mojolicious/json_config_lite_app_mode.testing.json
  126. @@ -0,0 +1,3 @@
  127. +{
  128. + "foo" : "baz"
  129. +}
Add Comment
Please, Sign In to add comment