Welcome Guest, Not a member yet? Register   Sign In
Is it possible to override a "services" function from module code?
#1

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

Awfully quiet here...
Anybody?
Reply
#3

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

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

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




Theme © iAndrew 2016 - Forum software by © MyBB