[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2022 The s9e authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Configurator\JavaScript;
   9  
  10  use ReflectionClass;
  11  use s9e\TextFormatter\Configurator\Collections\PluginCollection;
  12  
  13  class HintGenerator
  14  {
  15      /**
  16      * @var array Config on which hints are based
  17      */
  18      protected $config;
  19  
  20      /**
  21      * @var array Generated hints
  22      */
  23      protected $hints;
  24  
  25      /**
  26      * @var PluginCollection Configured plugins
  27      */
  28      protected $plugins;
  29  
  30      /**
  31      * @var string XSL stylesheet on which hints are based
  32      */
  33      protected $xsl;
  34  
  35      /**
  36      * Generate a HINT object that contains informations about the configuration
  37      *
  38      * @return string JavaScript Code
  39      */
  40  	public function getHints()
  41      {
  42          $this->hints = [];
  43          $this->setPluginsHints();
  44          $this->setRenderingHints();
  45          $this->setRulesHints();
  46          $this->setTagsHints();
  47  
  48          // Build the source. Note that Closure Compiler seems to require that each of HINT's
  49          // properties be declared as a const
  50          $js = "/** @const */ var HINT={};\n";
  51          ksort($this->hints);
  52          foreach ($this->hints as $hintName => $hintValue)
  53          {
  54              $js .= '/** @const */ HINT.' . $hintName . '=' . json_encode($hintValue) . ";\n";
  55          }
  56  
  57          return $js;
  58      }
  59  
  60      /**
  61      * Set the config on which hints are based
  62      *
  63      * @param  array $config
  64      * @return void
  65      */
  66  	public function setConfig(array $config)
  67      {
  68          $this->config = $config;
  69      }
  70  
  71      /**
  72      * Set the collection of plugins
  73      *
  74      * @param  PluginCollection $plugins
  75      * @return void
  76      */
  77  	public function setPlugins(PluginCollection $plugins)
  78      {
  79          $this->plugins = $plugins;
  80      }
  81  
  82      /**
  83      * Set the XSL on which hints are based
  84      *
  85      * @param  string $xsl
  86      * @return void
  87      */
  88  	public function setXSL($xsl)
  89      {
  90          $this->xsl = $xsl;
  91      }
  92  
  93      /**
  94      * Set custom hints from plugins
  95      *
  96      * @return void
  97      */
  98  	protected function setPluginsHints()
  99      {
 100          foreach ($this->plugins as $plugin)
 101          {
 102              $this->hints += $plugin->getJSHints();
 103          }
 104  
 105          $this->hints['regexp']      = 0;
 106          $this->hints['regexpLimit'] = 0;
 107          foreach ($this->config['plugins'] as $pluginConfig)
 108          {
 109              $this->hints['regexp']      |= isset($pluginConfig['regexp']);
 110              $this->hints['regexpLimit'] |= isset($pluginConfig['regexpLimit']);
 111          }
 112      }
 113  
 114      /**
 115      * Set hints related to rendering
 116      *
 117      * @return void
 118      */
 119  	protected function setRenderingHints()
 120      {
 121          // Test for post-processing in templates. Theorically allows for false positives and
 122          // false negatives, but not in any realistic setting
 123          $hints = [
 124              'hash'        => 'data-s9e-livepreview-hash',
 125              'ignoreAttrs' => 'data-s9e-livepreview-ignore-attrs',
 126              'onRender'    => 'data-s9e-livepreview-onrender',
 127              'onUpdate'    => 'data-s9e-livepreview-onupdate'
 128          ];
 129          foreach ($hints as $hintName => $match)
 130          {
 131              $this->hints[$hintName] = (int) (strpos($this->xsl, $match) !== false);
 132          }
 133      }
 134  
 135      /**
 136      * Set hints related to rules
 137      *
 138      * @return void
 139      */
 140  	protected function setRulesHints()
 141      {
 142          $this->hints['closeAncestor']   = 0;
 143          $this->hints['closeParent']     = 0;
 144          $this->hints['createChild']     = 0;
 145          $this->hints['fosterParent']    = 0;
 146          $this->hints['requireAncestor'] = 0;
 147  
 148          $flags = 0;
 149          foreach ($this->config['tags'] as $tagConfig)
 150          {
 151              // Test which rules are in use
 152              foreach (array_intersect_key($tagConfig['rules'], $this->hints) as $k => $v)
 153              {
 154                  $this->hints[$k] = 1;
 155              }
 156              $flags |= $tagConfig['rules']['flags'];
 157          }
 158          $flags |= $this->config['rootContext']['flags'];
 159  
 160          // Iterate over Parser::RULE_* constants and test which flags are set
 161          $parser = new ReflectionClass('s9e\\TextFormatter\\Parser');
 162          foreach ($parser->getConstants() as $constName => $constValue)
 163          {
 164              if (substr($constName, 0, 5) === 'RULE_')
 165              {
 166                  // This will set HINT.RULE_AUTO_CLOSE and others
 167                  $this->hints[$constName] = ($flags & $constValue) ? 1 : 0;
 168              }
 169          }
 170      }
 171  
 172      /**
 173      * Set hints based on given tag's attributes config
 174      *
 175      * @param  array $tagConfig
 176      * @return void
 177      */
 178  	protected function setTagAttributesHints(array $tagConfig)
 179      {
 180          if (empty($tagConfig['attributes']))
 181          {
 182              return;
 183          }
 184  
 185          foreach ($tagConfig['attributes'] as $attrConfig)
 186          {
 187              $this->hints['attributeDefaultValue'] |= isset($attrConfig['defaultValue']);
 188          }
 189      }
 190  
 191      /**
 192      * Set hints related to tags config
 193      *
 194      * @return void
 195      */
 196  	protected function setTagsHints()
 197      {
 198          $this->hints['attributeDefaultValue'] = 0;
 199          $this->hints['namespaces']            = 0;
 200          foreach ($this->config['tags'] as $tagName => $tagConfig)
 201          {
 202              $this->hints['namespaces'] |= (strpos($tagName, ':') !== false);
 203              $this->setTagAttributesHints($tagConfig);
 204          }
 205      }
 206  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1