Smarty::loadPlugin PHP Method

loadPlugin() public method

Takes unknown classes and loads plugin files for them class name format: Smarty_PluginType_PluginName plugin filename format: plugintype.pluginname.php
public loadPlugin ( string $plugin_name, boolean $check = true ) : string | boolean
$plugin_name string class plugin name to load
$check boolean check if already loaded
return string | boolean |boolean filepath of loaded file or false
    public function loadPlugin($plugin_name, $check = true)
    {
        // if function or class exists, exit silently (already loaded)
        if ($check && (is_callable($plugin_name) || class_exists($plugin_name, false))) {
            return true;
        }
        // Plugin name is expected to be: Smarty_[Type]_[Name]
        $_name_parts = explode('_', $plugin_name, 3);
        // class name must have three parts to be valid plugin
        // count($_name_parts) < 3 === !isset($_name_parts[2])
        if (!isset($_name_parts[2]) || strtolower($_name_parts[0]) !== 'smarty') {
            throw new SmartyException("plugin {$plugin_name} is not a valid name format");
            return false;
        }
        // if type is "internal", get plugin from sysplugins
        if (strtolower($_name_parts[1]) == 'internal') {
            $file = SMARTY_SYSPLUGINS_DIR . strtolower($plugin_name) . '.php';
            if (file_exists($file)) {
                require_once $file;
                return $file;
            } else {
                return false;
            }
        }
        // plugin filename is expected to be: [type].[name].php
        $_plugin_filename = "{$_name_parts[1]}.{$_name_parts[2]}.php";
        $_stream_resolve_include_path = function_exists('stream_resolve_include_path');
        // loop through plugin dirs and find the plugin
        foreach ($this->getPluginsDir() as $_plugin_dir) {
            $names = array($_plugin_dir . $_plugin_filename, $_plugin_dir . strtolower($_plugin_filename));
            foreach ($names as $file) {
                if (file_exists($file)) {
                    require_once $file;
                    return $file;
                }
                if ($this->use_include_path && !preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $_plugin_dir)) {
                    // try PHP include_path
                    if ($_stream_resolve_include_path) {
                        $file = stream_resolve_include_path($file);
                    } else {
                        $file = Smarty_Internal_Get_Include_Path::getIncludePath($file);
                    }
                    if ($file !== false) {
                        require_once $file;
                        return $file;
                    }
                }
            }
        }
        // no plugin loaded
        return false;
    }

Usage Example

 /**
  * Load compiler object
  *
  * @throws \SmartyException
  */
 public function loadCompiler()
 {
     if (!class_exists($this->source->handler->compiler_class)) {
         $this->smarty->loadPlugin($this->source->handler->compiler_class);
     }
     $this->compiler = new $this->source->handler->compiler_class($this->source->handler->template_lexer_class, $this->source->handler->template_parser_class, $this->smarty);
 }
All Usage Examples Of Smarty::loadPlugin