WebDevStudios / oops-wp

A collection of abstract classes, interfaces, and traits to promote object-oriented programming practices in WordPress.
57 stars 9 forks source link

Allow arguments to be passed to Services #32

Closed salcode closed 4 years ago

salcode commented 4 years ago

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.

jmichaelward commented 4 years ago

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.