Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wp-now: create php command to execute PHP files #336

Merged
merged 26 commits into from
May 18, 2023

Conversation

sejas
Copy link
Collaborator

@sejas sejas commented May 12, 2023

See #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.

Example: npx nx preview wp-now php ~/Desktop/example.php --path=/Users/macbookpro/demo-wp-now/gutenberg
@sejas sejas self-assigned this May 12, 2023
test('php file execution for each PHP Version', async () => {
const resultFilePath = path.join(exampleDir, 'php-version-result.txt');

for (const phpVersion of SupportedPHPVersions) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add explicit tests here? Assertions inside of for always give me concern because they can accidentally stop working.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the for loop here:
fe410c8

Thanks for the advice.

@kozer
Copy link
Contributor

kozer commented May 15, 2023

Hello @sejas ! Great job!
I tried to follow the testing steps. The PHP file was executed successfully initially, but for the rest steps (pointing to a plugin directory) it didn't.
What it tried to do, was to download WordPress, and use that, however, as my /home and / are in a different filesystem I got the following error:

Failed to start the server: EXDEV: cross-device link not permitted, rename '/tmp/wordpress' -> '/home/<username>/.wp-now/wordpress-versions/latest'

I know it's not related to the current issue, as it was something I have mentioned in the past, however I'd find it awesome if:

  • Guide me through a manual way to do that test.
  • You can fix that error. If this is not possible (as it's not part of this pr, I get it), can you please open a new issue:? If you can't I'm happy to do so.

UPDATE:
In the meantime, I moved manually the downloaded WordPress to my .wp-now folder. However, now I get the following:

WordPress latest folder already exists. Skipping download.
downloaded false
Sqlite folder already exists. Skipping download.
Warning: require_once(/var/www/html/wp-load.php): Failed to open stream: No such file or directory in /tmp/parent.php on line 2
Fatal error: Uncaught Error: Failed opening required '/var/www/html/wp-load.php' (include_path='.:') in /tmp/parent.php:2

@sejas
Copy link
Collaborator Author

sejas commented May 15, 2023

@kozer, Thanks for sharing your error. I think I fixed it here: d3ea228
Can you give it another try?

Failed to start the server: EXDEV: cross-device link not permitted, rename '/tmp/wordpress' -> '/home/<username>/.wp-now/wordpress-versions/latest'

@kozer
Copy link
Contributor

kozer commented May 15, 2023

@kozer, Thanks for sharing your error. I think I fixed it here: d3ea228 Can you give it another try?

Failed to start the server: EXDEV: cross-device link not permitted, rename '/tmp/wordpress' -> '/home/<username>/.wp-now/wordpress-versions/latest'

This is now fixed, thanks!
But now, I get the following:

Error: Could not mount /home/wild/.wp-now/sqlite-database-integration-main/db.copy: Not a directory or a symbolic link to a directory.

@sejas
Copy link
Collaborator Author

sejas commented May 15, 2023

This is now fixed, thanks! But now, I get the following:

Error: Could not mount /home/wild/.wp-now/sqlite-database-integration-main/db.copy: Not a directory or a symbolic link to a directory.

@kozer, I'm glad that the first issue is fixed 🥳

@kozer
Copy link
Contributor

kozer commented May 16, 2023

This is now fixed, thanks! But now, I get the following:

Error: Could not mount /home/wild/.wp-now/sqlite-database-integration-main/db.copy: Not a directory or a symbolic link to a directory.

@kozer, I'm glad that the first issue is fixed partying_face

Cool! I'll approve it then, as I don't find any other problems.
Thanks for handling creating this issue!

// check if filePath exists
const absoluteFilePath = path.resolve(filePath);
if (!fs.existsSync(absoluteFilePath)) {
throw new Error(`File not found: ${absoluteFilePath}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's match PHP's behavior:

$ php foo.php
Could not open input file: foo.php

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed cd4afb8

} catch (error) {
console.error(error);
spinner.fail(
`Failed to start the server: ${
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this output is necessary.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed 5bd0f53

});
},
async (argv) => {
const spinner = startSpinner('Running the php command...');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the caller might expect to use the output for something else, I don't think wp-now php should provide any additional output.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the wp-now logs:
5157e9e

describe: "WordPress version to use: e.g. '--wp=6.2'",
type: 'string',
default: DEFAULT_WORDPRESS_VERSION,
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One issue is that I can't pass arbitrary parameters to my script:

$ nx preview wp-now php example.php --php=8.1 --foo=ba
wp-now php <filePath>
Run the php command passing the arguments for php cli
Positionals:
  filePath  Path to the PHP file to run                      [string] [required]
Options:
      --version  Show version number                                   [boolean]
      --path     Path to the PHP or WordPress project. Defaults to the current w
                 orking directory.
      [string] [default: "/Users/danielbachhuber/projects/wordpress-playground"]
      --php      PHP version to use.                   [string] [default: "8.0"]
      --wp       WordPress version to use: e.g. '--wp=6.2'
                                                    [string] [default: "latest"]
  -h, --help     Show help                                             [boolean]
Unknown argument: foo

Copy link
Collaborator Author

@sejas sejas May 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For wp-cli we will have a different command that will accept any parameters and pass them to wp-cli.

I could do the same for this command, but the trade-off is that the user will not be able to change the php version pr the wp version or any other wp-now parameter.

);
process.exit(result.status);
} catch (error) {
console.error(error);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recently we've merged the output wrapper. Could we use output?.error() here too?

Should we modify shouldOutput to return false when executed from the PHP CLI context?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as this function is not a exported from the wp-now package, I think we're good here. There should be no way to trigger it from the outside. As for the internal use, printing too many errors sounds better than printing too few.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we modify shouldOutput to return false when executed from the PHP CLI context?

So no output when it's running in PHP CLI? When should it output, then?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah you meant wp-now php for shouldOutput=false. Your call.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adamziel yes, I think it should output whatever the PHP script outputs and nothing more. My "Hello World" script executed using PHP binary built-in with macOS outputs following:

& php /path/to/test.php
Hello World!

And our new PHP command:

$ npx nx preview wp-now php /path/to/test.php
[...nx build output here...]

(node:30521) ExperimentalWarning: Custom ESM Loaders is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
Running the php command......
directory: /Volumes/Sites/wordpress-playground
mode: index
php: 8.0
wp: latest
Hello World!                            

Copy link
Collaborator

@adamziel adamziel May 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, that makes sense! FYI the Custom ESM Loaders thing won't be there in the built npm package. I'd love to get rid of it from the repo version as well somehow.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for confirming that. As far as I saw how npm-installed wp-now works, the nx build output is also gone there.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the wp-now logs but I'm keeping the error trace in case the execution fails.
5157e9e

Copy link
Member

@danielbachhuber danielbachhuber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should update the README in this PR too.

@sejas
Copy link
Collaborator Author

sejas commented May 18, 2023

We should update the README in this PR too.

Updated: 10c3458

fs.renameSync(path.join(tempFolder, 'wordpress'), finalFolder);
fs.moveSync(path.join(tempFolder, 'wordpress'), finalFolder, {
overwrite: true,
});
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This solves @kozer issue #336 (comment)

@adamziel adamziel merged commit a8ede03 into WordPress:trunk May 18, 2023
@sejas sejas deleted the add/wp-now-php-file-execution branch May 18, 2023 14:58
danielbachhuber added a commit that referenced this pull request May 18, 2023
Pookie717 added a commit to Pookie717/wordpress-playground that referenced this pull request Oct 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants