/** * This method is used internally by {@link nutshell\core\loader\Loader} to * create/return instances of plugins. This almost always happens when * something attempts to access the plugin via a plugin shortcut since these * always point to the Loader's plugin container. * * @internal * @access public * @static * @param Array $args - Args to be passed to the plugin constructor. * @throws Exception - If one of the base behaviours is not implemented. * @return nutshell\core\plugin\Plugin */ public static function getInstance(array $args = array()) { $className = get_called_class(); //If $instance is set, then it is definately a singleton. if (!isset($GLOBALS['NUTSHELL_PLUGIN_SINGLETON'][$className])) { $instance = new static(); self::applyBehaviours($instance); $GLOBALS['NUTSHELL_PLUGIN_SINGLETON'][$className] = $instance; $namespace = ObjectHelper::getNamespace($className); list(, , $plugin) = explode('\\', $namespace); if (is_dir(NS_HOME . 'plugin' . _DS_ . $plugin . _DS_)) { $dir = NS_HOME . 'plugin' . _DS_ . $plugin . _DS_; } else { if (is_dir(APP_HOME . 'plugin' . _DS_ . $plugin . _DS_)) { $dir = APP_HOME . 'plugin' . _DS_ . $plugin . _DS_; } else { throw new PluginException(PluginException::LIBRARY_NOT_FOUND, NS_HOME . 'plugin' . _DS_ . $plugin . _DS_, APP_HOME . 'plugin' . _DS_ . $plugin . _DS_); } } /** * Of note, the plugin which instaciates */ foreach (new DirectoryIterator($dir) as $iteration) { //We don't load folders or files from within folders. if ($iteration->isFile() && !(substr($iteration->getBasename(), 0, 1) == '.')) { require_once $iteration->getPathname(); } } $instance->init(); } //It must be a singleton. Return the instance. return $GLOBALS['NUTSHELL_PLUGIN_SINGLETON'][$className]; }
private function getParentPlugin() { $NS = ObjectHelper::getNamespace($this); $NSParts = explode('\\', $NS); if ($NSParts[1] == 'plugin') { if (isset($NSParts[2])) { $NSParts[2] = ucwords($NSParts[2]); } else { throw new PluginException(PluginException::NO_PARENT_PLUGIN, 'Unable to find parent plugin.'); } return $NSParts[0] . '\\' . $NSParts[1] . '\\' . $NSParts[2]; } else { throw new PluginException(PluginException::INCORRECT_CONTEXT, 'Attempted to use PluginExtension outside of plugin context.'); } }
public static function autoload($className) { $namespace = ObjectHelper::getNamespace($className); $className = ObjectHelper::getBaseClassName($className); //Check for an application plugin's library class. // if (strstr($namespace,'plugin\\')) // { // $namespaceParts =explode('\\',$namespace); // $where =array_shift($namespaceParts); // $filePath =false; // if ($where==='nutshell') // { // $filePath=NS_HOME.implode(_DS_,$namespaceParts)._DS_.$className.'.php'; // } // else if ($where==='application') // { // $filePath=APP_HOME.implode(_DS_,$namespaceParts)._DS_.$className.'.php'; // } // if (is_file($filePath)) // { // //Invoke the plugin. // require_once($filePath); // } // else // { // throw new ('Unable to autoload class "'.$namespace.'\\'.$className.'".'); // } // } // //Check for a plugin behaviour. // else if (strstr($namespace, 'behaviour\\')) { list(, , $plugin) = explode('\\', $namespace); $pathSuffix = 'plugin' . _DS_ . $plugin . _DS_ . 'behaviour' . _DS_ . $className . '.php'; if (is_file($file = NS_HOME . $pathSuffix)) { //Invoke the plugin. Nutshell::getInstance()->plugin->{ucfirst($plugin)}; } else { if (is_file($file = APP_HOME . $pathSuffix)) { Nutshell::getInstance()->plugin->{ucfirst($plugin)}; } else { throw new LoaderException(LoaderException::CANNOT_AUTOLOAD_CLASS, 'Unable to autoload class "' . $namespace . $className . '".'); } } } else { $namespaceParts = explode('\\', $namespace); $where = array_shift($namespaceParts); $filePath = false; if ($where === 'nutshell') { $filePath = NS_HOME . implode(_DS_, $namespaceParts) . _DS_ . $className . '.php'; } else { if ($where === 'application') { $filePath = APP_HOME . implode(_DS_, $namespaceParts) . _DS_ . $className . '.php'; } } if (is_file($filePath)) { //Invoke the plugin. require $filePath; } else { throw new LoaderException(LoaderException::CANNOT_AUTOLOAD_CLASS, 'Unable to autoload class "' . $namespace . '\\' . $className . '"', '"' . $filePath . '" does not exist'); } } }