[TYPO3-core] RFC: feature #2743

Wolfgang Klinger wolfgang at stufenlos.net
Mon Mar 6 09:52:00 CET 2006


 *hiya!*

 On Thu, 02 Mar 2006, Wolfgang Klinger wrote the following:
>  Type: patch / feature
> 
>  Speed up substituteConstants with preg_replace_callback
>  BT reference: http://bugs.typo3.org/view.php?id=2743

 I found a bug in my test code that falsified the results for the method
 in tsparser_ext, now this one is corrected and the results are much
 better, so I attached another patch
 (this is NOT the same method as before, so please approve both
 patches!)


 kind regards
 Wolfgang

-------------- next part --------------
--- TYPO3core/t3lib/class.t3lib_tsparser_ext.php	2006-02-26 11:14:27.000000000 +0100
+++ TYPO3core_testing/t3lib/class.t3lib_tsparser_ext.php	2006-03-06 09:43:38.000000000 +0100
@@ -219,28 +219,42 @@
 	 */
 	function substituteConstants($all)	{
 		$this->Cmarker=substr(md5(uniqid("")),0,6);
-		reset($this->flatSetup);
-		while (list($const,$val)=each($this->flatSetup))	{
-			if (!is_array($val))	{
-				switch($this->constantMode)	{
-					case "const":
-						$all = str_replace('{$'.$const.'}','##'.$this->Cmarker.'_B##{$'.$const.'}##'.$this->Cmarker.'_E##',$all);
-					break;
-					case "subst":
-						$all = str_replace('{$'.$const.'}','##'.$this->Cmarker.'_B##'.$val.'##'.$this->Cmarker.'_E##',$all);
-					break;
-					case "untouched":
-					break;
-					default:
-						$all = str_replace('{$'.$const.'}',$val,$all);
-					break;
-				}
-			}
-		}
-		return $all;
+
+        return preg_replace_callback('/\{\$(.[^}]+)\}/', array($this, 'substituteConstantsCallBack'), $all);
 	}
 
 	/**
+	 * Call back method for preg_replace_callback in substituteConstants
+	 *
+	 * @param       array           Regular expression matches
+	 * @return      string          Replacement
+	 * @see substituteConstants()
+	 */
+    function substituteConstantsCallBack($matches) {
+        switch($this->constantMode) {
+            case 'const':
+                $ret_val =  isset($this->flatSetup[$matches[1]]) && !is_array($this->flatSetup[$matches[1]]) ?
+                            '##'.$this->Cmarker.'_B##'.$matches[0].'##'.$this->Cmarker.'_E##' :
+                            $matches[0];
+                break;
+            case 'subst':
+                $ret_val =  isset($this->flatSetup[$matches[1]]) && !is_array($this->flatSetup[$matches[1]]) ?
+                            '##'.$this->Cmarker.'_B##'.$this->flatSetup[$matches[1]].'##'.$this->Cmarker.'_E##' :
+                            $matches[0];
+                break;
+            case 'untouched':
+                $ret_val = $matches[0];
+                break;
+            default:
+                $ret_val = 	isset($this->flatSetup[$matches[1]]) && !is_array($this->flatSetup[$matches[1]]) ? 
+							$this->flatSetup[$matches[1]] : 
+							$matches[0];
+        }
+
+        return $ret_val;
+    }
+
+	/**
 	 * [Describe function...]
 	 *
 	 * @param	[type]		$all: ...
@@ -1710,4 +1724,4 @@
 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_tsparser_ext.php'])	{
 	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_tsparser_ext.php']);
 }
-?>
\ No newline at end of file
+?>


More information about the TYPO3-team-core mailing list