/** * Get the binding corresponding to the specified selector. * Better for use like this : ModuleClass::bind($selector)->getClassName() * * @param string $selector * @param bool $singleton if this binding should be a singleton or not * @return ModuleClassBinding * @see ModuleClass::bind * @since 1.1 * @experimental This method is EXPERIMENTAL. It could be changed in future version */ public static function bind($selector) { $osel = Selector\Factory::create($selector, 'iface'); $s = $osel->toString(true); if (!isset(self::$_bindings[$s])) { self::$_bindings[$s] = new ModuleClassBinding($osel); } return self::$_bindings[$s]; }
/** * Get the selector to the binded class * Protected because this does not work if called after a simple __construct() and a toInstance() * * @return string */ protected function _getClassSelector() { $class_selector = null; // the instance is not already created if ($this->toSelector === null && $this->instance === null) { $str_selector = $this->fromSelector->toString(); $str_selector_long = $this->fromSelector->toString(true); // 1) verify that a default implementation is specified in the jelix config file $config = App::config(); if (isset($config->classbindings) && count($config->classbindings)) { $conf = $config->classbindings; // No '~' allowed as key of a ini file, we use '-' instead $conf_selector = str_replace('~', '-', $str_selector); $conf_selector_long = str_replace('~', '-', $str_selector_long); // get the binding corresponding to selector, long or not $str_fromselector = null; if (isset($conf[$conf_selector])) { $str_fromselector = $conf_selector; } elseif (isset($conf[$conf_selector_long])) { $str_fromselector = $conf_selector_long; } if ($str_fromselector !== null) { $this->fromSelector = Selector\Factory::create($str_selector_long, 'iface'); return $this->toSelector = new Selector\ClassSelector($conf[$str_fromselector]); } } // 2) see if a default implementation is specified in the source class $constname = $this->fromSelector->className . '::JBINDING_BINDED_IMPLEMENTATION'; if (defined($constname)) { // check first, constant() crashes on some php version when the const does not exist $class_selector = constant($constname); if ($class_selector !== null) { return $this->toSelector = new Selector\ClassSelector($class_selector); } } // 3) If the source is a class, then use it as the default implementation if (true === $this->fromSelector instanceof Selector\ClassSelector) { return $this->toSelector = $this->fromSelector; } throw new \jException('jelix~errors.bindings.nobinding', array($this->fromSelector->toString(true))); } }