Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Cvs P5ee > [svn:p5ee] r101...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 607 of 694
Post > Topic >>

[svn:p5ee] r10188 - in p5ee/trunk/App-Context: lib/App t

by spadkins@[EMAIL PROTECTED] Nov 7, 2007 at 09:20 AM

Author: spadkins
Date: Wed Nov  7 09:20:07 2007
New Revision: 10188

Modified:
   p5ee/trunk/App-Context/lib/App/Context.pm
   p5ee/trunk/App-Context/t/App.t

Log:
add global include support

Modified: p5ee/trunk/App-Context/lib/App/Context.pm
==============================================================================
--- p5ee/trunk/App-Context/lib/App/Context.pm	(original)
+++ p5ee/trunk/App-Context/lib/App/Context.pm	Wed Nov  7 09:20:07 2007
@[EMAIL PROTECTED]
 -250,9 +250,17 @[EMAIL PROTECTED]
         $self->dbgprint(join("",@[EMAIL PROTECTED]
));
     }
 
+    ##############################################################
+    # initialize conf
+    ##############################################################
     my $conf = {};
+
     eval {
+
+        # Initialize from "app.pl" or other file/source specified by the
class
         $conf = App->new($conf_class, "new", \%options);
+
+        # Override any values which are supplied in "app.conf" (the
"deployment descriptor")
         foreach my $var (keys %options) {
             if ($var =~ /^app\.(.+)/) {
                 $conf->set($1, $options{$var});
@[EMAIL PROTECTED]
 -260,8 +268,53 @[EMAIL PROTECTED]
         }
     };
     $self->add_message($@[EMAIL PROTECTED]
) if ($@[EMAIL PROTECTED]
);
+
     $self->{conf} = $conf;
 
+    ##############################################################
+    # Include and Overlay $conf with additional files
+    ##############################################################
+    my ($includes);
+    $includes = $conf->{global}{include} if ($conf->{global});
+    if ($includes && ref($includes) eq "ARRAY") {
+        my $options = $self->{options};
+        my $prefix  = $options->{prefix};
+        my (@[EMAIL PROTECTED]
 $cond, $include_files, $matches);
+        for (my $i = 0; $i <= $#$includes; $i += 2) {
+            $cond = $includes->[$i];
+            $include_files = $includes->[$i+1];
+            $matches = $self->cond_matches_options($cond, $options);
+            if ($matches) {
+                if (ref($include_files) eq "ARRAY") {
+                    @[EMAIL PROTECTED]
 = @[EMAIL PROTECTED]
                }
+                elsif (ref($include_files) eq "") {
+                    @[EMAIL PROTECTED]
 = ( $include_files );
+                }
+                foreach my $conf_file (@[EMAIL PROTECTED]
) {
+                    $conf_file = "$prefix/etc/app/$conf_file" if
($conf_file !~ m!^/!);
+                    if ($self->{conf_included}{$conf_file}) {
+                        print STDERR "Conf global include:
[$cond][$conf_file] already included\n" if ($options{debug_conf});
+                        next;
+                    }
+                    if (-r $conf_file) {
+                        my $aux_conf = $conf_class->create({ conf_file =>
$conf_file });
+                        $conf->overlay($aux_conf);
+                        print STDERR "Conf global include:
[$cond][$conf_file] included (overlayed)\n" if ($options{debug_conf});
+                    }
+                    else {
+                        print STDERR "Conf global include:
[$cond][$conf_file] not readable\n" if ($options{debug_conf});
+                    }
+                    $self->{conf_included}{$conf_file} = 1;
+                }
+            }
+            print STDERR "Conf global include: [$cond] did not match
options\n" if (!$matches && $options{debug_conf});
+        }
+    }
+
+    ##############################################################
+    # misc
+    ##############################################################
     if ($options{debug_conf} >= 2) {
         $self->dbgprint($self->{conf}->dump());
     }
@[EMAIL PROTECTED]
 -288,6 +341,43 @[EMAIL PROTECTED]
     return("App::Session");
 }
 
+# NOTE: This is very similar logic to some logic in App::Options to see
if sections
+#       of app.conf are active.
+sub cond_matches_options {
+    &App::sub_entry if ($App::trace);
+    my ($self, $cond_str, $options) = @[EMAIL PROTECTED]
    my ($var, $value, $regexp, $cond, $cond_value);
+    my $matches = 1;                      # assume the condition matches
+    my @[EMAIL PROTECTED]
 = split(/;/,$cond_str);      # separate the conditions that
must be satisfied
+    foreach $cond (@[EMAIL PROTECTED]
) {  # check each condition
+        if ($cond =~ /^([^=]+)=(.*)$/) {  # i.e. city=ATL or
name=/[Ss]tephen/
+            $var = $1;
+            $cond_value = $2;
+        }
+        else {              # i.e. [go] matches the program (app) named
"go"
+            $var = "app";
+            $cond_value = $cond;
+        }
+        if ($cond_value =~ m!^/(.*)/$!) {  # variable's value must match
the regexp
+            $regexp = $1;
+            $value = $options->{$var};
+            $value = "" if (!defined $value);
+            $matches = ($value =~ /$regexp/) ? 1 : 0;
+        }
+        elsif ($var eq "app" && ($cond_value eq "" || $cond_value eq
"ALL")) {
+            $matches = 1;   # "" and "ALL" are special wildcards for the
"app" variable
+        }
+        else {  # a variable's value must match exactly
+            $value = $options->{$var};
+            $value = "" if (!defined $value);
+            $matches = ($value eq $cond_value) ? 1 : 0;
+        }
+        last if (!$matches);
+    }
+    &App::sub_exit($matches) if ($App::trace);
+    return($matches);
+}
+

#############################################################################
 # PROTECTED METHODS

#############################################################################

Modified: p5ee/trunk/App-Context/t/App.t
==============================================================================
--- p5ee/trunk/App-Context/t/App.t	(original)
+++ p5ee/trunk/App-Context/t/App.t	Wed Nov  7 09:20:07 2007
@[EMAIL PROTECTED]
 -23,7 +23,7 @[EMAIL PROTECTED]
 $config = App->conf("conf_file" => "$dir/app.pl");
 ok(defined $config, "constructor ok");
 isa_ok($config, "App::Conf", "right class");
-is_deeply($conf, { %$config }, "config to depth");
+is_deeply({ %$config }, $conf, "config to depth");
 
 ########################################################
 # use()




 1 Posts in Topic:
[svn:p5ee] r10188 - in p5ee/trunk/App-Context: lib/App t
spadkins@[EMAIL PROTECTED  2007-11-07 09:20:08 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue May 13 21:45:07 CDT 2008.