]> Frank Brehm's Git Trees - cookbook.git/commitdiff
Neu dazu
authorFrank Brehm <frank@brehm-online.com>
Tue, 7 Aug 2007 17:54:00 +0000 (17:54 +0000)
committerFrank Brehm <frank@brehm-online.com>
Tue, 7 Aug 2007 17:54:00 +0000 (17:54 +0000)
git-svn-id: http://svn.brehm-online.com/svn/cookbook/trunk@6 191103c4-1d37-0410-b3e5-d8c2315c0aac

lib/CookBook/Plugin/ConfigLoader.pm [new file with mode: 0644]

diff --git a/lib/CookBook/Plugin/ConfigLoader.pm b/lib/CookBook/Plugin/ConfigLoader.pm
new file mode 100644 (file)
index 0000000..603172d
--- /dev/null
@@ -0,0 +1,94 @@
+package CookBook::Plugin::ConfigLoader;
+
+# $Id: ConfigLoader.pm 61 2007-06-11 16:45:13Z fbrehm $
+# $URL$
+
+use base "Catalyst::Plugin::ConfigLoader";
+use CookBook::Common;
+use Catalyst::Log::Log4perl;
+
+my %LogLevels = (
+    'FATAL' => 1,
+    'ERROR' => 1,
+    'WARN'  => 1,
+    'INFO'  => 1,
+    'DEBUG' => 1,
+);
+
+=head2 finalize_config
+
+This method is called after the config file is loaded. It can be
+used to implement tuning of config values that can only be done
+at runtime. If you need to do this to properly configure any
+plugins, it's important to load ConfigLoader before them.
+ConfigLoader provides a default finalize_config method which
+walks through the loaded config hash and replaces any strings
+beginning containing C<__HOME__> with the full path to
+app's home directory (i.e. C<$c-E<gt>path_to('')> ).
+You can also use C<__path_to(foo/bar)__> which translates to
+C<$c-E<gt>path_to('foo', 'bar')>
+
+=cut
+
+sub finalize_config {
+
+    my $c = shift;
+    my $K = __PACKAGE__ . "::finalize_config(): ";
+
+    #$c->log->debug( get_output_string( $K, "aufgerufen." ) ) if $c->config->{'debug_level'} >= 2;
+
+    $c->SUPER::finalize_config();
+
+    # Vorgabe-View festlegen (kann mit $c->stash->{'current_view_instance'} oder $c->stash->{'current_view'} ueberschrieben werden.)
+    $c->config()->{'default_view'} = 'TT' unless $c->config()->{'default_view'};
+
+    my $log4perlconf = "";
+    my $log_level = uc( $c->config()->{'log_level'} || 'info' );
+    $log_level = 'INFO' unless $LogLevels{$log_level};
+
+    if ( $ENV{'CATALYST_DEBUG'} or $ENV{'COOKBOOK_DEBUG'} ) {
+        $log_level = 'DEBUG';
+    }
+    else {
+        $log_level = 'INFO' if $log_level eq 'DEBUG';
+    }
+
+    $log4perlconf .= 'log4perl.rootLogger=' . $log_level . ", A1\n";
+    if ( $c->config()->{'colored_log'} ) {
+        $log4perlconf .= "log4perl.appender.A1=Log::Log4perl::Appender::ScreenColoredLevels\n";
+    }
+    else {
+        $log4perlconf .= "log4perl.appender.A1=Log::Log4perl::Appender::Screen\n";
+    }
+    $log4perlconf .= "log4perl.appender.A1.layout=PatternLayout\n";
+    $log4perlconf .= "log4perl.appender.A1.layout.ConversionPattern=[%d] [CookBook] [%p] %m%n\n";
+
+    $c->log->debug( $K . "Log4perl-Konfiguration:\n" . $log4perlconf ) if $c->config->{'debug_level'} >= 1;
+    $c->log( Catalyst::Log::Log4perl->new( \$log4perlconf ) );
+
+    my $dsn = 'dbi:mysql:host=%s;database=%s';
+
+    $dsn = sprintf( $dsn, ( $c->config()->{'database'}->{'host'} || 'localhost' ),
+        ( $c->config()->{'database'}->{'schema'} || 'cookbook' ) );
+
+    my $port = 3306;
+    if ( $c->config()->{'database'}->{'port'} and to_int( $c->config()->{'database'}->{'port'} ) ) {
+        $port = $c->config()->{'database'}->{'port'};
+    }
+    $port = $port == 3306 ? '' : ';port=' . $port;
+    $dsn .= $port;
+
+    $c->config()->{'dsn'}       = $dsn;
+    $c->config()->{'db_user'}   = $c->config()->{'database'}->{'user'} || 'cookbook';
+    $c->config()->{'db_passwd'} = $c->config()->{'database'}->{'passwd'} || '';
+
+    $ENV{'COOKBOOK_DSN'}  = $dsn;
+    $ENV{'COOKBOOK_USER'} = $c->config()->{'db_user'};
+    $ENV{'COOKBOOK_PWD'}  = $c->config()->{'db_passwd'};
+
+    $c->log->debug( get_output_string( $K, "Aktuelle Konfiguration: ", $c->config ) ) if $c->config->{'debug_level'} >= 3;
+
+} ## end sub finalize_config
+
+1;
+