CodeIgniter Forums
Best way to create a Service from a composer package? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: Best way to create a Service from a composer package? (/showthread.php?tid=82147)



Best way to create a Service from a composer package? - tgix - 06-17-2022

Hopefully someone can point me in the right direction.
I have been busy breaking apart our three monolithic projects into microservices. In this process I have introduced a composer package containing general stuff (constants, models and some libraries). This has generally worked well but I'd like to make more use of CI4's Services to decouple things a but more (to make it possible to introduce testing for example). 
My own composer package lives in Tangix\VirtualTester namespace and the various parts have their own App\ namespace (they are basically appstarter-projects). The Apps use stuff from the Tangix\VirtualTester namespace in the normal PHP way. However, some more complex things from the package I'd like to present as a Service to the applications. However, I don't understand if I can register Services from the package and I'd rather not have to mess with the code in each project, defining Services in app/Config/Services.php because this will fail one day as they get out of sync.
In Laravel there is a way to register Service Providers that I find interesting, is there anything like that in CI?
Thanks,
/Mattias


RE: Best way to create a Service from a composer package? - kenjis - 06-17-2022

See https://github.com/codeigniter4/shield/blob/develop/src/Config/Services.php
This is Services in CodeIgniter Shield.

You can use the services just like `service('auth')` by default.
See https://codeigniter4.github.io/CodeIgniter4/general/modules.html#auto-discovery


RE: Best way to create a Service from a composer package? - tgix - 06-17-2022

OK, so I need to update app/Config/Autoload.php with the namespace of the package I include?


RE: Best way to create a Service from a composer package? - MGatner - 06-17-2022

You shouldn’t need to update Autoload.php as long as the Composer package uses PSR autoloading. There’s not a definite right way to do what you’re after but my favorite is service wrapping. As an example here is a thin library I made that wraps Stripe’s SDK in as a CodeIgniter Service with a Config file to make it easy to instantiate and store things in .env: https://github.com/tattersoftware/codeigniter4-stripe

There is no reason you couldn’t do that in app/Config/ as well, I just needed it across multiple projects so it made sense to publish it separately.


RE: Best way to create a Service from a composer package? - tgix - 06-17-2022

(06-17-2022, 03:53 AM)MGatner Wrote: You shouldn’t need to update Autoload.php as long as the Composer package uses PSR autoloading. There’s not a definite right way to do what you’re after but my favorite is service wrapping. As an example here is a thin library I made that wraps Stripe’s SDK in as a CodeIgniter Service with a Config file to make it easy to instantiate and store things in .env: https://github.com/tattersoftware/codeigniter4-stripe

There is no reason you couldn’t do that in app/Config/ as well, I just needed it across multiple projects so it made sense to publish it separately.

Ah, cool. So you mean that the CI framework will magically find Tatter\Stripe\Config\Services and Services:Confusedtripe() will be available to the project including composer package tatter/stripe? That would be exactly what I want!
Will try (as soon as I get the testing going - https://forum.codeigniter.com/showthread.php?tid=82150 - do you have any ideas what I am missing?)


RE: Best way to create a Service from a composer package? - MGatner - 06-19-2022

> So you mean that the CI framework will magically find Tatter\Stripe\Config\Services and Services:Stripe() will be available to the project including composer package tatter/stripe

Exactly! It’s one of the things I love about services, they making publishing external dependencies very easy.