[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_finder/src/Indexer/ -> Parser.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_finder
   6   *
   7   * @copyright   (C) 2011 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Finder\Administrator\Indexer;
  12  
  13  use Joomla\CMS\Filter\InputFilter;
  14  use Joomla\CMS\Language\Text;
  15  
  16  // phpcs:disable PSR1.Files.SideEffects
  17  \defined('_JEXEC') or die;
  18  // phpcs:enable PSR1.Files.SideEffects
  19  
  20  /**
  21   * Parser base class for the Finder indexer package.
  22   *
  23   * @since  2.5
  24   */
  25  abstract class Parser
  26  {
  27      /**
  28       * Parser support instances container.
  29       *
  30       * @var    Parser[]
  31       * @since  4.0.0
  32       */
  33      protected static $instances = array();
  34  
  35      /**
  36       * Method to get a parser, creating it if necessary.
  37       *
  38       * @param   string  $format  The type of parser to load.
  39       *
  40       * @return  Parser  A Parser instance.
  41       *
  42       * @since   2.5
  43       * @throws  \Exception on invalid parser.
  44       */
  45      public static function getInstance($format)
  46      {
  47          $format = InputFilter::getInstance()->clean($format, 'cmd');
  48  
  49          // Only create one parser for each format.
  50          if (isset(self::$instances[$format])) {
  51              return self::$instances[$format];
  52          }
  53  
  54          // Setup the adapter for the parser.
  55          $class = '\\Joomla\\Component\\Finder\\Administrator\\Indexer\\Parser\\' . ucfirst($format);
  56  
  57          // Check if a parser exists for the format.
  58          if (class_exists($class)) {
  59              self::$instances[$format] = new $class();
  60  
  61              return self::$instances[$format];
  62          }
  63  
  64          // Throw invalid format exception.
  65          throw new \Exception(Text::sprintf('COM_FINDER_INDEXER_INVALID_PARSER', $format));
  66      }
  67  
  68      /**
  69       * Method to parse input and extract the plain text. Because this method is
  70       * called from both inside and outside the indexer, it needs to be able to
  71       * batch out its parsing functionality to deal with the inefficiencies of
  72       * regular expressions. We will parse recursively in 2KB chunks.
  73       *
  74       * @param   string  $input  The input to parse.
  75       *
  76       * @return  string  The plain text input.
  77       *
  78       * @since   2.5
  79       */
  80      public function parse($input)
  81      {
  82          // If the input is less than 2KB we can parse it in one go.
  83          if (strlen($input) <= 2048) {
  84              return $this->process($input);
  85          }
  86  
  87          // Input is longer than 2Kb so parse it in chunks of 2Kb or less.
  88          $start = 0;
  89          $end = strlen($input);
  90          $chunk = 2048;
  91          $return = null;
  92  
  93          while ($start < $end) {
  94              // Setup the string.
  95              $string = substr($input, $start, $chunk);
  96  
  97              // Find the last space character if we aren't at the end.
  98              $ls = (($start + $chunk) < $end ? strrpos($string, ' ') : false);
  99  
 100              // Truncate to the last space character.
 101              if ($ls !== false) {
 102                  $string = substr($string, 0, $ls);
 103              }
 104  
 105              // Adjust the start position for the next iteration.
 106              $start += ($ls !== false ? ($ls + 1 - $chunk) + $chunk : $chunk);
 107  
 108              // Parse the chunk.
 109              $return .= $this->process($string);
 110          }
 111  
 112          return $return;
 113      }
 114  
 115      /**
 116       * Method to process input and extract the plain text.
 117       *
 118       * @param   string  $input  The input to process.
 119       *
 120       * @return  string  The plain text input.
 121       *
 122       * @since   2.5
 123       */
 124      abstract protected function process($input);
 125  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer