[ Index ] |
PHP Cross Reference of phpBB-3.3.14-deutsch |
[Summary view] [Print] [Text view]
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\RendererGenerators\PHP; 9 10 abstract class AbstractOptimizer 11 { 12 /** 13 * @var integer Number of tokens 14 */ 15 protected $cnt; 16 17 /** 18 * @var integer Current token index 19 */ 20 protected $i; 21 22 /** 23 * @var boolean Whether the tokens have been changed 24 */ 25 protected $changed; 26 27 /** 28 * @var array Tokens from current source 29 */ 30 protected $tokens; 31 32 /** 33 * Optimize the control structures of a script 34 * 35 * Removes brackets in control structures wherever possible. Prevents the generation of EXT_STMT 36 * opcodes where they're not strictly required. 37 * 38 * @param string $php Original code 39 * @return string Optimized code 40 */ 41 public function optimize($php) 42 { 43 $this->reset($php); 44 $this->optimizeTokens(); 45 46 // Rebuild the source if it has changed 47 if ($this->changed) 48 { 49 $php = $this->serialize(); 50 } 51 52 // Free the memory taken up by the tokens 53 unset($this->tokens); 54 55 return $php; 56 } 57 58 /** 59 * Optimize the stored tokens 60 * 61 * @return void 62 */ 63 abstract protected function optimizeTokens(); 64 65 /** 66 * Reset the internal state of this optimizer 67 * 68 * @param string $php PHP source 69 * @return void 70 */ 71 protected function reset($php) 72 { 73 $this->tokens = token_get_all('<?php ' . $php); 74 $this->i = 0; 75 $this->cnt = count($this->tokens); 76 $this->changed = false; 77 } 78 79 /** 80 * Serialize the tokens back to source 81 * 82 * @return string 83 */ 84 protected function serialize() 85 { 86 // Remove the first token, which should be T_OPEN_TAG, aka "<?php" 87 unset($this->tokens[0]); 88 89 $php = ''; 90 foreach ($this->tokens as $token) 91 { 92 $php .= (is_string($token)) ? $token : $token[1]; 93 } 94 95 return $php; 96 } 97 98 /** 99 * Move the internal cursor until it reaches given string 100 * 101 * @param string $str String to reach 102 * @return void 103 */ 104 protected function skipToString($str) 105 { 106 while (++$this->i < $this->cnt && $this->tokens[$this->i] !== $str); 107 } 108 109 /** 110 * Skip all whitespace 111 * 112 * @return void 113 */ 114 protected function skipWhitespace() 115 { 116 while (++$this->i < $this->cnt && $this->tokens[$this->i][0] === T_WHITESPACE); 117 } 118 119 /** 120 * Remove one tab of indentation off a range of PHP tokens 121 * 122 * @param integer $start Index of the first token to unindent 123 * @param integer $end Index of the last token to unindent 124 * @return void 125 */ 126 protected function unindentBlock($start, $end) 127 { 128 $this->i = $start; 129 do 130 { 131 if ($this->tokens[$this->i][0] === T_WHITESPACE || $this->tokens[$this->i][0] === T_DOC_COMMENT) 132 { 133 $this->tokens[$this->i][1] = preg_replace("/^\t/m", '', $this->tokens[$this->i][1]); 134 } 135 } 136 while (++$this->i <= $end); 137 } 138 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Mon Nov 25 19:05:08 2024 | Cross-referenced by PHPXref 0.7.1 |