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 #8513

Merged
merged 62 commits into from
Oct 5, 2021

Conversation

swissspidy
Copy link
Collaborator

As taken from ampproject/amp-wp#6484

Are there any dependencies we'd need to add already?

@swissspidy swissspidy added Type: Enhancement New feature or improvement of an existing feature Type: Code Quality Things that need a refactor, rewrite or just some good old developer ❤️ labels Jul 30, 2021
@swissspidy swissspidy requested a review from spacedmonkey July 30, 2021 08:31
@google-cla google-cla bot added the cla: yes label Jul 30, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Jul 30, 2021

Size Change: 0 B

Total Size: 4.05 MB

ℹ️ View Unchanged
Filename Size
assets/css/carousel-view-rtl.css 701 B
assets/css/carousel-view.css 701 B
assets/css/vendors-chunk-getStoryPropsToSave-wp-story-editor-rtl.css 706 B
assets/css/vendors-chunk-getStoryPropsToSave-wp-story-editor.css 706 B
assets/css/web-stories-block-rtl.css 4.43 kB
assets/css/web-stories-block.css 4.47 kB
assets/css/web-stories-embed-rtl.css 288 B
assets/css/web-stories-embed.css 288 B
assets/css/web-stories-list-styles-rtl.css 2.3 kB
assets/css/web-stories-list-styles.css 2.32 kB
assets/css/web-stories-theme-style-twentyeleven-rtl.css 102 B
assets/css/web-stories-theme-style-twentyeleven.css 102 B
assets/css/web-stories-theme-style-twentyfifteen-rtl.css 251 B
assets/css/web-stories-theme-style-twentyfifteen.css 251 B
assets/css/web-stories-theme-style-twentyfourteen-rtl.css 287 B
assets/css/web-stories-theme-style-twentyfourteen.css 287 B
assets/css/web-stories-theme-style-twentyseventeen-rtl.css 288 B
assets/css/web-stories-theme-style-twentyseventeen.css 288 B
assets/css/web-stories-theme-style-twentysixteen-rtl.css 224 B
assets/css/web-stories-theme-style-twentysixteen.css 224 B
assets/css/web-stories-theme-style-twentyten-rtl.css 143 B
assets/css/web-stories-theme-style-twentyten.css 143 B
assets/css/web-stories-theme-style-twentytwelve-rtl.css 256 B
assets/css/web-stories-theme-style-twentytwelve.css 256 B
assets/css/web-stories-theme-style-twentytwenty-rtl.css 86 B
assets/css/web-stories-theme-style-twentytwenty.css 86 B
assets/css/web-stories-theme-style-twentytwentyone-rtl.css 325 B
assets/css/web-stories-theme-style-twentytwentyone.css 326 B
assets/css/web-stories-widget-rtl.css 485 B
assets/css/web-stories-widget.css 485 B
assets/css/wp-dashboard-rtl.css 658 B
assets/css/wp-dashboard.css 659 B
assets/css/wp-story-editor-rtl.css 608 B
assets/css/wp-story-editor.css 609 B
assets/js/carousel-view.js 3.72 kB
assets/js/chunk-colorthief.js 2.61 kB
assets/js/chunk-focus-visible.js 999 B
assets/js/chunk-fonts.js 53.2 kB
assets/js/chunk-getStoryPropsToSave.js 1.32 MB
assets/js/chunk-web-stories-template-0.js 535 B
assets/js/chunk-web-stories-template-100.js 466 B
assets/js/chunk-web-stories-template-102.js 9.15 kB
assets/js/chunk-web-stories-template-104.js 516 B
assets/js/chunk-web-stories-template-108.js 488 B
assets/js/chunk-web-stories-template-110.js 6.99 kB
assets/js/chunk-web-stories-template-112.js 567 B
assets/js/chunk-web-stories-template-116.js 540 B
assets/js/chunk-web-stories-template-118.js 7.06 kB
assets/js/chunk-web-stories-template-12.js 503 B
assets/js/chunk-web-stories-template-120.js 531 B
assets/js/chunk-web-stories-template-124.js 503 B
assets/js/chunk-web-stories-template-126.js 7.69 kB
assets/js/chunk-web-stories-template-128.js 574 B
assets/js/chunk-web-stories-template-132.js 546 B
assets/js/chunk-web-stories-template-134.js 9.85 kB
assets/js/chunk-web-stories-template-136.js 535 B
assets/js/chunk-web-stories-template-14.js 8.57 kB
assets/js/chunk-web-stories-template-140.js 507 B
assets/js/chunk-web-stories-template-142.js 7.95 kB
assets/js/chunk-web-stories-template-144.js 572 B
assets/js/chunk-web-stories-template-148.js 545 B
assets/js/chunk-web-stories-template-150.js 8.79 kB
assets/js/chunk-web-stories-template-152.js 485 B
assets/js/chunk-web-stories-template-156.js 457 B
assets/js/chunk-web-stories-template-158.js 9.6 kB
assets/js/chunk-web-stories-template-16.js 571 B
assets/js/chunk-web-stories-template-160.js 542 B
assets/js/chunk-web-stories-template-164.js 514 B
assets/js/chunk-web-stories-template-166.js 8.21 kB
assets/js/chunk-web-stories-template-168.js 519 B
assets/js/chunk-web-stories-template-172.js 492 B
assets/js/chunk-web-stories-template-174.js 8.49 kB
assets/js/chunk-web-stories-template-176.js 510 B
assets/js/chunk-web-stories-template-180.js 483 B
assets/js/chunk-web-stories-template-182.js 6.91 kB
assets/js/chunk-web-stories-template-184.js 594 B
assets/js/chunk-web-stories-template-188.js 567 B
assets/js/chunk-web-stories-template-190.js 6.63 kB
assets/js/chunk-web-stories-template-192.js 511 B
assets/js/chunk-web-stories-template-196.js 484 B
assets/js/chunk-web-stories-template-198.js 10.5 kB
assets/js/chunk-web-stories-template-20.js 543 B
assets/js/chunk-web-stories-template-200.js 535 B
assets/js/chunk-web-stories-template-204.js 507 B
assets/js/chunk-web-stories-template-206.js 6.71 kB
assets/js/chunk-web-stories-template-208.js 589 B
assets/js/chunk-web-stories-template-212.js 562 B
assets/js/chunk-web-stories-template-214.js 6.32 kB
assets/js/chunk-web-stories-template-216.js 535 B
assets/js/chunk-web-stories-template-22.js 8.69 kB
assets/js/chunk-web-stories-template-220.js 507 B
assets/js/chunk-web-stories-template-222.js 7.16 kB
assets/js/chunk-web-stories-template-224.js 524 B
assets/js/chunk-web-stories-template-228.js 498 B
assets/js/chunk-web-stories-template-230.js 8.05 kB
assets/js/chunk-web-stories-template-232.js 551 B
assets/js/chunk-web-stories-template-236.js 524 B
assets/js/chunk-web-stories-template-238.js 8.09 kB
assets/js/chunk-web-stories-template-24.js 531 B
assets/js/chunk-web-stories-template-240.js 564 B
assets/js/chunk-web-stories-template-244.js 536 B
assets/js/chunk-web-stories-template-246.js 7.42 kB
assets/js/chunk-web-stories-template-248.js 502 B
assets/js/chunk-web-stories-template-252.js 476 B
assets/js/chunk-web-stories-template-254.js 9.14 kB
assets/js/chunk-web-stories-template-256.js 540 B
assets/js/chunk-web-stories-template-260.js 512 B
assets/js/chunk-web-stories-template-262.js 12 kB
assets/js/chunk-web-stories-template-264.js 489 B
assets/js/chunk-web-stories-template-268.js 463 B
assets/js/chunk-web-stories-template-270.js 8.54 kB
assets/js/chunk-web-stories-template-272.js 560 B
assets/js/chunk-web-stories-template-276.js 533 B
assets/js/chunk-web-stories-template-278.js 7.21 kB
assets/js/chunk-web-stories-template-28.js 503 B
assets/js/chunk-web-stories-template-280.js 556 B
assets/js/chunk-web-stories-template-284.js 528 B
assets/js/chunk-web-stories-template-286.js 8.48 kB
assets/js/chunk-web-stories-template-288.js 577 B
assets/js/chunk-web-stories-template-292.js 547 B
assets/js/chunk-web-stories-template-294.js 11.1 kB
assets/js/chunk-web-stories-template-296.js 522 B
assets/js/chunk-web-stories-template-30.js 7.81 kB
assets/js/chunk-web-stories-template-300.js 495 B
assets/js/chunk-web-stories-template-302.js 5.93 kB
assets/js/chunk-web-stories-template-304.js 570 B
assets/js/chunk-web-stories-template-308.js 543 B
assets/js/chunk-web-stories-template-310.js 7.5 kB
assets/js/chunk-web-stories-template-312.js 578 B
assets/js/chunk-web-stories-template-316.js 552 B
assets/js/chunk-web-stories-template-318.js 7.1 kB
assets/js/chunk-web-stories-template-32.js 558 B
assets/js/chunk-web-stories-template-320.js 550 B
assets/js/chunk-web-stories-template-324.js 523 B
assets/js/chunk-web-stories-template-326.js 8.7 kB
assets/js/chunk-web-stories-template-328.js 551 B
assets/js/chunk-web-stories-template-332.js 523 B
assets/js/chunk-web-stories-template-334.js 7.38 kB
assets/js/chunk-web-stories-template-336.js 512 B
assets/js/chunk-web-stories-template-340.js 487 B
assets/js/chunk-web-stories-template-342.js 6.57 kB
assets/js/chunk-web-stories-template-344.js 546 B
assets/js/chunk-web-stories-template-348.js 517 B
assets/js/chunk-web-stories-template-350.js 7.97 kB
assets/js/chunk-web-stories-template-352.js 561 B
assets/js/chunk-web-stories-template-356.js 533 B
assets/js/chunk-web-stories-template-358.js 9.79 kB
assets/js/chunk-web-stories-template-36.js 529 B
assets/js/chunk-web-stories-template-360.js 557 B
assets/js/chunk-web-stories-template-364.js 529 B
assets/js/chunk-web-stories-template-366.js 6.86 kB
assets/js/chunk-web-stories-template-368.js 528 B
assets/js/chunk-web-stories-template-372.js 501 B
assets/js/chunk-web-stories-template-374.js 4.57 kB
assets/js/chunk-web-stories-template-376.js 574 B
assets/js/chunk-web-stories-template-38.js 11 kB
assets/js/chunk-web-stories-template-380.js 546 B
assets/js/chunk-web-stories-template-382.js 7.93 kB
assets/js/chunk-web-stories-template-384.js 541 B
assets/js/chunk-web-stories-template-388.js 515 B
assets/js/chunk-web-stories-template-390.js 7.76 kB
assets/js/chunk-web-stories-template-392.js 498 B
assets/js/chunk-web-stories-template-396.js 472 B
assets/js/chunk-web-stories-template-398.js 9.39 kB
assets/js/chunk-web-stories-template-4.js 508 B
assets/js/chunk-web-stories-template-40.js 550 B
assets/js/chunk-web-stories-template-400.js 494 B
assets/js/chunk-web-stories-template-404.js 467 B
assets/js/chunk-web-stories-template-406.js 8.04 kB
assets/js/chunk-web-stories-template-408.js 523 B
assets/js/chunk-web-stories-template-412.js 495 B
assets/js/chunk-web-stories-template-414.js 9.57 kB
assets/js/chunk-web-stories-template-416.js 588 B
assets/js/chunk-web-stories-template-420.js 561 B
assets/js/chunk-web-stories-template-422.js 9.6 kB
assets/js/chunk-web-stories-template-424.js 545 B
assets/js/chunk-web-stories-template-428.js 517 B
assets/js/chunk-web-stories-template-430.js 5.35 kB
assets/js/chunk-web-stories-template-432.js 544 B
assets/js/chunk-web-stories-template-436.js 514 B
assets/js/chunk-web-stories-template-438.js 7.14 kB
assets/js/chunk-web-stories-template-44.js 521 B
assets/js/chunk-web-stories-template-440.js 568 B
assets/js/chunk-web-stories-template-444.js 540 B
assets/js/chunk-web-stories-template-446.js 6.22 kB
assets/js/chunk-web-stories-template-448.js 530 B
assets/js/chunk-web-stories-template-452.js 502 B
assets/js/chunk-web-stories-template-454.js 9.14 kB
assets/js/chunk-web-stories-template-456.js 522 B
assets/js/chunk-web-stories-template-46.js 8.92 kB
assets/js/chunk-web-stories-template-460.js 493 B
assets/js/chunk-web-stories-template-462.js 13.6 kB
assets/js/chunk-web-stories-template-464.js 550 B
assets/js/chunk-web-stories-template-468.js 521 B
assets/js/chunk-web-stories-template-470.js 5.27 kB
assets/js/chunk-web-stories-template-472.js 575 B
assets/js/chunk-web-stories-template-476.js 547 B
assets/js/chunk-web-stories-template-478.js 8.26 kB
assets/js/chunk-web-stories-template-48.js 559 B
assets/js/chunk-web-stories-template-480.js 501 B
assets/js/chunk-web-stories-template-484.js 473 B
assets/js/chunk-web-stories-template-486.js 8.51 kB
assets/js/chunk-web-stories-template-52.js 531 B
assets/js/chunk-web-stories-template-54.js 6.59 kB
assets/js/chunk-web-stories-template-56.js 555 B
assets/js/chunk-web-stories-template-6.js 10.4 kB
assets/js/chunk-web-stories-template-60.js 527 B
assets/js/chunk-web-stories-template-62.js 6.69 kB
assets/js/chunk-web-stories-template-64.js 565 B
assets/js/chunk-web-stories-template-68.js 537 B
assets/js/chunk-web-stories-template-70.js 7.94 kB
assets/js/chunk-web-stories-template-72.js 573 B
assets/js/chunk-web-stories-template-76.js 545 B
assets/js/chunk-web-stories-template-78.js 8.04 kB
assets/js/chunk-web-stories-template-8.js 531 B
assets/js/chunk-web-stories-template-80.js 525 B
assets/js/chunk-web-stories-template-84.js 496 B
assets/js/chunk-web-stories-template-86.js 6.54 kB
assets/js/chunk-web-stories-template-88.js 535 B
assets/js/chunk-web-stories-template-92.js 507 B
assets/js/chunk-web-stories-template-94.js 8 kB
assets/js/chunk-web-stories-template-96.js 494 B
assets/js/chunk-web-stories-textset-0.js 5.27 kB
assets/js/chunk-web-stories-textset-1.js 6.76 kB
assets/js/chunk-web-stories-textset-2.js 7.88 kB
assets/js/chunk-web-stories-textset-3.js 15.4 kB
assets/js/chunk-web-stories-textset-4.js 4.38 kB
assets/js/chunk-web-stories-textset-5.js 5.68 kB
assets/js/chunk-web-stories-textset-6.js 5.5 kB
assets/js/chunk-web-stories-textset-7.js 10.4 kB
assets/js/imgareaselect.js 4.14 kB
assets/js/lightbox.js 991 B
assets/js/tinymce-button.js 3.49 kB
assets/js/vendors-chunk-ffmpeg.js 5.61 kB
assets/js/vendors-chunk-getStoryPropsToSave-chunk-resize-observer-polyfill-wp-story-editor.js 2.55 kB
assets/js/vendors-chunk-getStoryPropsToSave-wp-story-editor.js 193 kB
assets/js/vendors-chunk-html-to-image.js 4.47 kB
assets/js/vendors-chunk-react-calendar.js 11.7 kB
assets/js/vendors-chunk-react-color.js 43.4 kB
assets/js/vendors-chunk-web-animations-js.js 14.6 kB
assets/js/vendors-wp-dashboard.js 94.3 kB
assets/js/web-stories-activation-notice.js 23.4 kB
assets/js/web-stories-block.js 19.4 kB
assets/js/web-stories-embed.js 493 B
assets/js/web-stories-widget.js 984 B
assets/js/wp-dashboard.js 116 kB
assets/js/wp-story-editor.js 1.49 MB

