Skip to content

Commit 3c3fe92

Browse files
committed
Remove deprecated Utilities::substituteVariables()
1 parent 5ab30b5 commit 3c3fe92

File tree

3 files changed

+17
-113
lines changed

3 files changed

+17
-113
lines changed

classes/ETL/Ingestor/RestIngestor.php

+6-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use ETL\DbModel\Query;
2020
use ETL\aOptions;
2121
use ETL\iAction;
22-
use ETL\Utilities;
22+
use ETL\VariableStore;
2323

2424
use Log;
2525
use PDO;
@@ -647,13 +647,8 @@ protected function setRestUrlWithParameters()
647647
// A format was specified. Substitute any existing parameters in the format string.
648648

649649
$substitutionDetails = array();
650-
$queryString = Utilities::substituteVariables(
651-
$this->restRequestConfig->format,
652-
$this->restParameters,
653-
null,
654-
null,
655-
$substitutionDetails
656-
);
650+
$vs = new VariableStore($this->restParameters);
651+
$queryString = $vs->substitute($this->restRequestConfig->format, null, $substitutionDetails);
657652

658653
if ( false !== strpos($queryString, '${^REMAINING}') ) {
659654
$used = array_combine($substitutionDetails['substituted'], $substitutionDetails['substituted']);
@@ -668,7 +663,9 @@ function ($v, $k) {
668663
array_keys($remaining)
669664
)
670665
);
671-
$queryString = Utilities::substituteVariables($queryString, array('^REMAINING' => $parameters));
666+
$vs->clear();
667+
$vs->set('^REMAINING', $parameters);
668+
$queryString = $vs->substitute($queryString);
672669
}
673670
} else {
674671
// Use standard query string format

classes/ETL/Maintenance/VerifyDatabase.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use ETL\DataEndpoint\iRdbmsEndpoint;
1919
use ETL\DbModel\Query;
2020
use PDOException;
21-
use ETL\Utilities;
21+
use ETL\VariableStore;
2222
use Log;
2323

2424
use PHPSQLParser\PHPSQLParser;
@@ -263,7 +263,8 @@ public function execute(EtlOverseerOptions $etlOverseerOptions)
263263
$this->logger->info(count($result) . " matches found");
264264
if ( 0 != count($result) ) {
265265
foreach ( $result as $row ) {
266-
$line = Utilities::substituteVariables($lineTemplate, $row);
266+
$vs = new VariableStore($row, $this->logger);
267+
$line = $vs->substitute($lineTemplate);
267268
$this->logger->warning($line);
268269
$lines[] = $line;
269270
}

classes/ETL/Utilities.php

+8-102
Original file line numberDiff line numberDiff line change
@@ -47,106 +47,6 @@ public static function containsVariable($string)
4747
return 1 === preg_match('/\$\{.+\}/', $string);
4848
} // containsVariable()
4949

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-
15050
/* ------------------------------------------------------------------------------------------
15151
* Process a macro file. A macro is simply a text fragment containing markers that will be
15252
* 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)
248148

249149
// Replace macro arguments
250150

151+
$vs = new VariableStore();
152+
251153
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+
253157
}
254158

255-
$string = self::substituteVariables($string, array( $config->name => $macro ));
159+
$vs->clear();
160+
$vs->set( array( $config->name => $macro ) );
161+
$string = $vs->substitute($string);
256162

257163
return $string;
258164

0 commit comments

Comments
 (0)