[Catalyst-commits] r6589 - in trunk/Catalyst-Runtime: lib
lib/Catalyst t
jrockway at dev.catalyst.perl.org
jrockway at dev.catalyst.perl.org
Sat Jul 21 03:59:05 GMT 2007
Author: jrockway
Date: 2007-07-21 03:59:04 +0100 (Sat, 21 Jul 2007)
New Revision: 6589
Added:
trunk/Catalyst-Runtime/t/unit_utils_env_value.t
Modified:
trunk/Catalyst-Runtime/lib/Catalyst.pm
trunk/Catalyst-Runtime/lib/Catalyst/Utils.pm
Log:
add env_value patch from (Adam Herzog)++
Modified: trunk/Catalyst-Runtime/lib/Catalyst/Utils.pm
===================================================================
--- trunk/Catalyst-Runtime/lib/Catalyst/Utils.pm 2007-07-20 19:26:11 UTC (rev 6588)
+++ trunk/Catalyst-Runtime/lib/Catalyst/Utils.pm 2007-07-21 02:59:04 UTC (rev 6589)
@@ -292,7 +292,29 @@
return \%merged;
}
+=head2 env_value($class, $key)
+Checks for and returns an environment value. For instance, if $key is
+'home', then this method will check for and return the first value it finds,
+looking at $ENV{MYAPP_HOME} and $ENV{CATALYST_HOME}.
+
+=cut
+
+sub env_value {
+ my ( $class, $key ) = @_;
+
+ $key = uc($key);
+ my @prefixes = ( class2env($class), 'CATALYST' );
+
+ for my $prefix (@prefixes) {
+ if ( defined( my $value = $ENV{"${prefix}_${key}"} ) ) {
+ return $value;
+ }
+ }
+
+ return;
+}
+
=head1 AUTHOR
Sebastian Riedel, C<sri at cpan.org>
Modified: trunk/Catalyst-Runtime/lib/Catalyst.pm
===================================================================
--- trunk/Catalyst-Runtime/lib/Catalyst.pm 2007-07-20 19:26:11 UTC (rev 6588)
+++ trunk/Catalyst-Runtime/lib/Catalyst.pm 2007-07-21 02:59:04 UTC (rev 6589)
@@ -1924,15 +1924,10 @@
$dispatcher = 'Catalyst::Dispatcher::' . $dispatcher;
}
- if ( $ENV{CATALYST_DISPATCHER} ) {
- $dispatcher = 'Catalyst::Dispatcher::' . $ENV{CATALYST_DISPATCHER};
+ if ( my $env = Catalyst::Utils::env_value( $class, 'DISPATCHER' ) ) {
+ $dispatcher = 'Catalyst::Dispatcher::' . $env;
}
- if ( $ENV{ uc($class) . '_DISPATCHER' } ) {
- $dispatcher =
- 'Catalyst::Dispatcher::' . $ENV{ uc($class) . '_DISPATCHER' };
- }
-
unless ($dispatcher) {
$dispatcher = $class->dispatcher_class;
}
@@ -1958,14 +1953,10 @@
$engine = 'Catalyst::Engine::' . $engine;
}
- if ( $ENV{CATALYST_ENGINE} ) {
- $engine = 'Catalyst::Engine::' . $ENV{CATALYST_ENGINE};
+ if ( my $env = Catalyst::Utils::env_value( $class, 'ENGINE' ) ) {
+ $engine = 'Catalyst::Engine::' . $env;
}
- if ( $ENV{ uc($class) . '_ENGINE' } ) {
- $engine = 'Catalyst::Engine::' . $ENV{ uc($class) . '_ENGINE' };
- }
-
if ( $ENV{MOD_PERL} ) {
# create the apache method
@@ -2078,15 +2069,10 @@
sub setup_home {
my ( $class, $home ) = @_;
- if ( $ENV{CATALYST_HOME} ) {
- $home = $ENV{CATALYST_HOME};
+ if ( my $env = Catalyst::Utils::env_value( $class, 'HOME' ) ) {
+ $home = $env;
}
- if ( $ENV{ uc($class) . '_HOME' } ) {
- $class =~ s/::/_/g;
- $home = $ENV{ uc($class) . '_HOME' };
- }
-
unless ($home) {
$home = Catalyst::Utils::home($class);
}
@@ -2110,14 +2096,8 @@
$class->log( Catalyst::Log->new );
}
- my $app_flag = Catalyst::Utils::class2env($class) . '_DEBUG';
-
- if (
- ( defined( $ENV{CATALYST_DEBUG} ) || defined( $ENV{$app_flag} ) )
- ? ( $ENV{CATALYST_DEBUG} || $ENV{$app_flag} )
- : $debug
- )
- {
+ my $env_debug = Catalyst::Utils::env_value( $class, 'DEBUG' );
+ if ( defined($env_debug) ? $env_debug : $debug ) {
no strict 'refs';
*{"$class\::debug"} = sub { 1 };
$class->log->debug('Debug messages enabled');
Added: trunk/Catalyst-Runtime/t/unit_utils_env_value.t
===================================================================
--- trunk/Catalyst-Runtime/t/unit_utils_env_value.t (rev 0)
+++ trunk/Catalyst-Runtime/t/unit_utils_env_value.t 2007-07-21 02:59:04 UTC (rev 6589)
@@ -0,0 +1,44 @@
+use strict;
+use warnings;
+
+use Test::More tests => 5;
+
+BEGIN { use_ok("Catalyst::Utils") }
+
+##############################################################################
+### No env vars defined
+##############################################################################
+{
+ ok( !Catalyst::Utils::env_value( 'MyApp', 'Key' ),
+ 'No env values defined returns false'
+ );
+}
+
+##############################################################################
+### App env var defined
+##############################################################################
+{
+ $ENV{'MYAPP2_KEY'} = 'Env value 2';
+ is( Catalyst::Utils::env_value( 'MyApp2', 'Key' ),
+ 'Env value 2', 'Got the right value from the application var' );
+}
+
+##############################################################################
+### Catalyst env var defined
+##############################################################################
+{
+ $ENV{'CATALYST_KEY'} = 'Env value 3';
+ is( Catalyst::Utils::env_value( 'MyApp3', 'Key' ),
+ 'Env value 3', 'Got the right value from the catalyst var' );
+}
+
+##############################################################################
+### Catalyst and Application env vars defined
+##############################################################################
+{
+ $ENV{'CATALYST_KEY'} = 'Env value bad';
+ $ENV{'MYAPP4_KEY'} = 'Env value 4';
+ is( Catalyst::Utils::env_value( 'MyApp4', 'Key' ),
+ 'Env value 4', 'Got the right value from the application var' );
+}
+
More information about the Catalyst-commits
mailing list