The easiest way to parameterize / configure a php application | user friendly data serialization

When development time matters, everything that others can help is the goal. My PHP application is now parameterized and configured with an included file that contains an array in the form:

$config = array(
   'company'            => 'BMC' ,       // the visible company name
   'aplicable_tax'      => .21   ,       // the IVA tax
   'context_arr'        => array(
        'case1'             =>    12,    // the defalul value
        'case2'             =>    13,
        'case3'             =>    14
                           ),
   'EN_welcome_text'       => 'hello',   // do NOT translate on regionalization

   // xx comparation matrix
   'xx_maxref'=> 5,
   'xx_range' => array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
   'xx_comp'  => array( 
    //  V Other V  > I >>   0, 1, 2, 3, 4, 5, 6, 7, 8, 9
        /*  0 */     array( 0, 3, 5, 5, 5, 5, 5, 5, 5, 5),
        /*  1 */     array(-3, 0, 3, 5, 5, 5, 5, 5, 5, 5),
        /*  2 */     array(-5,-3, 0, 3, 5, 5, 5, 5, 5, 5),
        /*  3 */     array(-5,-5,-3, 0, 3, 5, 5, 5, 5, 5),
        /*  4 */     array(-5,-5,-5,-3, 0, 3, 5, 5, 5, 5),
        /*  5 */     array(-5,-5,-5,-5,-3, 0, 3, 5, 5, 5),
        /*  6 */     array(-5,-5,-5,-5,-5,-3, 0, 3, 5, 5),
        /*  7 */     array(-5,-5,-5,-5,-5,-5,-3, 0, 3, 5),
        /*  8 */     array(-5,-5,-5,-5,-5,-5,-5,-3, 0, 3),
        /*  9 */     array(-5,-5,-5,-5,-5,-5,-5,-5,-3, 0),
),


// and so on
// and so on
// and so on
)

But this approach is unsafe, because any authorized editor can enter PHP code or errors.

My questions:

  • Can you offer a simple and flexible format to give three parties the ability to parameterize a PHP application?
  • Is there a script conversion from this format to PHP?
+5
source share
7 answers

XML YAML. YAML / -. , YAML . symfony: http://fabien.potencier.org/article/40/the-state-of-yaml-in-php

:

company: BMC         #the visible company name
aplicable_tax: 0.21  #the IVA tax
context_arr:
    case1: 12       #the defalul value
    case2: 13
    case3: 14
EN_welcome_text: hello #do NOT translate on regionalization

#xx comparation matrix
xx_maxref: 5
xx_range: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
xx_comp:
# V Other V  > I >>   0, 1, 2, 3, 4, 5, 6, 7, 8, 9
    -[  0, 3, 5, 5, 5, 5, 5, 5, 5, 5 ] # [ 0 ]
    -[ -3, 0, 3, 5, 5, 5, 5, 5, 5, 5 ] # [ 1 ]
    -[ -5,-3, 0, 3, 5, 5, 5, 5, 5, 5 ] # [ 2 ]
    -[ -5,-5,-3, 0, 3, 5, 5, 5, 5, 5 ] # [ 3 ]
    -[ -5,-5,-5,-3, 0, 3, 5, 5, 5, 5 ] # [ 4 ]
    -[ -5,-5,-5,-5,-3, 0, 3, 5, 5, 5 ] # [ 5 ]
    -[ -5,-5,-5,-5,-5,-3, 0, 3, 5, 5 ] # [ 6 ]
    -[ -5,-5,-5,-5,-5,-5,-3, 0, 3, 5 ] # [ 7 ]
    -[ -5,-5,-5,-5,-5,-5,-5,-3, 0, 3 ] # [ 8 ]
    -[ -5,-5,-5,-5,-5,-5,-5,-5,-3, 0 ] # [ 9 ]

YAML XML .

+2

, , , .

, , , , , - .

  • , (, , ) . , , .
  • Encoding Solution - JSON, Serialization INI - , . . . .
  • Database + Encoding Solution .

- , . PHP . , . .

PHP CONFIG

PHP, INI . PHP . - , .. - parse_ini_file(). /, , . , - , INI .

nD

parse_ini_file() , . , , , , , (.. ).

+4

JSON PHP, json_encode json_decode. :

{
    "company" : "BMC",
    "aplicable_tax" : 0.21,
    "context_arr" :
    {
        "case1" : 12,
        "case2" : 13,
        "case3" : 14
    },
    "EN_welcome_text" : "hello"
}

, (, ) .

+2

DB, , . , .

- , .

+1

XML - , , , . , , IDE .
- SimpleXML ( ).

0

, .ini .xml , :

  • $config = array ( ); , = >
  • - :

    eval ( "$ config = array (" + file_get_contents ('config.file') + ");" );

config.file, - , /uncommented ; , . eval.

However, if you do not trust a third party, so as not to supply a dangerous include file, you need to choose a different approach. Can you provide a third-party web interface that sends you configuration files? Comments / hints will be displayed on screen in HTML, but you will have good, safe JSON for parsing.

0
source

All Articles