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

Fix autoloading #56

Merged
merged 2 commits into from
Sep 14, 2022
Merged

Fix autoloading #56

merged 2 commits into from
Sep 14, 2022

Conversation

leogermani
Copy link
Collaborator

Fixes the following error when using WorDBless as a dependency

and updates the README

Class WorDBless\Composer\InstallDropin is not autoloadable, can not call post-update-cmd script

@kraftbj
Copy link
Contributor

kraftbj commented Sep 14, 2022

In terms of autoloading, but still need to fix up the copy command.

> WorDBless\Composer\InstallDropin::copy
Script WorDBless\Composer\InstallDropin::copy handling the post-update-cmd event terminated with an exception

In InstallDropin.php line 17:
                                                                               
  copy(src/dbless-wpdb.php): Failed to open stream: No such file or directory  
                                                                               

(after running composer update automattic/wordbless@dev-update/fix-autoload in Jetpack's projects/packages/abtest with the updated post-install command.

@kraftbj
Copy link
Contributor

kraftbj commented Sep 14, 2022

Do you think we need to define a constant to define the dir that we're in and use that, e.g. WDBLESS_DIR so it'll work both ways? ignore this

@@ -22,7 +22,8 @@
},
"autoload": {
"psr-4": {
"WorDBless\\": "src/"
"WorDBless\\": "src/",
"WorDBless\\Composer\\": "src/Composer/"
Copy link
Collaborator

Choose a reason for hiding this comment

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

In PSR-4 the filesystem structure matches the namespace.
So line number 26 is not necessary.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't use PSR-4 much/ever (thanks WordPress...) but given the error above Class WorDBless\Composer\InstallDropin is not autoloadable, can not call post-update-cmd script, it can't hurt anything to have.

@szepeviktor
Copy link
Collaborator

when using WorDBless as a dependency

When a package is a dependency its post-install-cmd event is not triggered.

@szepeviktor
Copy link
Collaborator

szepeviktor commented Sep 14, 2022

README: Add this script to your composer.json:

I see! This is in the main project.
Using a dependency's class in a Composer script is a mystery for me!

PHP classes containing defined callbacks must be autoloadable via Composer's autoload functionality.
Callbacks can only autoload classes from psr-0, psr-4 and classmap definitions. If a defined callback relies on functions defined outside of a class, the callback itself is responsible for loading the file containing these functions.

(Composer docs)

🖱️ composer/composer#8147 (comment)

@kraftbj
Copy link
Contributor

kraftbj commented Sep 14, 2022

This worked for me now.

Steps to test:

  1. In the Jetpack repo, cd projects/packages/abtest.
  2. Ensure no vendor, wordpress, or composer.lock file (just for the sake of testing)
  3. Update abtest's composer.json to look for version "automattic/wordbless": "dev-update/fix-autoload",
  4. Run composer update

Expected CLI result should include:

...
10 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
36 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> WorDBless\Composer\InstallDropin::copy

and there should be a wordpress/wp-content/db.php file in the abtest package dir.

I'm going to approve this based on that, but @leogermani can you double-check me on ^ before we cut a new version.

@leogermani
Copy link
Collaborator Author

Worked for me as well

@leogermani leogermani merged commit 4811e45 into master Sep 14, 2022
@leogermani leogermani deleted the update/fix-autoload branch September 14, 2022 14:01
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.

3 participants