compressed-size-action

@spacedmonkey
Copy link
Contributor

I am not sure about adding this, until we need it. Is there any example service we could implement this on?

How about Jetpack or New_Relic?

@spacedmonkey
Copy link
Contributor

Silly question, how does this different from conditional service?

@swissspidy
Copy link
Collaborator Author

Silly question, how does this different from conditional service?

This is for services that depend on another service, not services that depend on some outside condition (e.g. is_admin()).

For example if service A uses Services::get( 'service_b' ) somewhere in its code, then B should be added as a dependency, so that it's guaranteed to be instantiated and exist first.

While we might not have obvious candidates for this in our code base yet (at least at first glance), it seems like a reasonable enhancement to facilitate further restructuring of the code base to use more services.

One opportunity there might be moving some of our functions in namespace.php to services implementing the Activateable and Deactivateable interfaces, where they could properly depend on the services they'd need.

Just some ideas though, haven't looked at it closely.

@swissspidy
Copy link
Collaborator Author

Sooo... this PR seems to uncover some flaws in our code.

For example, Stories_Settings_Controller relies on Settings first registering the settings, so that these are properly exposed in the REST API.

But with this PR it looks like Settings is not running yet when Stories_Settings_Controller is initialized. Adding the following to the controller class even causes a timeout:

