Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/vendors/Textpattern/Textfilter/Registry.php - 211 lines - 4858 bytes - Summary - Text - Print

   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  namespace Textpattern\Textfilter;
  25  
  26  /**
  27   * A registry of Textfilters interfaces those to the core.
  28   *
  29   * @since   4.6.0
  30   * @package Textfilter
  31   */
  32  
  33  class Registry implements \ArrayAccess, \IteratorAggregate, \Textpattern\Container\ReusableInterface
  34  {
  35      /**
  36       * An array of filters.
  37       *
  38       * @var array
  39       */
  40  
  41      protected $filters;
  42  
  43      /**
  44       * Stores an array of filter titles.
  45       *
  46       * @var array
  47       */
  48  
  49      protected $titles;
  50  
  51      /**
  52       * Constructor.
  53       *
  54       * Creates core Textfilters according to a preference and registers all
  55       * available filters with the core.
  56       *
  57       * This method triggers 'textfilter.register' callback
  58       * event.
  59       */
  60  
  61      public function __construct()
  62      {
  63          if ($filters = get_pref('admin_textfilter_classes')) {
  64              foreach (do_list($filters) as $filter) {
  65                  new $filter;
  66              }
  67          } else {
  68              new Plain();
  69              new Nl2Br();
  70              new Textile();
  71          }
  72  
  73          $this->filters = array();
  74          callback_event('textfilter', 'register', 0, $this);
  75      }
  76  
  77      /**
  78       * Gets an array map of filter keys vs. titles.
  79       *
  80       * @return array Map of 'key' => 'title' for all Textfilters
  81       */
  82  
  83      public function getMap()
  84      {
  85          if ($this->titles === null) {
  86              $this->titles = array();
  87  
  88              foreach ($this as $filter) {
  89                  $this->titles[$filter->getKey()] = $filter->title;
  90              }
  91          }
  92  
  93          return $this->titles;
  94      }
  95  
  96      /**
  97       * Filter raw input text by calling one of our known Textfilters by its key.
  98       *
  99       * Invokes the 'textfilter.filter' pre- and post-callbacks.
 100       *
 101       * @param  string $key     The Textfilter's key
 102       * @param  string $thing   Raw input text
 103       * @param  array  $context Filter context ('options' => array, 'field' => string, 'data' => mixed)
 104       * @return string Filtered output text
 105       * @throws Exception
 106       */
 107  
 108      public function filter($key, $thing, $context)
 109      {
 110          // Preprocessing, anyone?
 111          callback_event_ref('textfilter', 'filter', 0, $thing, $context);
 112  
 113          if (isset($this[$key])) {
 114              $thing = $this[$key]->filter($thing, $context['options']);
 115          } else {
 116              throw new Exception(gTxt('invalid_argument', array('{name}' => 'key')));
 117          }
 118  
 119          // Postprocessing, anyone?
 120          callback_event_ref('textfilter', 'filter', 1, $thing, $context);
 121  
 122          return $thing;
 123      }
 124  
 125      /**
 126       * Get help text for a certain Textfilter.
 127       *
 128       * @param  string $key The Textfilter's key
 129       * @return string HTML for human-readable help
 130       */
 131  
 132      public function getHelp($key)
 133      {
 134          if (isset($this[$key])) {
 135              return $this[$key]->getHelp();
 136          }
 137  
 138          return '';
 139      }
 140  
 141      /**
 142       * ArrayAccess interface to our set of filters.
 143       *
 144       * @param string $key
 145       * @param string $filter
 146       * @see   ArrayAccess
 147       */
 148  
 149      public function offsetSet($key, $filter)
 150      {
 151          if ($key === null) {
 152              $key = $filter->getKey();
 153          }
 154  
 155          $this->filters[$key] = $filter;
 156      }
 157  
 158      /**
 159       * Returns the value at specified offset.
 160       *
 161       * @param  string $key
 162       * @return string The value
 163       * @see    ArrayAccess
 164       */
 165  
 166      public function offsetGet($key)
 167      {
 168          if ($this->offsetExists($key)) {
 169              return $this->filters[$key];
 170          }
 171  
 172          return null;
 173      }
 174  
 175      /**
 176       * Whether an offset exists.
 177       *
 178       * @param  string $key
 179       * @return bool
 180       * @see    ArrayAccess
 181       */
 182  
 183      public function offsetExists($key)
 184      {
 185          return isset($this->filters[$key]);
 186      }
 187  
 188      /**
 189       * Offset to unset.
 190       *
 191       * @param string $key
 192       * @see   ArrayAccess
 193       */
 194  
 195      public function offsetUnset($key)
 196      {
 197          unset($this->filters[$key]);
 198      }
 199  
 200      /**
 201       * IteratorAggregate interface.
 202       *
 203       * @return ArrayIterator
 204       * @see    IteratorAggregate
 205       */
 206  
 207      public function getIterator()
 208      {
 209          return new \ArrayIterator($this->filters);
 210      }
 211  }

title

Description

title

Description

title

Description

title

title

Body