wp-cli / scaffold-package-command

Scaffolds WP-CLI commands with functional tests, full README.md, and more.
MIT License
68 stars 19 forks source link

Update documentation to help users what to do after changing 'Hello World' #215

Open Luke1982 opened 1 year ago

Luke1982 commented 1 year ago

Feature Request

Describe your use case and the problem you are facing

I spent quite a lot of time figuring out why my adapted Hello World scaffolded package command wouldn't run, until I found out I had to update the package to make sure the autoload files in the main composer package were regenerated. Maybe it's because of my lack of composer knowledge, but the docs could have told me that, would have been nice.

danielbachhuber commented 1 year ago

@Luke1982 Seems reasonable to me! Want to submit a PR with your suggested change?

Luke1982 commented 1 year ago

@danielbachhuber I am willing, but afraid I don't have sufficient knowledge to describe the process correctly. I had a lot of hit 'n misses before I solved it and can't remember correctly how I did it.

danielbachhuber commented 1 year ago

@Luke1982 Ok, no worries. It would be great if someone attempted this process, and then documented what they need to do to resolve.

ozamorowski commented 1 year ago

I have the same problem as @Luke1982 a while ago, but I ran wp plugin update and now everything is working. Is this the correct method to do that?

danielbachhuber commented 1 year ago

Hm, I wouldn't expect wp plugin update to have an impact on this.

ozamorowski commented 1 year ago

Lol, I did wp package update not wp plugin update. Sorry!

danielbachhuber commented 1 year ago

Ah. wp package update would reset the autoloader, so yes: that would fix the reported problem.

billbellon commented 1 month ago

I'm running into a similar situation. I created a project and it works (i.e., wp hello-world runs successfully). However I want to rename files, classes, namespaces ... so that they don't use "hello world" - I want to use names that make sense for the custom command I want to create. I don't see anything in the documentation on how to do this.

Here's how to reproduce my problem:

  1. use wp scaffold package myusername/mypackage to create a package
  2. run wp hello-world in a WordPress installation - it runs successfully
  3. cd .wp-cli/packages/local/myusername/mypackage/
  4. rename hello-world-command.php to hello-world-command-debug.php as a test to see if I can rename this file
  5. edit .wp-cli.yml to require the new filename (i.e., on line 2 change to: - hello-world-command-debug.php)
  6. edit composer.json to change hello-world-command.php to hello-world-command-debug.php in the autoload section
  7. run wp hello-world in a WordPress installation again and you get the following errors:
PHP Warning:  require(/home/myusername/.wp-cli/packages/vendor/composer/../myusername/mypackage/hello-world-command.php): Failed to open stream: No such file or directory in /home/myusername/.wp-cli/packages/vendor/composer/autoload_real.php on line 78
PHP Fatal error:  Uncaught Error: Failed opening required '/home/myusername/.wp-cli/packages/vendor/composer/../myusername/mypackage/hello-world-command.php' (include_path='.:/usr/share/pear:/usr/share/php') in /home/myusername/.wp-cli/packages/vendor/composer/autoload_real.php:78
Stack trace:
#0 /home/myusername/.wp-cli/packages/vendor/composer/autoload_real.php(61): composerRequire9c3b4f1402cbe43de8bcebbc523f4050()
#1 /home/myusername/.wp-cli/packages/vendor/autoload.php(7): ComposerAutoloaderInit9c3b4f1402cbe43de8bcebbc523f4050::getLoader()
#2 phar:///home/myusername/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/AutoloaderStep.php(52): require('...')
#3 phar:///home/myusername/bin/wp/vendor/wp-cli/wp-cli/php/bootstrap.php(78): WP_CLI\Bootstrap\AutoloaderStep->process()
#4 phar:///home/myusername/bin/wp/vendor/wp-cli/wp-cli/php/wp-cli.php(32): WP_CLI\bootstrap()
#5 phar:///home/myusername/bin/wp/php/boot-phar.php(11): include('...')
#6 /home/myusername/bin/wp(4): include('...')
#7 {main}
  thrown in /home/myusername/.wp-cli/packages/vendor/composer/autoload_real.php on line 78

After I got this error I then tried

wp package update

and re-ran the wp hello-world command and and it still didn't work, with the following errors:

PHP Warning:  require(/home/myusername/.wp-cli/packages/vendor/composer/../myusername/mypackage/hello-world-command.php): Failed to open stream: No such file or directory in /home/myusername/.wp-cli/packages/vendor/composer/autoload_real.php on line 78
PHP Fatal error:  Uncaught Error: Failed opening required '/home/myusername/.wp-cli/packages/vendor/composer/../myusername/mypackage/hello-world-command.php' (include_path='.:/usr/share/pear:/usr/share/php') in /home/myusername/.wp-cli/packages/vendor/composer/autoload_real.php:78
Stack trace:
#0 /home/myusername/.wp-cli/packages/vendor/composer/autoload_real.php(61): composerRequire9c3b4f1402cbe43de8bcebbc523f4050()
#1 /home/myusername/.wp-cli/packages/vendor/autoload.php(7): ComposerAutoloaderInit9c3b4f1402cbe43de8bcebbc523f4050::getLoader()
#2 phar:///home/myusername/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/AutoloaderStep.php(52): require('...')
#3 phar:///home/myusername/bin/wp/vendor/wp-cli/wp-cli/php/bootstrap.php(78): WP_CLI\Bootstrap\AutoloaderStep->process()
#4 phar:///home/myusername/bin/wp/vendor/wp-cli/wp-cli/php/wp-cli.php(32): WP_CLI\bootstrap()
#5 phar:///home/myusername/bin/wp/php/boot-phar.php(11): include('...')
#6 /home/myusername/bin/wp(4): include('...')
#7 {main}
  thrown in /home/myusername/.wp-cli/packages/vendor/composer/autoload_real.php on line 78
BrianHenryIE commented 1 month ago

With Composer's autoloading, you can use a psr-4 key which will use the PHP namespace and classname to determine the path on the filesystem to the class. You also have the options of classmap and files keys which are used when generating the autoloader, i.e. as more files are added, they are not known by the autoloader until it is regenerated.

As you're developing you'll need to run:

composer dump-autoload --working-dir=$(wp package path)

I think wp package update did not work for you because there was nothing actually updated, so it didn't rebuild the autoloader.

https://getcomposer.org/doc/01-basic-usage.md#autoloading