public static function get_requirements(): array {
return [
	'settings',
];
}

The timeout goes away if Settings does not implement Delayed. So... a bit confusing 🤔

@pierlon do you perhaps have any thoughts or pointers?

@pierlon
Copy link
Contributor

pierlon commented Jul 30, 2021

@swissspidy It seems you've uncovered a bug. With the example you stated above the Settings service wouldn't be available yet for Stories_Settings_Controller as its registration is delayed, and thus it won't be in the service container. Therefore Stories_Settings_Controller will be infinitely waiting on a service that will never exist in the service container until the init action has been triggered.

I'll see how best this can be resolved and test the changes over at ampproject/amp-wp#6484.

@schlessera
Copy link
Contributor

Silly question, how does this different from conditional service?

The main purpose of this is to make the service registration independent of loading order. Relying on a specific order of things is always a latent bug, basically, so explicitly stating the requirements and adapting the loading order based on that is the only way to reliably deal with changing code and potentially even extending the service hierarchy.

@schlessera
Copy link
Contributor

Regarding the Settings <=> Stories_Settings_Controller bug:

  1. Settings is set up incorrectly. It is actually Delayed if you look at its register() method, but doesn't declare itself to be. So, Settings should be Delayed, with a registration_action of 'init'.
  2. The reordering implementation we currently have in the AMP plugin was the most simplistic implementation we could think of. However, it doesn't deal correctly with situations like the above. The interface and approach is still sound, but the actually reordering needs to happen in time, instead of only at the service array level. This means that when Service A depends on Service B, and Service B is delayed, then Service A needs to be Delayed to after Service B (using an action). This will then work reliably in terms of timing.
    However, keep in mind that this can still lead to issues. But these issues are design/dependency problems, not problems of the order resolution.

