Adam Kiss Posted August 3, 2012 Share Posted August 3, 2012 I've built an extendable module; you can install one 'master' module, and multiple 'slave' (content modules), but I'm struggling on how to tie them together; Option A: slave modules are autoloaded and hooked onto the master modules main function, and ran as needed. This is good from the terms of simplicity, but absolutely terrible - such a waste of memory (you hardly need all slave modules at once) Option B: Upon installation, slave module pings master module 'hey, I am here, you can find me under this key' and master module then builds directory of available slaves. What I'm struggling with is the most PW-compatible way to save a key=>value dictionary in a way available to module. I prefer option B, but so far I have just to ideas: either save it as serialized array (sounds incredibly stupid) or create repeater field with two text fields (sounds better, but repeater is optional and I'd like to have this as self-sufficient as possible). Need help, please. P.S.: I reveal more once it's at least in stable beta. Link to comment Share on other sites More sharing options...
ryan Posted August 4, 2012 Share Posted August 4, 2012 It sounds to me like the master module doesn't know ahead of time what the slave modules are? Otherwise, I'd say let the master call them up on demand as it needs them. But based on what you said, I'm going to assume the slaves might know about the master, but not the other way around. What I would do is have the slave modules descend from a common class or interface: class AdamSlave extends WireData implements Module { } // or interface AdamSlave { } Then let the slave modules extend or implement that (whether you add any functions to it or not doesn't matter for this concept): class AdamSlaveTest extends AdamSlaveModule { ... } // or class AdamSlaveTest extends WireData implements Module, AdamSlaveTest { ... } What you are doing above is just establishing a common type for your slave modules, so that your master can identify that they are slave modules. But since PW doesn't actually keep modules in memory (unless they are autoload) you also need to be able to identify them by class name. This you can do without loading all the modules because PW just keeps tiny placeholder objects for them in memory with a className() method that reflects the full modules real class name. In fact, the creation of a type above is just a secondary confirmation and you could technically get by without it (though I wouldn't). So what you need to do is name all the slaves in a consistent format like ProcessWire does for Inputfield or Fieldtype modules. Lets say that you've decided your name format will be "AdamSlave[something]" as above. You'd name your module classes like this: AdamSlaveThis AdamSlaveThat AdamSlaveSomething AdamSlaveSomethingElse (or whatever) Now you can probably guess how the master finds them: $slaves = array(); foreach(wire('modules')->find('className^=AdamSlave') as $slave) { // use instanceof just to double check this is really an AdamSlave // this is just in case someone else uses the name 'AdamSlave' for // something else in the future, it won't break your module if($slave instanceof AdamSlave) $slaves[] = $slave; } Now $slaves has the modules you want, ready to use (their init() method will have also been called at this point). Link to comment Share on other sites More sharing options...
Adam Kiss Posted August 4, 2012 Author Share Posted August 4, 2012 And if I wanted to keep a key based array of all functions supplemented by these slaves, how would i add it to the module? Thanks Ryan Link to comment Share on other sites More sharing options...
ryan Posted August 6, 2012 Share Posted August 6, 2012 Adam I'm not sure I understand the question fully, but if you want to poll for what functions are available in a given class, definitely take a look at PHP's Reflection classes (included with PHP5) as I think they may fit the bill for what you are asking. ProcessWire also uses them in a couple of instances, though for something different. Link to comment Share on other sites More sharing options...
Adam Kiss Posted September 18, 2012 Author Share Posted September 18, 2012 Oh god, I absolutely make no sense. let's say you have following modules installed: Master FunctionsThese FunctionsThose FunctionsWithBoobs Now, when you open a page, Master sees (in the textarea, somewhere), that you'd like to use functions 'cut' and 'paste'. It looks into its data store, and sees that both are located in the 'FunctionsThose' module, so it loads that module and runs those functions. The main idea is to go somewhere (module config? custom page?) and save records like these there: function: cut, module: FunctionsThose function: copy, module: FunctionsThose function: paste, module: FunctionsThose Link to comment Share on other sites More sharing options...
ryan Posted September 19, 2012 Share Posted September 19, 2012 PHP interfaces are the way that you deal with being able to identify something that is going to have this or that known method. Basically back to this. There's not anything different built into ProcessWire that would accomplish exactly what you are talking about, but of course you could manage your own registry for a custom need. I would avoid anything that relies upon using method_exists() just because that's only telling you that a method name exists and not that it's going to have the expected arguments and return, etc. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now