WildPHP / irc-bot

A simple and modular PHP IRC bot
MIT License
84 stars 24 forks source link

Automagically insert module instances into modules #40

Closed NanoSector closed 9 years ago

NanoSector commented 9 years ago

I was thinking, if we have a dependencies array we could make the module manager automatically insert instances.

Example: Say a module depends on Auth. It defines its dependency and gets loaded. The module manager automatically makes the Auth module available through $this->Auth. This so you don't have to care about getting module instances yourself. We want module development to be quick and effortless.

Amunak commented 9 years ago

The module manager should definitely manage the instances because other modules would probably just fuck it up. If something has auth module as a hard dependency then when it calls for it (by like $moduleManager->getModule('Auth')) it should be guaranteed that it gets its instance. The module manager will look if that module is loaded and if not load it and pass the instance.

Likewise when it's a soft dependency and the module isn't available the module manager should just throw an exception. It's up to whoever made the dependent module to handle that exception itself (as it should have known that the module is unavailable by whatever function it uses to tell the bot that it has this soft dependency). The module manager of course also has to catch exceptions from separate modules so that the whole bot doesn't crash.

It could (and should) actually be smart in that when a module throws an exception the module manager should kill it (and try to gracefully kill anything that has hard dependencies on it). All killed/unloaded modules should stop receiving events and stuff and be like not used in any way.

NanoSector commented 9 years ago

So you are saying that we should raise an exception instead of kicking the module off the stack?

How would we implement this on the module level?

Amunak commented 9 years ago

Not sure what you are referring to? Basically we just catch any exception a module raises and then kick the module out of the stack

NanoSector commented 9 years ago

I mean if an exception is raised, there's no way of the module to catch it because dependency initialisation happens before the module sets itself up.

NanoSector commented 9 years ago

Since we no longer use module instances for module communication (but instead events), we don't need this anymore.