I'll adapt the PR in the AMP plugin to take delaying actions into account, and then we can adapt this one to ensure the problem is resolved.

@swissspidy
Copy link
Collaborator Author

  1. Settings is set up incorrectly. It is actually Delayed if you look at its register() method, but doesn't declare itself to be. So, Settings should be Delayed, with a registration_action of 'init'.

@schlessera Settings extends Service_Base which implements Delayed

@schlessera
Copy link
Contributor

@swissspidy Yes, but Settings' registration action does nothing, except for further delaying...
Implement dependency resolution for services by swissspidy · Pull Request #  2021-08-02 at 12 27 14 PM

@swissspidy
Copy link
Collaborator Author

swissspidy commented Aug 2, 2021

Oh, right, that doesn't make much sense 🤔 It's registered at init w/ prio 5 only to then call register_settings() at prio 10.

@schlessera
Copy link
Contributor

Yep. The method register_settings() is actually what Settings::register() is supposed to be, which even the name already suggests.

@swissspidy swissspidy marked this pull request as draft August 3, 2021 08:50
@schlessera
Copy link
Contributor

@swissspidy Is the main issue you've identified the first time around fixed now with the changes I made?

@swissspidy
Copy link
Collaborator Author

I believe so, yes! Thanks a lot!

Next I'll go through our services to properly identify & add dependencies where needed.

