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

Dependency Injection / IoC #240

Open
tobeycodes opened this issue Oct 24, 2024 · 2 comments
Open

Dependency Injection / IoC #240

tobeycodes opened this issue Oct 24, 2024 · 2 comments

Comments

@tobeycodes
Copy link
Member

The scaffold currently has a pattern of Modules and a way to initialize these modules. These are useful but they do not allow for dependency injection which makes it harder to write easily testable code.

I think we should replace this in favour of a container and dependency injection. There are several popular packages to do this or we could maintain our own

https://github.com/silexphp/Pimple
https://github.com/PHP-DI/PHP-DI

@darylldoyle
Copy link
Collaborator

Thanks, @tobeycodes. I've added this to the Internal Tools working group agenda to be discussed. If you have any additional context/information that you think would be useful for those reviewing this, I'd greatly appreciate it if you could add it.

@tobeycodes
Copy link
Member Author

@darylldoyle I don't have too much context to add but I can perhaps help with an example of what I mean

class Bitly extends Module
  // CANT DO THIS WITH MODULES
  public function __construct(
    private BitlyAPI $bitlyAPI,
  ) {}

  public function add_hooks() {
    add_action('save_post' [$this, 'add_bitly_short_url_on_save']);
  }

  public function add_bitly_short_url_on_save(WP_Post $post) {
     // CANT DO THIS WITH MODULES
    $this->bitlyAPI

    // NEED TO DO THIS OR PUT THE API CODE IN THIS CLASS AND CODE IS TIGHTLY COUPLED
    $bitlyApi = new BitlyAPI();
  }
}

By not using depedency injection we are making our code less testable and tighly coupling our WP actions/filters with generic PHP code. It makes it harder to refactor and replace any code in the future

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants