Textpattern PHP Cross Reference Content Management Systems

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

Description: Handles template tag registry.

   1  <?php
   2  
   3  /*
   4   * Textpattern Content Management System
   5   * http://textpattern.com
   6   *
   7   * Copyright (C) 2016 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 <http://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.
  37       *
  38       * @var array
  39       */
  40  
  41      private $tags = array();
  42  
  43      /**
  44       * Registers a tag.
  45       *
  46       * <code>
  47       * Txp::get('\Textpattern\Tag\Registry')->register(array('class', 'method'), 'tag');
  48       * </code>
  49       *
  50       * @param  callback    $callback The tag callback
  51       * @param  string|null $tag      The tag name
  52       * @return \Textpattern\Tag\Registry
  53       */
  54  
  55      public function register($callback, $tag = null)
  56      {
  57          // is_callable only checks syntax here to avoid autoloading
  58          if (is_callable($callback, true)) {
  59              if ($tag === null && is_string($callback)) {
  60                  $tag = $callback;
  61              }
  62  
  63              if ($tag) {
  64                  $this->tags[$tag] = $callback;
  65              }
  66          }
  67  
  68          return $this;
  69      }
  70  
  71      /**
  72       * Processes a tag by name.
  73       *
  74       * @param  string      $tag   The tag
  75       * @param  array|null  $atts  An array of Attributes
  76       * @param  string|null $thing The contained statement
  77       * @return string|bool The tag's results (string) or FALSE on unknown tags
  78       */
  79  
  80      public function process($tag, array $atts = null, $thing = null)
  81      {
  82          if ($this->isRegistered($tag)) {
  83              return (string) call_user_func($this->tags[$tag], (array)$atts, $thing);
  84          } else {
  85              return false;
  86          }
  87      }
  88  
  89      /**
  90       * Checks if a tag is registered.
  91       *
  92       * @param  string $tag The tag
  93       * @return bool TRUE if the tag exists
  94       */
  95  
  96      public function isRegistered($tag)
  97      {
  98          return isset($this->tags[$tag]) && is_callable($this->tags[$tag]);
  99      }
 100  
 101      /**
 102       * Lists registered tags.
 103       *
 104       * @return array
 105       */
 106  
 107      public function getRegistered()
 108      {
 109          return $this->tags;
 110      }
 111  }

title

Description

title

Description

title

Description

title

title

Body