Welcome Guest, Not a member yet? Register   Sign In
Best way to create a Service from a composer package?
#1

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
Reply
#2

See https://github.com/codeigniter4/shield/b...rvices.php
This is Services in CodeIgniter Shield.

You can use the services just like `service('auth')` by default.
See https://codeigniter4.github.io/CodeIgnit...-discovery
Reply
#3

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

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.
Reply
#5

(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?)
Reply
#6

> 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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB