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

Implement dependency resolution for services #6484

Merged
merged 16 commits into from
Aug 4, 2021

Conversation

pierlon
Copy link
Contributor

@pierlon pierlon commented Jul 25, 2021

Summary

Fixes #6459

This PR implements a simple way for services to resolve their dependent services so that it is guaranteed that those services exist before retrieving them from the service container.

In order to accomplish this, the AmpProject\AmpWP\Infrastructure\ServiceBasedPlugin::register_services() method has been modified to check if a service implements the AmpProject\AmpWP\Infrastructure\HasRequirements interface, and if it does, it then retrieves the list of service identifiers it requires in order for the service to be registered. If any of the required service IDs do not exist in the service container, the registration of the service will be delayed (moved to the end of the to-be-registered services list) to allow for other services to be registered in the mean time.

Testing

To test that this fixes #6459:

  1. Add the following plugin to replicate the bug:
<?php
/**
 * Plugin Name: Activate AMP Plugin
 */

add_action( 'admin_head', function () {
    activate_plugin( 'amp/amp.php' );
} );
  1. Deactivate the AMP plugin
  2. Activate the plugin above and it should successfully activate the AMP plugin without any errors.

Checklist

  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@pierlon pierlon added this to the v2.1.4 milestone Jul 27, 2021
@pierlon pierlon marked this pull request as ready for review July 27, 2021 20:38
@github-actions
Copy link
Contributor

github-actions bot commented Jul 27, 2021

Plugin builds for 953a039 are ready 🛎️!

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

Looks great to me, but I defer to @schlessera for his final word.

Comment on lines -3 to -7
codecov:
notify:
# Wait for Jest & PHPUnit $ Behat reports before notifying
after_n_builds: 3

Copy link
Member

Choose a reason for hiding this comment

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

Why is this no longer needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Codecov waits for all 3 coverage reports before posting the summary for the PR; problem is that we are now skipping some PR checks which means it's no longer guaranteed 3 coverage reports will be uploaded, so Codecov will be waiting for reports that it will potentially never receive.

/**
* Get the list of service IDs required for this service to be registered.
*
* @return array<string> List of required services.
Copy link
Member

Choose a reason for hiding this comment

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

This means the same thing, but any reason for/against?

Suggested change
* @return array<string> List of required services.
* @return string[] List of required services.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Either works, just a little copypasta 😄.

Copy link
Contributor Author

@pierlon pierlon Jul 28, 2021

Choose a reason for hiding this comment

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

I'll commit the suggestion since it's what's more prominent in the codebase.

Comment on lines 201 to 209
/*
* Move the service to the end of the array.
*
* Note: Unsetting the key advances the internal array pointer to the next array item, so
* the current array item will have to be temporarily stored so that it can be re-added later.
*/
$delayed_service = [ key( $services ) => current( $services ) ];
unset( $services[ key( $services ) ] );
$services[ key( $delayed_service ) ] = current( $delayed_service );
Copy link
Member

Choose a reason for hiding this comment

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

Clever!

Co-authored-by: Weston Ruter <[email protected]>
@swissspidy
Copy link
Collaborator

Any objections to copy this for the Web Stories plugin? :-)

@pierlon
Copy link
Contributor Author

pierlon commented Jul 29, 2021

Any objections to copy this for the Web Stories plugin? :-)

None at all 😄.

src/AmpWpPlugin.php Outdated Show resolved Hide resolved
@schlessera
Copy link
Collaborator

@pierlon I made changes to this to add a more robust mechanism to deal with the load order. Please review these changes and let me know your thoughts.

@pierlon
Copy link
Contributor Author

pierlon commented Aug 4, 2021

@schlessera Thanks for making those changes. I've reviewed and tested the updates regarding delayed services and it works as expected 👍.

@westonruter westonruter merged commit 543aa27 into develop Aug 4, 2021
@westonruter westonruter deleted the fix/6459-service-dependency-resolution branch August 4, 2021 23:57
westonruter added a commit that referenced this pull request Aug 5, 2021
Co-authored-by: Pierre Gordon <[email protected]>
Co-authored-by: Alain Schlesser <[email protected]>
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.

Plugin fails to activate using TGMPA
4 participants