Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

$smarty -> register -> defaultPluginHandler() does not

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty 3
View previous topic :: View next topic  
Author Message
tundracomp
Smarty n00b


Joined: 23 Oct 2010
Posts: 3

PostPosted: Sat Oct 23, 2010 5:23 am    Post subject: $smarty -> register -> defaultPluginHandler() does not Reply with quote

Hi everyone,

If I get it right then
$smarty -> register -> defaultPluginHandler($function)
should register a smarty function which is called in case Smarty cannot find a plugin by itsself...

default_plugin_handler.php
Code:

<?php
   require_once("libs/Smarty.class.php");
   
   function handler()
   {
      print("Plugin not found!");
   }
   
   $smarty = new Smarty();
   $smarty -> register -> defaultPluginHandler('handler');
   $smarty -> display("default_plugin_handler.tpl");
?>

default_plugin_handler.tpl
Code:

Test<br>
{invalid}<br>
I'm not here!


The above example of corse results in an fatal error but before that happens there should be the message "Plugin not found!" be printed.

I hope I get everything right!
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Sat Oct 23, 2010 12:08 pm    Post subject: Reply with quote

It's not implemented. And I currently see no good case for a use.
Back to top
View user's profile Send private message
tundracomp
Smarty n00b


Joined: 23 Oct 2010
Posts: 3

PostPosted: Sun Oct 24, 2010 5:12 am    Post subject: Patch Reply with quote

The following tiny patch can be applied on the sysplugins/smarty_internal_templatecompilerbase.php and solves this problem:

Code:

299,304c299,308
<         $filename = $this->smarty->loadPlugin($function);

<         

<         if (!$filename && is_callable($this->smarty->default_plugin_handler_func)) {

<            $function = call_user_func($this->smarty->default_plugin_handler_func, $plugin_name, $type, $this->smarty);

<         }

