-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wp-now: create
php
command to execute PHP files (#336)
- Related to #242 ## Proposed changes - Create a new `php` command to execute php files - Tests for `executePHPFile` `wp-cli` will have its separate command in a different PR. ## Testing instructions - Create a simple php. e.g. `example.php`: `<?php echo "Hello World!";` - Copy the relative or absolute path to that file - Execute `npx nx preview wp-now php ~/path-to/example.php` - Modify your `example.php` to add WordPress functions: e.g. `<?php echo get_bloginfo();` - Execute `npx nx preview wp-now php ~/path-to/example.php --path=/path-to-your-plugin/gutenberg` // To load WordPress needs to be executed in a mode different than `index`. --------- Co-authored-by: Daniel Bachhuber <[email protected]>
- Loading branch information
1 parent
1a31776
commit a8ede03
Showing
12 changed files
with
237 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import startWPNow from './wp-now'; | ||
import { WPNowOptions } from './config'; | ||
import { disableOutput } from './output'; | ||
|
||
const VFS_TMP_PATH = '/vfs-wp-now-tmp'; | ||
const VFS_PHP_FILE = path.join(VFS_TMP_PATH, 'parent.php'); | ||
|
||
/** | ||
* | ||
* Execute a PHP file given its path. For non index mode it loads WordPress context. | ||
* | ||
* @param {string} filePath - The path to the PHP file to be executed. | ||
* @param {WPNowOptions} [options={}] - Optional configuration object for WPNow. Defaults to an empty object. | ||
* @returns {Promise<{ name: string; status: 0; }>} - Returns a Promise that resolves to an object containing | ||
* the exit name and status (0 for success). | ||
* @throws {Error} - Throws an error if the specified file is not found or if an error occurs while executing the file. | ||
*/ | ||
export async function executePHPFile( | ||
filePath: string, | ||
options: WPNowOptions = {} | ||
) { | ||
disableOutput(); | ||
const { phpInstances, options: wpNowOptions } = await startWPNow({ | ||
...options, | ||
numberOfPhpInstances: 2, | ||
}); | ||
const [, php] = phpInstances; | ||
|
||
// check if filePath exists | ||
const absoluteFilePath = path.resolve(filePath); | ||
if (!fs.existsSync(absoluteFilePath)) { | ||
throw new Error(`Could not open input file: ${absoluteFilePath}`); | ||
} | ||
|
||
let fileToExecute = absoluteFilePath; | ||
if (wpNowOptions.mode !== 'index') { | ||
// Load WordPress context for non index mode. | ||
php.mkdirTree(VFS_TMP_PATH); | ||
php.writeFile( | ||
VFS_PHP_FILE, | ||
`<?php | ||
$_SERVER['HTTP_HOST'] = '${wpNowOptions.absoluteUrl}'; | ||
require_once '${path.join(wpNowOptions.documentRoot, 'wp-load.php')}'; | ||
require_once '${filePath}'; | ||
` | ||
); | ||
fileToExecute = VFS_PHP_FILE; | ||
} | ||
|
||
try { | ||
php.useHostFilesystem(); | ||
await php.cli(['php', fileToExecute]); | ||
} catch (resultOrError) { | ||
const success = | ||
resultOrError.name === 'ExitStatus' && resultOrError.status === 0; | ||
if (!success) { | ||
throw resultOrError; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import fs from 'fs-extra'; | ||
import path from 'path'; | ||
import { executePHPFile } from '../execute-php-file'; | ||
import getWpNowConfig from '../config'; | ||
|
||
const exampleDir = path.join(__dirname, 'execute-php-file'); | ||
|
||
test('php file execution in index mode', async () => { | ||
const resultFilePath = path.join(exampleDir, 'hello-world-result.txt'); | ||
// reset result file | ||
fs.writeFileSync(resultFilePath, ''); | ||
const options = await getWpNowConfig({ | ||
path: exampleDir, | ||
}); | ||
await executePHPFile(path.join(exampleDir, 'hello-world.php'), options); | ||
const output = fs.readFileSync(resultFilePath, 'utf8'); | ||
expect(output).toBe('Hello World!'); | ||
}); | ||
|
||
test('php file execution for each PHP Version', async () => { | ||
const resultFilePath = path.join(exampleDir, 'php-version-result.txt'); | ||
const options = await getWpNowConfig({ | ||
path: exampleDir, | ||
}); | ||
await executePHPFile(path.join(exampleDir, 'php-version.php'), { | ||
...options, | ||
phpVersion: '7.4', | ||
}); | ||
let output = fs.readFileSync(resultFilePath, 'utf8'); | ||
expect(output.substring(0, 16)).toBe('PHP Version: 7.4'); | ||
|
||
await executePHPFile(path.join(exampleDir, 'php-version.php'), { | ||
...options, | ||
phpVersion: '8.0', | ||
}); | ||
output = fs.readFileSync(resultFilePath, 'utf8'); | ||
expect(output.substring(0, 16)).toBe('PHP Version: 8.0'); | ||
|
||
await executePHPFile(path.join(exampleDir, 'php-version.php'), { | ||
...options, | ||
phpVersion: '8.2', | ||
}); | ||
output = fs.readFileSync(resultFilePath, 'utf8'); | ||
expect(output.substring(0, 16)).toBe('PHP Version: 8.2'); | ||
|
||
fs.writeFileSync(resultFilePath, 'PHP Version: X.Y'); | ||
}); |
1 change: 1 addition & 0 deletions
1
packages/wp-now/src/tests/execute-php-file/hello-world-result.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello World! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?php | ||
$resultFile = fopen(__DIR__ . '/hello-world-result.txt', 'w'); | ||
fwrite($resultFile, 'Hello World!'); | ||
fclose($resultFile); |
1 change: 1 addition & 0 deletions
1
packages/wp-now/src/tests/execute-php-file/php-version-result.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
PHP Version: X.Y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?php | ||
$resultFile = fopen(__DIR__ . '/php-version-result.txt', 'w'); | ||
fwrite($resultFile, "PHP Version: " . phpversion()); | ||
fclose($resultFile); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters