[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Configurator/JavaScript/ -> CallbackGenerator.php (source)

   1  <?php
   2  
   3  /*
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2019 The s9e Authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Configurator\JavaScript;
   9  class CallbackGenerator
  10  {
  11      public $callbacks = [
  12          'tags.*.attributes.*.filterChain.*' => [
  13              'attrValue' => '*',
  14              'attrName'  => '!string'
  15          ],
  16          'tags.*.filterChain.*' => [
  17              'tag'       => '!Tag',
  18              'tagConfig' => '!Object'
  19          ]
  20      ];
  21      protected $encoder;
  22  	public function __construct()
  23      {
  24          $this->encoder = new Encoder;
  25      }
  26  	public function replaceCallbacks(array $config)
  27      {
  28          foreach ($this->callbacks as $path => $params)
  29              $config = $this->mapArray($config, \explode('.', $path), $params);
  30          return $config;
  31      }
  32  	protected function buildCallbackArguments(array $params, array $localVars)
  33      {
  34          unset($params['parser']);
  35          $localVars += ['logger' => 1, 'openTags' => 1, 'registeredVars' => 1, 'text' => 1];
  36          $args = [];
  37          foreach ($params as $k => $v)
  38              if (isset($v))
  39                  $args[] = $this->encoder->encode($v);
  40              elseif (isset($localVars[$k]))
  41                  $args[] = $k;
  42              else
  43                  $args[] = 'registeredVars[' . \json_encode($k) . ']';
  44          return \implode(',', $args);
  45      }
  46  	protected function generateFunction(array $config, array $params)
  47      {
  48          if ($config['js'] == 'returnFalse' || $config['js'] == 'returnTrue')
  49              return new Code((string) $config['js']);
  50          $config += ['params' => []];
  51          $src  = $this->getHeader($params);
  52          $src .= 'function(' . \implode(',', \array_keys($params)) . '){';
  53          $src .= 'return ' . $this->parenthesizeCallback($config['js']);
  54          $src .= '(' . $this->buildCallbackArguments($config['params'], $params) . ');}';
  55          return new Code($src);
  56      }
  57  	protected function getHeader(array $params)
  58      {
  59          $header = "/**\n";
  60          foreach ($params as $paramName => $paramType)
  61              $header .= '* @param {' . $paramType . '} ' . $paramName . "\n";
  62          $header .= "*/\n";
  63          return $header;
  64      }
  65  	protected function mapArray(array $array, array $path, array $params)
  66      {
  67          $key  = \array_shift($path);
  68          $keys = ($key === '*') ? \array_keys($array) : [$key];
  69          foreach ($keys as $key)
  70          {
  71              if (!isset($array[$key]))
  72                  continue;
  73              $array[$key] = (empty($path)) ? $this->generateFunction($array[$key], $params) : $this->mapArray($array[$key], $path, $params);
  74          }
  75          return $array;
  76      }
  77  	protected function parenthesizeCallback($callback)
  78      {
  79          return (\preg_match('(^[.\\w]+$)D', $callback)) ? $callback : '(' . $callback  . ')';
  80      }
  81  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1