4 * Smarty Extension Loadplugin
6 * $smarty->loadPlugin() method
9 * @subpackage PluginsInternal
12 class Smarty_Internal_Method_LoadPlugin
15 * Cache of searched plugin files
19 public $plugin_files = array();
22 * Takes unknown classes and loads plugin files for them
23 * class name format: Smarty_PluginType_PluginName
24 * plugin filename format: plugintype.pluginname.php
26 * @param \Smarty $smarty
27 * @param string $plugin_name class plugin name to load
28 * @param bool $check check if already loaded
31 * @throws \SmartyException
33 public function loadPlugin(Smarty $smarty, $plugin_name, $check)
35 // if function or class exists, exit silently (already loaded)
36 if ($check && (is_callable($plugin_name) || class_exists($plugin_name, false))) {
39 if (!preg_match('#^smarty_((internal)|([^_]+))_(.+)$#i', $plugin_name, $match)) {
40 throw new SmartyException("plugin {$plugin_name} is not a valid name format");
42 if (!empty($match[ 2 ])) {
43 $file = SMARTY_SYSPLUGINS_DIR . strtolower($plugin_name) . '.php';
44 if (isset($this->plugin_files[ $file ])) {
45 if ($this->plugin_files[ $file ] !== false) {
46 return $this->plugin_files[ $file ];
52 $this->plugin_files[ $file ] = $file;
56 $this->plugin_files[ $file ] = false;
61 // plugin filename is expected to be: [type].[name].php
62 $_plugin_filename = "{$match[1]}.{$match[4]}.php";
63 $_lower_filename = strtolower($_plugin_filename);
64 if (isset($this->plugin_files)) {
65 if (isset($this->plugin_files[ 'plugins_dir' ][ $_lower_filename ])) {
66 if (!$smarty->use_include_path || $this->plugin_files[ 'plugins_dir' ][ $_lower_filename ] !== false) {
67 return $this->plugin_files[ 'plugins_dir' ][ $_lower_filename ];
70 if (!$smarty->use_include_path || $smarty->ext->_getIncludePath->isNewIncludePath($smarty)) {
71 unset($this->plugin_files[ 'include_path' ]);
73 if (isset($this->plugin_files[ 'include_path' ][ $_lower_filename ])) {
74 return $this->plugin_files[ 'include_path' ][ $_lower_filename ];
78 $_file_names = array($_plugin_filename);
79 if ($_lower_filename != $_plugin_filename) {
80 $_file_names[] = $_lower_filename;
82 $_p_dirs = $smarty->getPluginsDir();
83 if (!isset($this->plugin_files[ 'plugins_dir' ][ $_lower_filename ])) {
84 // loop through plugin dirs and find the plugin
85 foreach ($_p_dirs as $_plugin_dir) {
86 foreach ($_file_names as $name) {
87 $file = $_plugin_dir . $name;
89 $this->plugin_files[ 'plugins_dir' ][ $_lower_filename ] = $file;
93 $this->plugin_files[ 'plugins_dir' ][ $_lower_filename ] = false;
97 if ($smarty->use_include_path) {
98 foreach ($_file_names as $_file_name) {
99 // try PHP include_path
100 $file = $smarty->ext->_getIncludePath->getIncludePath($_p_dirs, $_file_name, $smarty);
101 $this->plugin_files[ 'include_path' ][ $_lower_filename ] = $file;
102 if ($file !== false) {