CodeIgniter Forums
Is it possible to override a "services" function from module code? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Is it possible to override a "services" function from module code? (/showthread.php?tid=75866)



Is it possible to override a "services" function from module code? - dave friend - 03-24-2020

In other words should \acme\Blog\Config\Services::logger() be able to override \CodeIgniter\Config\Services::logger() ?

Obviously $psr4 in Config\Autoload needs to have the acme\Blog namespace set up. But what else, if anything, needs to be provided for the override to happen?

Config\Modules is unchanged from the way it is distributed.


RE: Is it possible to override a "services" function from module code? - dave friend - 04-01-2020

Awfully quiet here...
Anybody?


RE: Is it possible to override a "services" function from module code? - kilishan - 04-01-2020

I don't believe a module can override an app-level service. Technically, they could use the injectMock method that is intended for testing (and might need some better restrictions put on it, come to think of it). In general, though, I don't think that should be allowed as the app itself should have final say on what Service is being used. It should not be possible for third-party modules to override an app-level setting. That's just bad form and asking for trouble. Smile


RE: Is it possible to override a "services" function from module code? - dave friend - 04-01-2020

(04-01-2020, 11:12 AM)kilishan Wrote: I don't believe a module can override an app-level service. Technically, they could use the injectMock method that is intended for testing (and might need some better restrictions put on it, come to think of it). In general, though, I don't think that should be allowed as the app itself should have final say on what Service is being used. It should not be possible for third-party modules to override an app-level setting. That's just bad form and asking for trouble. Smile

So using a modular approach to extend a "core" class (e.g. \CodeIgniter\Log\Logger) is not appropriate?
I don't see how that is any different from extending a "core" class by writing code in the App namespace.

What am I failing to see?


RE: Is it possible to override a "services" function from module code? - kilishan - 04-01-2020

If the app developer loses control of which class to use within a service because it can be overridden by any third-party modules that might be included via Composer, then it opens the application up to harmful attacks.

However, if the app-level Services config file takes precedence, then the app developer can always be sure they're specifying the correct class.

Extending a core class not a problem at all. The app developer can then choose to use that class by extending the App-level Services config. Your question was if the services config file methods could be overridden, if I understood correctly. And that, to me, is dangerous.