Convert array to PHP code
Following my last post about better syntax for arrays, I've decided to use JSON format for arrays and use a method to convert them into PHP code.
Note that the ultimate goal of this is to be write a $config array in 开发者_如何学JAVAJSON and translate that into PHP code (so I can avoid having to use PHP's ugly array syntax):
The function works fine for arrays of arbitrary size and dimension, maybe I could improve it by having it automatically indent, but there isn't much more. Does anyone here any suggestions on how to make it better?
Have you seen var_export
? It looks like you've reinvented it.
Also, if you're defining your config in JSON, why are you turning it into PHP syntax? Why not just read it in as JSON, json_decode
it, and then use it as is? It seems like maintaining the data serialized in both PHP format, and JSON format is really ugly and unnecessary.
I would also echo what amber said in the comments... it seems like you've replaced the somewhat ugly, but very straightforward PHP array syntax with a much uglier hack. No offense, but this doesn't seem like a very good idea. Here's an example of a config file from the Kohana PHP framework. I don't find this file to be particularly ugly to read, and it's native PHP, so any PHP developer can work with it.
I will make one last effort to convince you not to do this. You asked for ways to improve upon your idea, and the best improvement you could make would be to not do it.
Here's is the PHP version of a config file from Kohana:
$test = array(
'default' => array(
'type' => 'mysql',
'connection' => array(
'hostname' => 'localhost',
'database' => 'kohana',
'username' => FALSE,
'password' => FALSE,
'persistent' => FALSE,
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
'profiling' => TRUE,
),
'alternate' => array(
'type' => 'pdo',
'connection' => array(
'dsn' => 'mysql:host=localhost;dbname=kohana',
'username' => 'root',
'password' => 'r00tdb',
'persistent' => FALSE,
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
'profiling' => TRUE,
),
);
And here is the JSON version:
var test = {
"default": {
"type": "mysql",
"connection": {
"hostname": "localhost",
"database": "kohana",
"username": false,
"password": false,
"persistent": false
},
"table_prefix": "",
"charset": "utf8",
"caching": false,
"profiling": true
},
"alternate": {
"type": "pdo",
"connection": {
"dsn": "mysql:host=localhost;dbname=kohana",
"username": "root",
"password": "r00tdb",
"persistent": false
},
"table_prefix": "",
"charset": "utf8",
"caching": false,
"profiling": true
}
};
They're nearly identical. I really fail to see what you're gaining.
var_export is your answer! Makes your a lot easier.
OK, after hearing all the feedback from everybody here, I've decided to make a "compromise." My main beef with the existing array syntax is its bad readability, which can certainly be improved (a lot) by using indentation.
Because I am lazy to indent (and the files I am writing are huge), I opted for JSON (or any syntax that's more readable than PHP's). I didn't make myself very clear, but another strong reason why I am using the JSON format because many other people will be looking at these config files. Most of them are not PHP-savvy and JSON is a much more human-readable format.
Unfortunately PHP code formatters/beautifiers out there don't do anything to array formatting, so I coded my own. It is based on that ugly piece of code I wrote above (and it is uglier), but it does the job.
The result is now I basically have an array beautifier, and I can generate readable native PHP code while being lazy. That's all I wanted, thanks everybody for the suggestions and pointers.
PS: Here's the beautified Kohana config array I generated with my function:
array (
'default' => array (
'type' => 'mysql',
'connection' => array (
'hostname' => 'localhost',
'database' => 'kohana',
'username' => false,
'password' => false,
'persistent' => false
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => false,
'profiling' => true
),
'alternate' => array (
'type' => 'pdo',
'connection' => array (
'dsn' => 'mysql:host=localhost;dbname=kohana',
'username' => 'root',
'password' => 'r00tdb',
'persistent' => false
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => false,
'profiling' => true
)
);
Which doesn't look bad at all.
Here is my solution for that
$data = json_decode(file_get_contents(__DIR__ . '/data.json'));
$code = var_export((array)$data, true);
$code = "<?php\n return " . preg_replace('/stdClass::__set_state/', '(object)', $code) . ';';
file_put_contents(__DIR__ . '/data.array.php', $code);
Data are taken from JSON file, but can be replaced with something else.
精彩评论