简体   繁体   English

在Perl文件中包含PHP配置值

[英]Include a PHP configuration value in a Perl file

I have a PHP application in CodeIgniter (v2.x) which has a configuration file, this has values in a fairly standard PHP format, such as: 我在CodeIgniter(v2.x)中有一个PHP应用程序,它具有一个配置文件,该文件具有相当标准的PHP格式的值,例如:

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

I'm writing a Perl program to be run on a nightly basis to parse information into a database for the PHP to use/display, can I get the Perl program to read the configuration values from PHP? 我正在编写一个Perl程序,该程序每晚运行一次,以将信息解析到数据库中供PHP使用/显示,我能否让Perl程序从PHP读取配置值?

I've tried to PHP::Include module , but the Perl values generated appears to be blank (using the DEBUG) module. 我尝试使用PHP::Include module ,但是生成的Perl值似乎为空(使用DEBUG)模块。 I've also tried the Config::IniFiles module , but my source is not configured as an ini file. 我也尝试了Config::IniFiles module ,但是我的源没有配置为ini文件。

Thanks 谢谢

If its a simple config file like so... 如果它是一个像这样的简单配置文件...

    <?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;

?>

You could use this old script that I have... 您可以使用我拥有的这个旧脚本...

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;

The result would be.... 结果将是...。

$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/'
                      }
        };

Yes you can convert you config array to a language independent format like json: 是的,您可以将配置数组转换为与语言无关的格式,例如json:

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

Now that you have your config array in json format, you can save it to a file or retrieve it with an ajax request directly from your program or do whatever you wanna do with it. 现在您已经有了json格式的配置数组,您可以将其保存到文件中,或者直接从程序中通过ajax请求检索它,或者执行任何您想使用的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM