代码之家  ›  专栏  ›  技术社区  ›  neophytte

在Perl文件中包含PHP配置值

  •  0
  • neophytte  · 技术社区  · 7 年前

    我有一个PHP应用程序 代码点火器(v2.x) 它有一个配置文件,它有一个相当标准的PHP格式的值,例如:

    $config['key1'] = 'value1';
    $config['key2'] = 'value2';
    

    我正在编写一个Perl程序,每晚运行一次,将信息解析到数据库中供PHP使用/显示,我可以让Perl程序从PHP读取配置值吗?

    我已经试过了 PHP::Include module ,但生成的Perl值似乎为空(使用调试)模块。我也试过了 Config::IniFiles module ini 文件

    2 回复  |  直到 7 年前
        1
  •  3
  •   Stephanie Temple    7 年前

    如果是这样一个简单的配置文件。。。

        <?php
    
    /**
    *
    *  service: /www/vhosts/x456/docs/config.php
    *  program: community one
    *  version: 1.01, 01/01/2009 01:01:01
    *
    **/
    
    $config                  = array ();
    
    $config['system_admin']  = 1;
    
    $config['system_name']   = 'Community One';
    
    $config['session_name']  = 'community';
    
    $config['system_email']  = '@gmail.com';
    
    $config['system_allow']  = '127.0.0.1';
    
    $config['storage_type']  = 'file';
    
    $config['host_name']     = '';
    
    $config['service_path']  = '/';
    
    $config['system_root']   = 'c:/services/www/vhosts/x456/';
    
    $config['document_root'] = 'c:/services/www/vhosts/x456/docs/';
    
    $config['database_name'] = 'community';
    
    $config['database_host'] = 'localhost';
    
    $config['database_user'] = 'admin';
    
    $config['database_pass'] = '';
    
    $config['database_type'] = 'mysqli';
    
    $config['database_port'] = '3306';
    
    $config['database_salt'] = '*^&$_';
    
    $config['min_timeout']   = 180;
    
    $config['max_timeout']   = 31557600;
    
    $config['ssl_only']      = FALSE;
    
    ?>
    

    你可以用我的旧剧本。。。

    use strict;
    
    use warnings;
    
    use Data::Dumper qw ( Dumper );
    
    #error log location and name
    
    my $log = 'C:/services/www/log/errors.txt';
    
    # the hash container
    
    my %data;
    
    # the config file processor
    
    sub configFile
    {
        # grab the config files location
    
        my $file = shift;
    
        # open the config file up
    
        open ( CONFIG, '<', $file ) or logError ( 0, $file );
    
        # read the config file, line by line
    
        while ( my $line = <CONFIG> )
        {
            # lose the eol and any spaces  found >> (^|$) 
    
            $line = trim ( $line );
    
            # move to next line if we don't have a $var
    
            next if $line !~ /^\$/;
    
            # move to the next line if there is no
            # (key = value) pair
    
            my $find  = index $line, '=';
    
            # grab the array & key name(s) + clean them up
    
            my $name  = rtrim ( substr $line, 0, $find );
    
            # grab the value + clean it up
    
            my $value = ltrim ( substr $line, ( $find + 1 ) );
    
            # final check, skip over $var(s) = array(), []
            # process only $var['key'] = values!
    
            next if ( my $pos = index $name, '[' ) == -1;
    
            # set the (hash key) name = $(hash key)
    
            my $hash  = substr $name, 1,  ( $pos - 1 );
    
            # set the variables (key) name = $(hash key)[(key)]
    
            my $key   = substr $name, ( $pos + 2 ), -2;
    
            # strip ('|'|;) from the variable(s) value
    
            if ( $value =~ m/^'|^"/ )
            {
                $value = substr $value, 1, -2;
            }
            else
            {
                $value = substr $value, 0, -1;
            }
    
            # add the varaible to the data hash
    
            $data{$hash}{$key} = $value;
        }
    
        # done, close it
    
        close CONFIG;
    }
    
    sub logError ( )
    {
        my ( $type, $data ) = @_;
    
        open ( ERRORS, '>>', $log );
    
        print ERRORS $type . ", " . $data . "\n";
    
        close ERRORS;
    
        exit ( 0 );
    }
    
    
    sub ltrim
    {
        my $s = shift;
    
        $s =~ s/^\s+//;
    
        return $s
    };
    
    sub rtrim
    {
        my $s = shift;
    
        $s =~ s/\s+$//;
    
        return $s
    };
    
    sub trim
    {
        my $s = shift;
    
        $s =~ s/^\s+|\s+$//g;
    
        return $s
    };
    
    
    # the php config style file to process
    
    configFile ( 'C:\\services\\www\\config.php' );
    
    # just print the $data hash out so you
    # can see what it returns
    
    print Dumper \%data;
    

    $VAR1 = {
              'config' => {
                            'ssl_only' => 'FALSE',
                            'database_salt' => '*^&$_',
                            'system_allow' => '127.0.0.1',
                            'system_email' => '@gmail.com',
                            'max_timeout' => '31557600',
                            'database_type' => 'mysqli',
                            'database_pass' => '',
                            'storage_type' => 'file',
                            'database_user' => 'admin',
                            'min_timeout' => '180',
                            'system_admin' => '1',
                            'system_name' => 'Community One',
                            'system_root' => 'c:/services/www/vhosts/x456/',
                            'database_host' => 'localhost',
                            'database_port' => '3306',
                            'session_name' => 'community',
                            'service_path' => '/',
                            'database_name' => 'community',
                            'host_name' => '',
                            'document_root' => 'c:/services/www/vhosts/x456/docs/'
                          }
            };
    
        2
  •  0
  •   Sherif Salah    7 年前

    是的,您可以将配置数组转换为与语言无关的格式,如json:

    $json = json_encode($this->config->config);
    

    现在您已经有了json格式的配置数组,您可以将其保存到文件中,或者直接从程序中通过ajax请求检索它,或者对其执行任何操作。