Cleanest way to read config settings from PHP file and upload entire project code using shell script

Deadly 提交于 2019-12-24 08:53:32

问题


I am a newbie with shell scripting so need a few ideas on parsing a PHP file using a shell script.

Ours is a PHP project and I am improving our shell script which is used to upload code to production server.

There is one PHP config file production.settings.php which needs to be read during upload, for a few constants -

BASE_PATH (path to project root on prod server)
db_host, db_name etc. (database settings of prod database - to be used for taking a backup of the database before upload)

Question

  • How to read the value of the constants?

    They are defined like this:

    define("BASE_PATH","/path/to/project/root");
    
  • How to read the first uncommented value of the constant?
    Note - The constant may be defined more than once in the same file (let's assume the possibilty - this may happen by mistake or there may be commented instances of the line)

So far I am only able to get the number of lines containing the string define("BASE_PATH" using grep in my shell script -

cd ..
PROJECT_ROOT=$PWD
result= grep -ic 'define("BASE_PATH",' $PROJECT_ROOT'/config/main.settings.php'
echo "see"$result
  • Is this method of parsing good enough or a yml file would be better? Is there any shell command/snippet for doing this so that I can get the result by writing lesser amount of code?

Updates
Check my other questions for more details on this:-
Manipulating an array (printed by php-cli) in shell script,
Assigning values printed by PHP CLI to shell variables,
Initiating dynamic variables (variable variables) in bash shell script


回答1:


just do it using the php, then call your shell script to invoke the php script.

Assuming you have your bunch of defines defined in defs.php:

define('NAME', 'JOHN');
define('HOBBY', 'FISHING');

then create a php script get_defs.php:

require_once 'defs.php';
$const = get_defined_constants(true);
foreach($const['user'] as $k => $v) {
   echo "export $k=$v";
}

then in your shell script, run it like so:

`php get_defs.php`

What happen is, get_defs.php will output bunch of export KEY=VALUE, then shell will run those commands outputted by your php get_defs.php.




回答2:


Why don't you just code with PHP CLI? That's what you understand? Also maybe you could put constants in a ini file and read them?




回答3:


If youre comforttable with PHP then use PHP to write the shell script. IF you go this route i would move all config settings to a config file... INI, YAML, XML, whetever floats your boat. Then i would modify the bootstrap of the application that defines your constants to also read from this config file. That way you can use it in botht your script and the app without having to change it.



来源:https://stackoverflow.com/questions/4374345/cleanest-way-to-read-config-settings-from-php-file-and-upload-entire-project-cod

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!