Skip to content

Commit af0c2b2

Browse files
committed
Initial commit. ⚡️
1 parent 632becf commit af0c2b2

8 files changed

+2114
-1
lines changed

.gitignore

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# Bower dependency directory (https://bower.io/)
27+
bower_components
28+
29+
# node-waf configuration
30+
.lock-wscript
31+
32+
# Compiled binary addons (https://nodejs.org/api/addons.html)
33+
build/Release
34+
35+
# Dependency directories
36+
node_modules/
37+
jspm_packages/
38+
39+
# Typescript v1 declaration files
40+
typings/
41+
42+
# Optional npm cache directory
43+
.npm
44+
45+
# Optional eslint cache
46+
.eslintcache
47+
48+
# Optional REPL history
49+
.node_repl_history
50+
51+
# Output of 'npm pack'
52+
*.tgz
53+
54+
# Yarn Integrity file
55+
.yarn-integrity
56+
57+
# dotenv environment variables file
58+
.env

README.md

+50-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,51 @@
11
# php-arrays
2-
Parse PHP array-like configuration files in JavaScript.
2+
3+
Parse PHP array-like configuration files into JavaScript objects.
4+
5+
Sometimes it is useful to share your PHP application configuration with, for example, your JavaScript build system. Let's say the application has a theme configuration key, instead of duplicating the same key in JavaScript configuration, this package allows to transform the PHP array-like configuration file into JSON.
6+
7+
## Quick Example
8+
9+
```php
10+
<?php
11+
12+
return [
13+
'foo' => 'bar',
14+
];
15+
```
16+
17+
```js
18+
let parser = require('php-arrays');
19+
20+
parser.parse('../path/to/file.php', (result) => {
21+
console.log(result); // { foo: "bar" }
22+
}, (error) => {
23+
// Handle error..
24+
});
25+
26+
let result = parser.parseSync('../path/to/file.php', (error) => {
27+
return 'this will be the value of the result variable if an error occurs.';
28+
});
29+
```
30+
31+
## Installing
32+
33+
Run `npm install oxy/php-arrays --save-dev`.
34+
35+
## API
36+
37+
### parse(file : string, callback : function, onError? : function)
38+
39+
The parse function parses the given file asynchronously. Whenever the parsing of the file is successful, the callback function will be triggered, otherwise, the optional onError function is called.
40+
41+
### parseSync(file : string, onError? : function)
42+
43+
The parseSync function parses the given file synchronously. If the parsing of the file provokes an error, the optional onError function is called. This function returns the PHP array as JSON or the value returned by the onError function.
44+
45+
### command(file : string)
46+
47+
This function allows to sanitize the file input and also generate the command to parse the file. Therefore, it returns the generated command.
48+
49+
## Contributing
50+
51+
If you happen to find an error, or you might be thinking about a general improvement to the project, please do create an Issue, or consider creating a Pull Request. All contributions are appreciated.

index.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// @ts-check
2+
'use strict';
3+
4+
/**
5+
* Generates the command that is necessary to parse the given file.
6+
*
7+
* @param {string} file PHP configuration file path (relative or absolute).
8+
*/
9+
exports.command = function (file) {
10+
file = file.replace(/(["\s'$`\\])/g, '\\$1');
11+
12+
return `$(which php) -n -d display_errors=0 -r \'echo json_encode(include("${file}"));\'`
13+
};
14+
15+
/**
16+
* Parses the given file asynchronously, passing the result to the callback
17+
* function. In case of failure, it triggers the optional onError callback
18+
* to handle the error.
19+
*
20+
* @param {string} file PHP configuration file path (relative or absolute).
21+
* @param {function(object, Error=, string=, string=)} callback Function that will be triggered upon parsing the file.
22+
* @param {function(string)=} onError Function that will be triggered upon an error during the parsing of the file.
23+
*/
24+
exports.parse = function (file, callback, onError = (error) => {}) {
25+
let sh = require('child_process');
26+
27+
sh.exec(this.command(file), function (err, stdout, stderr) {
28+
try {
29+
let result = JSON.parse(stdout.trim());
30+
31+
if (result) {
32+
return callback(result, err, stdout, stderr);
33+
}
34+
35+
throw new Error('Failed to parse the given file because: ' + result);
36+
} catch (error) {
37+
onError(error);
38+
}
39+
});
40+
};
41+
42+
/**
43+
* Parses the given file synchronously, returning the result.
44+
* In case of failure, it triggers the optional onError callback to handle the error.
45+
*
46+
* @param {string} file PHP configuration file path (relative or absolute).
47+
* @param {function(*)=} onError Function that will be triggered upon an error during the parsing of the file.
48+
*/
49+
exports.parseSync = function (file, onError = (error) => {}) {
50+
let sh = require('child_process');
51+
52+
try {
53+
let result = JSON.parse(
54+
sh.execSync(
55+
this.command(file)
56+
).toString().trim()
57+
);
58+
59+
if (result) {
60+
return result;
61+
}
62+
63+
throw new Error('Failed to parse the given file because: ' + result);
64+
} catch (error) {
65+
return onError(error);
66+
}
67+
}

0 commit comments

Comments
 (0)