@swissspidy
Copy link
Collaborator Author

For any class that the injects settings/user_perfernce classes, don't these also need to be service dependency? To ensure that the user meta / settings is registered?

Nope, the get_requirements() method to add requirements is only needed for services that reference another service in the static is_needed() class.

So far Cross_Origin_Isolation is the only place where this is needed.

When an instance is injected via the constructor, ServiceBasedPlugin and SimpleInjector will create those instances first.

This is in line with how it works in the AMP plugin:

https://github.com/ampproject/amp-wp/blob/543aa27087c6fe1847746eda84facea44a5a90bf/src/Admin/ValidationCounts.php#L42-L61
https://github.com/ampproject/amp-wp/blob/543aa27087c6fe1847746eda84facea44a5a90bf/src/Admin/Polyfills.php#L38-L56
https://github.com/ampproject/amp-wp/blob/d1652901e9eb0316102fe8e1256f73d30d2c866e/src/Admin/PairedBrowsing.php#L69-L103

@spacedmonkey

This comment has been minimized.

@spacedmonkey
Copy link
Contributor

There are still a couple of instances of update_option and get_option in the code, do we need to fix these as well?

Copy link
Contributor

@spacedmonkey spacedmonkey left a comment

Choose a reason for hiding this comment

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

Great work! Let's ship it :shipit:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Code Quality Things that need a refactor, rewrite or just some good old developer ❤️ Type: Enhancement New feature or improvement of an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants