2

-- Background --

So I am working on a installer script for local dev machines and its for installing word press, What I am trying to do is have it so that the user would select a specific plug in from a drop down box and then from there select which version they want to install.

-- Issue --

So I need to be able to parse the .cfg file and store all the values into variables dynamically so that way if we add more plugins it won't break. How do I go about doing this?

Wooble
  • 87,717
  • 12
  • 108
  • 131
GhostDZ9
  • 145
  • 1
  • 6
  • 18
  • does the ".cfg file" have a specific structure? Are we talking about a standard wordpress cfg file, or something you have creative control over? etc. – horatio Apr 18 '12 at 17:12
  • Its a standard Wordpress cfg file, when i use parse_ini_file the output is a multidimensional array – GhostDZ9 Apr 18 '12 at 17:25

2 Answers2

3

Couple of ways, depending on how you wish to store the config data. The fastest way is to store the data as an .ini file and parse it using PHP's built in parse_ini_file. You could also store the data in other formats such as XML and YAML, but I wouldn't recommend XML as you probably won't be transporting the data betweeen disparate systems, also XML tends to be harder to read with all the extraneous tags (vs yaml or ini).

I personally prefer yaml and use Symfony's Yaml Component parser which will parse the yaml file into an array. Symfony's Yaml Component can be used outside of the Symfony framework. You could also look into Zend's Yaml parser as well.

After you pick the format and parser you wish to use, it's as easy as storing the config file somewhere that is accessible by your webserver, requiring it, and passing it through the parser API. After it is parsed you should have access to the values via an array.

-- Update --

<?php

$settings = parse_ini_file('test.ini');

var_dump($settings);

Results:

array(41) {
["plugins"]=>
string(0) ""
["breadcrumb-navxt"]=>
string(5) "4.0.1"
["bp-moderation"]=>
string(5) "0.1.4"
["buddypress-activity-stream-hashtags"]=>
string(5) "0.4.0"
["buddypress-group-documents"]=>
string(5) "0.3.5"
["buddypress-group-email-subscription"]=>
string(5) "2.9.1"
["buddypress-links"]=>
string(3) "0.5"
["buddypress"]=>
string(6) "1.2.10"
["calendar"]=>
string(5) "1.3.1"
["collapsing-pages"]=>
string(5) "0.6.1"

This appears to work as expected, so if I want the version number of the calendar plug-in I would just do:

var_dump($settings['calendar']);

To store in dynamic variables:

$settings = parse_ini_file('test.ini');

foreach ($settings as $key => $setting) {
    // Notice the double $$, this tells php to create a variable with the same name as key
    $$key = $setting; 
}

var_dump($calendar);
Mike Purcell
  • 19,847
  • 10
  • 52
  • 89
  • I would rather not use any third party frameworks, I want to do it completely through php – GhostDZ9 Apr 18 '12 at 17:22
  • Then PHP's built in `parse_ini_file`, may be your best bet. – Mike Purcell Apr 18 '12 at 17:24
  • I have tried using the parse_ini_file but every time i can only get the version numbers, not the name of the plugins and its all in on array I want to store those values into individual variables – GhostDZ9 Apr 18 '12 at 17:29
  • Post your ini file contents and lets see what's going on. You should really mention what you have already tried etc otherwise questions like this morph into something completely different than the original question asked. – Mike Purcell Apr 18 '12 at 17:32
  • I just ran your ini settings through `parse_ini_file` and it created an array where the plugin names are the keys of the array. I posted a snippet in my answer. – Mike Purcell Apr 18 '12 at 17:42
  • Ok now how do i get "calendar" to be be stored into a variable and the version in another? – GhostDZ9 Apr 18 '12 at 17:49
  • Is there a way to remove the the data type and length from the display cause i get things like string(13) "plugins – GhostDZ9 Apr 18 '12 at 17:58
  • Yes, don't use `var_dump` that's just to illustrate that the variable houses data and datatype. `echo $calendar`. – Mike Purcell Apr 18 '12 at 18:02
0

Could you store the file in json or php serialised format?

Andrew Hall
  • 3,058
  • 21
  • 30