@@ -47,106 +47,6 @@ public static function containsVariable($string)
47
47
return 1 === preg_match ('/\$\{.+\}/ ' , $ string );
48
48
} // containsVariable()
49
49
50
- /* ------------------------------------------------------------------------------------------
51
- * Perform variable/macro substitution on a string using a variable map. The map keys
52
- * are the names of the variables WITHOUT the ${} wrapper (e.g., a key of 'SCHEMA'
53
- * matches the variable ${SCHEMA}) while the values are the destination strings.
54
- * Optionally throw an exception if there are un-matched variables left in the string.
55
- *
56
- * NOTE: Map keys with a value of NULL are ignored.
57
- *
58
- * @param $string Target string containing variables
59
- * @param $map Associative array containing the variable mappings. The keys are the
60
- * names of the variables WITHOUT the ${} wrapper.
61
- * @param $logger An optional class that extends Loggable. If this is present then an
62
- * exception will be thrown if there are any unsubstituted variables present in the
63
- * string.
64
- * @param $exceptionPrefix An optional string to use in the exception message
65
- * @param $substitutionDetails An optional array that, if present, will be populated
66
- * with the macros that were substituted and those that were not substituted.
67
- *
68
- * 'substituted' => An array of variables that were substituted
69
- * 'unsubstituted' => An array of variables that were present in the string but not
70
- * substituted
71
- *
72
- * @return The string with variables substituted.
73
- *
74
- * @throws An exception of $logger is not NULL and there are unsubstituted macros
75
- * found in the string
76
- * ------------------------------------------------------------------------------------------
77
- */
78
-
79
- public static function substituteVariables (
80
- $ string ,
81
- array $ map ,
82
- Loggable $ logger = null ,
83
- $ exceptionPrefix = null ,
84
- array $ substitutionDetails = null
85
- ) {
86
-
87
- if ( null === $ string ) {
88
- return $ string ;
89
- }
90
-
91
- $ exceptionForUnusedVariables = ( null !== $ logger );
92
- $ trackDetails = ( null !== $ substitutionDetails );
93
-
94
- // If we are not tracking the variables that have or have not been substituted, simply
95
- // perform a string replacement.
96
-
97
- if ( ! $ exceptionForUnusedVariables && ! $ trackDetails ) {
98
- foreach ( $ map as $ k => $ v ) {
99
- if ( null === $ v ) {
100
- continue ;
101
- }
102
- $ string = str_replace ('${ ' . $ k . '} ' , $ v , $ string );
103
- }
104
- } else {
105
-
106
- $ substitutionDetails = array (
107
- 'unsubstituted ' => array (),
108
- 'substituted ' => array ()
109
- );
110
-
111
- // Perform the substitution and track variables that have been substituted
112
-
113
- array_map (
114
- function ($ v , $ k ) use (&$ string , &$ substitutionDetails ) {
115
- if ( null === $ v ) {
116
- return ;
117
- }
118
- $ search = '${ ' . $ k . '} ' ;
119
- if ( false !== strpos ($ string , $ search ) ) {
120
- $ substitutionDetails ['substituted ' ][] = $ k ;
121
- }
122
- $ string = str_replace ($ search , $ v , $ string );
123
- },
124
- $ map ,
125
- array_keys ($ map )
126
- );
127
-
128
- // If there are any variables left in the string, track them as unsubstituted.
129
-
130
- $ matches = array ();
131
- if ( 0 !== preg_match_all ('/(\$\{.+\})/ ' , $ string , $ matches ) ) {
132
- $ substitutionDetails ['unsubstituted ' ] = array_shift ($ matches );
133
- }
134
-
135
- $ substitutionDetails ['unsubstituted ' ] = array_unique ($ substitutionDetails ['unsubstituted ' ]);
136
-
137
- if ( $ exceptionForUnusedVariables && 0 != count ($ substitutionDetails ['unsubstituted ' ]) ) {
138
- $ logger ->logAndThrowException (
139
- ( null !== $ exceptionPrefix ? $ exceptionPrefix . ": " : "Undefined macros found: " )
140
- . implode (", " , $ substitutionDetails ['unsubstituted ' ])
141
- );
142
- }
143
-
144
- } // else ( null === $substitutedVariables && null == $unsubstitutedVariables )
145
-
146
- return $ string ;
147
-
148
- } // substituteVariables()
149
-
150
50
/* ------------------------------------------------------------------------------------------
151
51
* Process a macro file. A macro is simply a text fragment containing markers that will be
152
52
* replaced with values. Markers are identified using bash variable syntax (e.g., ${macro}) and
@@ -248,11 +148,17 @@ public static function processMacro($string, stdClass $config)
248
148
249
149
// Replace macro arguments
250
150
151
+ $ vs = new VariableStore ();
152
+
251
153
if ( isset ($ config ->args ) && count ($ config ->args ) > 0 ) {
252
- $ macro = self ::substituteVariables ($ macro , (array ) $ config ->args );
154
+ $ vs ->add ((array ) $ config ->args );
155
+ $ macro = $ vs ->substitute ($ macro );
156
+
253
157
}
254
158
255
- $ string = self ::substituteVariables ($ string , array ( $ config ->name => $ macro ));
159
+ $ vs ->clear ();
160
+ $ vs ->set ( array ( $ config ->name => $ macro ) );
161
+ $ string = $ vs ->substitute ($ string );
256
162
257
163
return $ string ;
258
164
0 commit comments