|
9 | 9 | * @package WordPress |
10 | 10 | */ |
11 | 11 |
|
12 | | -const child_process = require( 'child_process' ); |
13 | 12 | const fs = require( 'fs' ); |
14 | 13 | const path = require( 'path' ); |
15 | 14 | const json2php = require( 'json2php' ); |
| 15 | +const { fromString } = require( 'php-array-reader' ); |
16 | 16 |
|
17 | 17 | // Paths. |
18 | 18 | const rootDir = path.resolve( __dirname, '../..' ); |
@@ -78,36 +78,14 @@ const COPY_CONFIG = { |
78 | 78 | * Given a path to a PHP file which returns a single value, converts that |
79 | 79 | * value into a native JavaScript value (limited by JSON serialization). |
80 | 80 | * |
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. |
82 | 82 | * |
83 | 83 | * @param {string} phpFilepath Absolute path of PHP file returning a single value. |
84 | 84 | * @return {Object|Array} JavaScript representation of value from input file. |
85 | 85 | */ |
86 | 86 | 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 ); |
111 | 89 | } |
112 | 90 |
|
113 | 91 | /** |
|
0 commit comments