<         elseif ($filename) {

---
>         $found = false;

>         foreach((array)$this->smarty->plugins_dir as $_plugin_dir) {

>             $file = rtrim($_plugin_dir, '/\\') . DS . $type . '.' . $plugin_name . '.php';

>             if (file_exists($file)) {

>                 // require_once($file);

>                 $found = true;

>                 break;

>             }

>         }

>         if ($found) {

306c310
<                 $this->template->required_plugins['nocache'][$plugin_name][$type]['file'] = $filename;

---
>                 $this->template->required_plugins['nocache'][$plugin_name][$type]['file'] = $file;

309c313
<                 $this->template->required_plugins['compiled'][$plugin_name][$type]['file'] = $filename;

---
>                 $this->template->required_plugins['compiled'][$plugin_name][$type]['file'] = $file;

Please note that closures will not work with this patch since the smarty template compiler tries to convert them to a string which will result in an fatal error.

The useage of this plugin handler is described here:
http://www.smarty.net/forums/viewtopic.php?t=15207
Except for the fact that the plugin-loading function should return the function name if a plugin is loaded.
Back to top
View user's profile Send private message
tundracomp
Smarty n00b


Joined: 23 Oct 2010
Posts: 3

PostPosted: Thu Nov 18, 2010 5:37 am    Post subject: Better patch Reply with quote

This better patch integrates a bit deeper into smarty and loads functions, modifiers, compiler functions, compiler modifiers, resources and filters on demand when needed. Additionally it integrates with the modifier and function compilers and allows you to use closures as plugin functions.
Code:

diff --recursive --context --ignore-space-change --new-file Smarty-3.0.4/libs/Smarty.class.php smarty/libs/Smarty.class.php
*** Smarty-3.0.4/libs/Smarty.class.php   2010-11-13 13:29:33.000000000 -0600
--- smarty/libs/Smarty.class.php   2010-11-17 23:27:39.000000000 -0600
***************
*** 112,117 ****
--- 112,118 ----
     const PLUGIN_BLOCK = 'block';
     const PLUGIN_COMPILER = 'compiler';
     const PLUGIN_MODIFIER = 'modifier';
+    const PLUGIN_MODIFIERCOMPILER = 'modifiercompiler';
 
     /**
     * static variables
***************
*** 130,135 ****
--- 131,138 ----
      public $template_dir = null;
      // default template handler
      public $default_template_handler_func = null;
+     // default plugin handler
+     public $default_plugin_handler_func = null;
      // compile directory
      public $compile_dir = null;
      // plugins directory
diff --recursive --context --ignore-space-change --new-file Smarty-3.0.4/libs/sysplugins/smarty_internal_compile_private_modifier.php smarty/libs/sysplugins/smarty_internal_compile_private_modifier.php
*** Smarty-3.0.4/libs/sysplugins/smarty_internal_compile_private_modifier.php   2010-11-13 13:28:58.000000000 -0600
--- smarty/libs/sysplugins/smarty_internal_compile_private_modifier.php   2010-11-17 22:55:47.000000000 -0600
***************
*** 33,44 ****
--- 33,56 ----
              $modifier = $single_modifier[0];

        $single_modifier[0] = $output;

              $params = implode(',', $single_modifier);

+             

+             if (!isset($compiler->smarty->registered_plugins[Smarty::PLUGIN_MODIFIER][$modifier])) {

+                 // try registering modifier on-the-fly

+                 if (is_callable($this->smarty->default_plugin_handler_func)) {

+                     call_user_func($this->smarty->default_plugin_handler_func, $modifier, Smarty::PLUGIN_MODIFIER, $this->smarty);

+                 }

+             }

+             

              // check for registered modifier

              if (isset($compiler->smarty->registered_plugins[Smarty::PLUGIN_MODIFIER][$modifier])) {

                  $function = $compiler->smarty->registered_plugins[Smarty::PLUGIN_MODIFIER][$modifier][0];

                  if (!is_array($function)) {

+                    if (is_string($function)) {

                          $output = "{$function}({$params})";

                      } else {

+                         $output = 'call_user_func_array(\$_smarty_tpl->smarty->registered_plugins[Smarty::PLUGIN_MODIFIER][\'' . $modifier . '\'][0], ' . $params . ')';

+                     }

+                 } else {

                      if (is_object($function[0])) {

                          $output = '$_smarty_tpl->smarty->registered_plugins[Smarty::PLUGIN_MODIFIER][\'' . $modifier . '\'][0][0]->' . $function[1] . '(' . $params . ')';

                      } else {

***************
*** 49,54 ****
--- 61,68 ----
              } else if ($compiler->smarty->loadPlugin('smarty_modifiercompiler_' . $modifier)) {

                  $plugin = 'smarty_modifiercompiler_' . $modifier;

                  $output = $plugin($single_modifier, $compiler);

+             } else if (isset($this->smarty->registered_plugins[Smarty::PLUGIN_MODIFIERCOMPILER][$modifier][0])) {

+                 $output = call_user_func($this->smarty->registered_plugins[Smarty::PLUGIN_MODIFIERCOMPILER][$modifier][0], $single_modifier, $this->smarty);

                  // check for plugin modifier

              } else if ($function = $this->compiler->getPlugin($modifier, Smarty::PLUGIN_MODIFIER)) {

                  $output = "{$function}({$params})";

***************
*** 58,63 ****
--- 72,82 ----
                  if (!is_object($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedModifier($modifier, $this->compiler)) {

                      $output = "{$modifier}({$params})";

                  }

+                 // check for on-the-fly compiler modifier registration

+             } else if (is_callable($this->smarty->default_plugin_handler_func)

+                    &&  call_user_func($this->smarty->default_plugin_handler_func, $modifier, Smarty::PLUGIN_MODIFIERCOMPILER, $this->smarty)

+                    &&  isset($this->smarty->registered_plugins[Smarty::PLUGIN_MODIFIERCOMPILER][$modifier][0])) {

+                 $output = call_user_func($this->smarty->registered_plugins[Smarty::PLUGIN_MODIFIERCOMPILER][$modifier][0], $args, $this->smarty);

              } else {

                  $this->compiler->trigger_template_error ("unknown modifier \"" . $modifier . "\"", $this->compiler->lex->taglineno);

              }

diff --recursive --context --ignore-space-change --new-file Smarty-3.0.4/libs/sysplugins/smarty_internal_compile_private_registered_function.php smarty/libs/sysplugins/smarty_internal_compile_private_registered_function.php
*** Smarty-3.0.4/libs/sysplugins/smarty_internal_compile_private_registered_function.php   2010-11-13 13:28:58.000000000 -0600
--- smarty/libs/sysplugins/smarty_internal_compile_private_registered_function.php   2010-11-17 22:59:50.000000000 -0600
***************
*** 50,61 ****
          $_params = 'array(' . implode(",", $_paramsArray) . ')';

          $function = $compiler->smarty->registered_plugins[Smarty::PLUGIN_FUNCTION][$tag][0];

          // compile code

!         if (!is_array($function)) {

!             $output = "<?php echo {$function}({$_params},\$_smarty_tpl);?>\n";

!         } else if (is_object($function[0])) {

!             $output = "<?php echo \$_smarty_tpl->smarty->registered_plugins[Smarty::PLUGIN_FUNCTION]['{$tag}'][0][0]->{$function[1]}({$_params},\$_smarty_tpl);?>\n";

          } else {

!             $output = "<?php echo {$function[0]}::{$function[1]}({$_params},\$_smarty_tpl);?>\n";

          }

          return $output;

      }

--- 50,63 ----
          $_params = 'array(' . implode(",", $_paramsArray) . ')';

          $function = $compiler->smarty->registered_plugins[Smarty::PLUGIN_FUNCTION][$tag][0];

          // compile code

!         if (!is_array($function) && is_string($function)) {

!             $output = "<?php echo {$function}({$_params},\$_smarty_tpl->smarty);?>\n";

!         } else if (is_array($function) && is_object($function[0])) {

!             $output = "<?php echo \$_smarty_tpl->smarty->registered_plugins[Smarty::PLUGIN_FUNCTION]['{$tag}'][0][0]->{$function[1]}({$_params},\$_smarty_tpl->smarty);?>\n";

!         } else if (is_array($function) && is_class($function[0])) {

!             $output = "<?php echo {$function[0]}::{$function[1]}({$_params},\$_smarty_tpl->smarty);?>\n";

          } else {

!             $output = "<?php echo call_user_func(\$_smarty_tpl->smarty->registered_plugins[Smarty::PLUGIN_FUNCTION]['{$tag}'][0],{$_params},\$_smarty_tpl->smarty);?>\n";

          }

          return $output;

      }

diff --recursive --context --ignore-space-change --new-file Smarty-3.0.4/libs/sysplugins/smarty_internal_filter.php smarty/libs/sysplugins/smarty_internal_filter.php
*** Smarty-3.0.4/libs/sysplugins/smarty_internal_filter.php   2010-11-13 13:28:58.000000000 -0600
--- smarty/libs/sysplugins/smarty_internal_filter.php   2010-11-17 23:11:24.000000000 -0600
***************
*** 72,84 ****
      {
          $_plugin = "smarty_{$type}filter_{$name}";
          $_filter_name = $_plugin;
!         if ($this->smarty->loadPlugin($_plugin)) {
              if (class_exists($_plugin, false)) {
                  $_plugin = array($_plugin, 'execute');
              }
              if (is_callable($_plugin)) {
!                 return $this->smarty->registered_filters[$type][$_filter_name] = $_plugin;
!             }
          }
          throw new SmartyException("{$type}filter \"{$name}\" not callable");
          return false;
--- 72,89 ----
      {
          $_plugin = "smarty_{$type}filter_{$name}";
          $_filter_name = $_plugin;
!         if (!$this->loadPlugin($_plugin)) {
!            // try on-the-fly registration if filter cannot be found
!             if (is_callable($this->default_plugin_handler_func)) {
!                call_user_func($this->default_plugin_handler_func, "{$type}filter", $name, $this);
!             }
!         }
!         
          if (class_exists($_plugin, false)) {
              $_plugin = array($_plugin, 'execute');
          }
          if (is_callable($_plugin)) {
!             return $this->registered_filters[$type][$_filter_name] = $_plugin;
          }
          throw new SmartyException("{$type}filter \"{$name}\" not callable");
          return false;
diff --recursive --context --ignore-space-change --new-file Smarty-3.0.4/libs/sysplugins/smarty_internal_templatecompilerbase.php smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php
*** Smarty-3.0.4/libs/sysplugins/smarty_internal_templatecompilerbase.php   2010-11-13 13:28:58.000000000 -0600
--- smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php   2010-11-17 23:25:16.000000000 -0600
***************
*** 189,194 ****
--- 189,204 ----
                          if ($type == Smarty::PLUGIN_FUNCTION || $type == Smarty::PLUGIN_BLOCK) {

                              return $this->callTagCompiler('private_registered_' . $type, $args, $parameter, $tag);

                          }

+                     } elseif (is_callable($this->smarty->default_plugin_handler_func)) {

+                         if (call_user_func($this->smarty->default_plugin_handler_func, $tag, $type, $this->smarty)) {

+                             if (isset($this->smarty->registered_plugins[$type][$tag])) {

+                                 if ($type == Smarty::PLUGIN_COMPILER) {

+                                     return $this->smarty->registered_plugins[$type][$tag][0]($args, $this);

+                                 } else {

+                                     return $this->callTagCompiler('private_registered_' . $type, $args, $tag, $this->smarty->registered_plugins[$type][$tag][0]);

+                                 }

+                            }

+                         }

                      }

                  }

                  // check plugins from plugins folder

***************
*** 315,330 ****
          }

          // loop through plugin dirs and find the plugin

          $function = 'smarty_' . $type . '_' . $plugin_name;

!         $found = false;

!         foreach((array)$this->smarty->plugins_dir as $_plugin_dir) {

!             $file = rtrim($_plugin_dir, '/\\') . DS . $type . '.' . $plugin_name . '.php';

!             if (file_exists($file)) {

!                 // require_once($file);

!                 $found = true;

!                 break;

!             }

!         }

!         if ($found) {

              if ($this->template->caching && ($this->nocache || $this->tag_nocache)) {

                  $this->template->required_plugins['nocache'][$plugin_name][$type]['file'] = $file;

                  $this->template->required_plugins['nocache'][$plugin_name][$type]['function'] = $function;

--- 325,333 ----
          }

          // loop through plugin dirs and find the plugin

          $function = 'smarty_' . $type . '_' . $plugin_name;

!         $file     = $this->smarty->loadPlugin($function);

!         

!         if ($file) {

              if ($this->template->caching && ($this->nocache || $this->tag_nocache)) {

                  $this->template->required_plugins['nocache'][$plugin_name][$type]['file'] = $file;

                  $this->template->required_plugins['nocache'][$plugin_name][$type]['function'] = $function;

diff --recursive --context --ignore-space-change --new-file Smarty-3.0.4/libs/sysplugins/smarty_internal_template.php smarty/libs/sysplugins/smarty_internal_template.php
*** Smarty-3.0.4/libs/sysplugins/smarty_internal_template.php   2010-11-13 13:28:58.000000000 -0600
--- smarty/libs/sysplugins/smarty_internal_template.php   2010-11-17 23:19:06.000000000 -0600
***************
*** 731,736 ****
--- 731,744 ----
                          }
                          return new Smarty_Internal_Resource_Stream($this->smarty);
                      } else {
+                         if (is_callable($this->smarty->default_plugin_handler_func)) {
+                             if (call_user_func($this->smarty->default_plugin_handler_func, $resource_type, 'resource', $this->smarty)) {
+                                 if (isset($this->smarty->registered_resources[$resource_type])) {
+                                      return new Smarty_Internal_Resource_Registered($this->smarty);
+                                 }
+                             }
+                         }
+                         
                          throw new SmartyException('Unkown resource type \'' . $resource_type . '\'');
                      }
                  }


Please give me any feedback on things I have done wrong/not implemented (correctly).
This patch is fully compatible to the documentation on: [url=http://www.smarty.net/forums/viewtopic.php?t=15207]
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Thu Oct 13, 2011 11:15 am    Post subject: Reply with quote

From what I can see simpleinvoices does still use Smarty2 and not Smarty3.

But I can't tell what a valid configuration of simpleinvoices could be.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty 3 All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP