WebDevStudios/oops-wp

Allow arguments to be passed to Services

Closed this issue · 1 comments

Currently, when we instantiate a Service, we call it without any arguments

'object'    => new $service_class(),

see src/Structure/ServiceRegistrar.php

If we allowed arguments to be passed here, we could inject the dependencies for the service (e.g. we could provide the Custom Post Types to be registered to the ContentRegistrar)

new ContentRegistrar([
    $recipeCpt,
    $bookCpt,
]);

We could do this with a dependency injector like auryn, however this would add some complexity to this project.

Overall, I think a move like this would allow us to move some of the specific configuration (e.g. post type configuration) from deep within the file tree to higher up (perhaps even in a config file).

For reference, the similar project mwpd/basic-scaffold uses a dependency injector.

This issue has already been addressed in version 0.2.1. Visibility of the init_services method was changed from private to protected to allow concrete implementations to provide their own service instantiation procedure. In fact, I had done this specifically so I could use Auryn in another project - see an example here.

Closing this out.