abdus Posted April 30, 2017 Posted April 30, 2017 I'm building a module using Comments field as reference. Inside Comments module and many others, there's always this expression which is used to inject dependencies (according to Wire class documentation) $fieldset = $this->wire(new InputfieldWrapper()); Is there a reason for injecting API variables inside objects? Because everything seems to work fine without. 1
Robin S Posted April 30, 2017 Posted April 30, 2017 Not 100% sure on this, but I think it will be because of the multi-instance support introduced in PW3. The example you gave from CommentFilterAkismet.module... $inputfields = $this->wire(new InputfieldWrapper()); ...if you check the PW2.x version of this file, before multi-instance support was added, it is simply... $inputfields = new InputfieldWrapper(); 3
LostKobrakai Posted May 2, 2017 Posted May 2, 2017 On 1.5.2017 at 0:45 AM, Robin S said: Not 100% sure on this, but I think it will be because of the multi-instance support introduced in PW3. Exactly this. 1
abdus Posted May 2, 2017 Author Posted May 2, 2017 17 minutes ago, LostKobrakai said: Exactly this. That makes it clear. I've also found this part from the core inside Wire class. <?php // /wire/core/Wire.php public function wire($name = '', $value = null, $lock = false) { // ... if(is_object($name)) { // make an object wired (inject ProcessWire instance to object) if($name instanceof WireFuelable) { if($this->_wire) $name->setWire($wire); // inject fuel, PW 3.0 if(is_string($value) && $value) { // set as new API var if API var name specified in $value $wire->fuel()->set($value, $name, $lock); } $value = $name; // return the provided instance } else { throw new WireException("Wire::wire(\$o) expected WireFuelable for \$o and was given " . get_class($name)); } } // ... } Here, setWire() method sets the instance. /** * Set the current ProcessWire instance for this object (PW 3.0) * * Specify no arguments to get, or specify a ProcessWire instance to set. * * #pw-internal * * @param ProcessWire $wire * */ public function setWire(ProcessWire $wire) { $this->_wire = $wire; $this->getInstanceNum(); } Another question @LostKobrakai: Does every single object from PW core needs an instance to be set? What classes are instance-critical, so to speak?
LostKobrakai Posted May 2, 2017 Posted May 2, 2017 If the class does use `$this->wire()` somewhere it's most-likely dependent on some state of the actual processwire instance, so the class does need to be wired after construction. I mean the config, installed modules, available pages and so on are all potentially different in different processwire instances if running in multi-instance mode. 1
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