Skip to content

Commit 396aef8

Browse files
committed
Avoid calling PHP directly in build process.
1 parent 8eb4dc9 commit 396aef8

File tree

3 files changed

+23
-26
lines changed

3 files changed

+23
-26
lines changed

package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"grunt-webpack": "7.0.1",
6767
"install-changed": "1.1.0",
6868
"json2php": "0.0.12",
69+
"php-array-reader": "2.1.3",
6970
"postcss": "8.5.8",
7071
"prettier": "npm:wp-prettier@3.0.3",
7172
"qunit": "~2.25.0",

tools/gutenberg/copy.js

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
* @package WordPress
1010
*/
1111

12-
const child_process = require( 'child_process' );
1312
const fs = require( 'fs' );
1413
const path = require( 'path' );
1514
const json2php = require( 'json2php' );
15+
const { fromString } = require( 'php-array-reader' );
1616

1717
// Paths.
1818
const rootDir = path.resolve( __dirname, '../..' );
@@ -78,36 +78,14 @@ const COPY_CONFIG = {
7878
* Given a path to a PHP file which returns a single value, converts that
7979
* value into a native JavaScript value (limited by JSON serialization).
8080
*
81-
* @throws Error when PHP source file unable to be read, or PHP is unavailable.
81+
* @throws Error when PHP source file unable to be read or parsed.
8282
*
8383
* @param {string} phpFilepath Absolute path of PHP file returning a single value.
8484
* @return {Object|Array} JavaScript representation of value from input file.
8585
*/
8686
function readReturnedValueFromPHPFile( phpFilepath ) {
87-
const results = child_process.spawnSync(
88-
'php',
89-
[ '-r', '$path = file_get_contents( "php://stdin" ); if ( ! is_file( $path ) ) { die( 1 ); } try { $data = require $path; } catch ( \\Throwable $e ) { die( 2 ); } $json = json_encode( $data ); if ( ! is_string( $json ) ) { die( 3 ); } echo $json;' ],
90-
{
91-
encoding: 'utf8',
92-
input: phpFilepath,
93-
}
94-
);
95-
96-
switch ( results.status ) {
97-
case 0:
98-
return JSON.parse( results.stdout );
99-
100-
case 1:
101-
throw new Error( `Could not read PHP source file: '${ phpFilepath }'` );
102-
103-
case 2:
104-
throw new Error( `PHP source file did not return value when imported: '${ phpFilepath }'` );
105-
106-
case 3:
107-
throw new Error( `Could not serialize PHP source value into JSON: '${ phpFilepath }'` );
108-
}
109-
110-
throw new Error( `Unknown error while reading PHP source file: '${ phpFilepath }'` );
87+
const content = fs.readFileSync( phpFilepath, 'utf8' );
88+
return fromString( content );
11189
}
11290

11391
/**

0 commit comments

Comments
 (0)