/** Load single plugin. @param[in] $compiler @ref TemplateCompilerEx instance @param[in] $node Optional @ref TemplateNodeEx instance @param[in] $plugin Plugin name @param[in] $pluginFile Plugin's full filename (optional) @param[in] $noThrow If @c true, no exception will be thrown on error (boolean @c false will be returned) @retval true Plugin has been loaded @retval false Plugin is invalid/non-existant */ public function load(TemplateCompilerEx $compiler, $node, $plugin, $pluginFile = null, $noThrow = false) { // if already loaded, then abort if (isset($this->plugins[$plugin])) { return; } // sanity check TemplateUtils::checkIfAllowed($compiler, 'plugin', $plugin, $node); if (!$pluginFile) { $pluginFile = $this->findPlugin($plugin); } $className = 'Template' . $plugin . 'Plugin'; if ($pluginFile === false || !(include_once $pluginFile) || !class_exists($className) || !TemplateUtils::doesImplement($className, 'ITemplatePlugin')) { if ($noThrow) { return false; } throw new TemplateError('Could not load plugin: "' . $plugin . '". Either file or main class does not exists, or is invalid', TemplateError::E_INVALID_PLUGIN); } // create instance and register handlers $pluginObj = new $className(); $handlers = $pluginObj->providedHandlers(); if (!isset($handlers['tags'])) { $handlers['tags'] = array(); } if (!isset($handlers['filters'])) { $handlers['filters'] = array(); } if (!isset($handlers['hooks'])) { $handlers['hooks'] = array(); } $this->register($plugin, $pluginObj, 'tag', $handlers['tags']); $this->register($plugin, $pluginObj, 'filter', $handlers['filters']); // too special case after all $this->registerHooks($plugin, $pluginObj, $handlers['hooks']); $this->plugins[$plugin] = $pluginObj; }
/** Verifies correctness of given element. Checks whether: <ul> <li>Element exists</li> <li>Element's handler is callable</li> <li>Enough arguments have been provided</li> </ul> @param[in] $node %Template node representing element @param[in] $element Element type - @c 'tag' or @c 'filter' @param[in] $name Element's name @param[in] $args Element's arguments @return Element's info array */ private function commonVerifyElement(TemplateNodeEx $node, $element, &$name, array &$args) { if (!in_array($element, array('tag', 'filter'))) { TemplateUtils::panic(__FILE__, __LINE__); } $this->raiseIf(!$this->plugins->known($element, $name), $node, 'Unknown ' . $element . ' "' . $name . '" encountered', $element == 'tag' ? TemplateError::E_UNKNOWN_TAG : TemplateError::E_UNKNOWN_FILTER); $elementInfo = $this->plugins->get($element, $name); TemplateUtils::checkIfAllowed($this, $element, $name, $node); if (!is_callable($elementInfo['handler'])) { throw new TemplateError('Invalid handler [' . TemplateUtils::strip(var_export($elementInfo['handler'], true)) . '] supplied for ' . $element . ' "' . $name . '" by plugin "' . $elementInfo['plugin'] . '"', TemplateError::E_INVALID_HANDLER); } if (isset($elementInfo['minArgs'])) { $this->raiseIf(count($args) < $elementInfo['minArgs'], $node, 'Invalid ' . $element . ' call - ' . $element . ' "' . $name . '" requires at least ' . '"' . $elementInfo['minArgs'] . '" arguments ', TemplateError::E_INVALID_SYNTAX); } return $elementInfo; }