Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/vendors/Textpattern/Tag/Registry.php - 210 lines - 5810 bytes - Summary - Text - Print

Description: Handles template tag registry.

   1  <?php
   2  
   3  /*
   4   * Textpattern Content Management System
   5   * https://textpattern.com/
   6   *
   7   * Copyright (C) 2020 The Textpattern Development Team
   8   *
   9   * This file is part of Textpattern.
  10   *
  11   * Textpattern is free software; you can redistribute it and/or
  12   * modify it under the terms of the GNU General Public License
  13   * as published by the Free Software Foundation, version 2.
  14   *
  15   * Textpattern is distributed in the hope that it will be useful,
  16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18   * GNU General Public License for more details.
  19   *
  20   * You should have received a copy of the GNU General Public License
  21   * along with Textpattern. If not, see <https://www.gnu.org/licenses/>.
  22   */
  23  
  24  /**
  25   * Handles template tag registry.
  26   *
  27   * @since   4.6.0
  28   * @package Tag
  29   */
  30  
  31  namespace Textpattern\Tag;
  32  
  33  class Registry implements \Textpattern\Container\ReusableInterface
  34  {
  35      /**
  36       * Stores registered tags and attributes.
  37       *
  38       * @var array
  39       */
  40  
  41      private $tags = array();
  42      private $atts = array();
  43      private $params = array();
  44      private $attr = array();
  45  
  46      /**
  47       * Registers a tag.
  48       *
  49       * <code>
  50       * Txp::get('\Textpattern\Tag\Registry')->register(array('class', 'method'), 'tag');
  51       * </code>
  52       *
  53       * @param  callback    $callback The tag callback
  54       * @param  string|null $tag      The tag name
  55       * @return \Textpattern\Tag\Registry
  56       */
  57  
  58      public function register($callback, $tag = null)
  59      {
  60          // is_callable only checks syntax here to avoid autoloading
  61          if (is_callable($callback, true)) {
  62              if ($tag === null && is_string($callback)) {
  63                  $tag = $callback;
  64              } elseif (is_array($tag)) {
  65                  list($tag, $atts) = $tag + array(null, null);
  66              }
  67  
  68              if ($tag) {
  69                  $this->tags[$tag] = $callback;
  70                  $params = array_slice(func_get_args(), 2);
  71  
  72                  if (!empty($params)) {
  73                      $this->params[$tag] = $params;
  74                  }
  75  
  76                  if (isset($atts)) {
  77                      $this->atts[$tag] = (array)$atts;
  78                  }
  79              }
  80          }
  81  
  82          return $this;
  83      }
  84  
  85      /**
  86       * Registers an attribute.
  87       *
  88       * <code>
  89       * Txp::get('\Textpattern\Tag\Registry')->registerAtt(array('class', 'method'), 'tag');
  90       * </code>
  91       *
  92       * @param  callback    $callback The attribute callback
  93       * @param  string|null $tag      The attribute name
  94       * @return \Textpattern\Tag\Registry
  95       */
  96  
  97      public function registerAttr($callback, $tag = null)
  98      {
  99          // is_callable only checks syntax here to avoid autoloading
 100          if (is_bool($callback)) {
 101              foreach (do_list_unique($tag) as $tag) {
 102                  $this->attr[$tag] = $callback;
 103              }
 104          } elseif ($callback && is_callable($callback, true)) {
 105              if ($tag === null && is_string($callback)) {
 106                  $this->attr[$callback] = $callback;
 107              } else {
 108                  foreach (do_list_unique($tag) as $tag) {
 109                      $this->attr[$tag] = $callback;
 110                  }
 111              }
 112          }
 113  
 114          return $this;
 115      }
 116  
 117      /**
 118       * Processes a tag by name.
 119       *
 120       * @param  string      $tag   The tag
 121       * @param  array|null  $atts  An array of Attributes
 122       * @param  string|null $thing The contained statement
 123       * @return string|bool The tag's results (string) or FALSE on unknown tags
 124       */
 125  
 126      public function process($tag, array $atts = null, $thing = null)
 127      {
 128          if ($this->isRegistered($tag)) {
 129              $atts = (array)$atts;
 130  
 131              if (isset($this->atts[$tag])) {
 132                  $atts += $this->atts[$tag];
 133              }
 134  
 135              //TODO: switch to args unpacking for php 5.6+
 136              return isset($this->params[$tag]) ?
 137  //                (string) call_user_func($this->tags[$tag], (array)$atts, $thing, ...$this->params[$tag]) :
 138                  (string) call_user_func_array($this->tags[$tag], array_merge(array($atts, $thing), $this->params[$tag])) :
 139                  (string) call_user_func($this->tags[$tag], $atts, $thing);
 140          } else {
 141              return false;
 142          }
 143      }
 144  
 145      /**
 146       * Processes an attribute by name.
 147       *
 148       * @param  string      $tag   The attribute
 149       * @param  string|null $atts  The value of attribute
 150       * @param  string|null $thing The processed statement
 151       * @return string|bool The tag's results (string) or FALSE on unknown tags
 152       */
 153  
 154      public function processAttr($tag, $atts = null, $thing = null)
 155      {
 156          if ($this->isRegisteredAttr($tag)) {
 157              return (string) call_user_func($this->attr[$tag], $atts, $thing);
 158          } else {
 159              return false;
 160          }
 161      }
 162  
 163      /**
 164       * Checks if a tag is registered.
 165       *
 166       * @param  string $tag The tag
 167       * @return bool TRUE if the tag exists
 168       */
 169  
 170      public function isRegistered($tag)
 171      {
 172          return isset($this->tags[$tag]) && is_callable($this->tags[$tag]);
 173      }
 174  
 175      /**
 176       * Checks if an attribute is registered.
 177       *
 178       * @param  string $tag The tag
 179       * @return bool TRUE if the tag exists
 180       */
 181  
 182      public function isRegisteredAttr($tag)
 183      {
 184          return isset($this->attr[$tag]) && is_callable($this->attr[$tag]);
 185      }
 186  
 187      /**
 188       * Lists registered tags.
 189       *
 190       * @param  bool $is_attr tag or attr?
 191       * @return array
 192       */
 193  
 194      public function getRegistered($is_attr = false)
 195      {
 196          return $is_attr ? $this->attr : $this->tags;
 197      }
 198  
 199      /**
 200       * Tags getter.
 201       *
 202       * @param  string $tag
 203       * @return callable
 204       */
 205  
 206      public function getTag($tag)
 207      {
 208          return $this->isRegistered($tag) ? $this->tags[$tag] : false;
 209      }
 210  }

title

Description

title

Description

title

Description

title

title

Body