Skip to content

Commit

Permalink
Merge pull request #816 from smgallo/remove-deprecated-code
Browse files Browse the repository at this point in the history
Remove deprecated code
  • Loading branch information
smgallo authored Feb 23, 2019
2 parents 9f48a19 + 060d8fa commit e56b246
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 115 deletions.
15 changes: 6 additions & 9 deletions classes/ETL/Ingestor/RestIngestor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use ETL\DbModel\Query;
use ETL\aOptions;
use ETL\iAction;
use ETL\Utilities;
use ETL\VariableStore;

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

$substitutionDetails = array();
$queryString = Utilities::substituteVariables(
$this->restRequestConfig->format,
$this->restParameters,
null,
null,
$substitutionDetails
);
$vs = new VariableStore($this->restParameters);
$queryString = $vs->substitute($this->restRequestConfig->format, null, $substitutionDetails);

if ( false !== strpos($queryString, '${^REMAINING}') ) {
$used = array_combine($substitutionDetails['substituted'], $substitutionDetails['substituted']);
Expand All @@ -668,7 +663,9 @@ function ($v, $k) {
array_keys($remaining)
)
);
$queryString = Utilities::substituteVariables($queryString, array('^REMAINING' => $parameters));
$vs->clear();
$vs->add('^REMAINING', $parameters);
$queryString = $vs->substitute($queryString);
}
} else {
// Use standard query string format
Expand Down
5 changes: 3 additions & 2 deletions classes/ETL/Maintenance/VerifyDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use ETL\DataEndpoint\iRdbmsEndpoint;
use ETL\DbModel\Query;
use PDOException;
use ETL\Utilities;
use ETL\VariableStore;
use Log;

use PHPSQLParser\PHPSQLParser;
Expand Down Expand Up @@ -263,7 +263,8 @@ public function execute(EtlOverseerOptions $etlOverseerOptions)
$this->logger->info(count($result) . " matches found");
if ( 0 != count($result) ) {
foreach ( $result as $row ) {
$line = Utilities::substituteVariables($lineTemplate, $row);
$vs = new VariableStore($row, $this->logger);
$line = $vs->substitute($lineTemplate);
$this->logger->warning($line);
$lines[] = $line;
}
Expand Down
110 changes: 8 additions & 102 deletions classes/ETL/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,106 +47,6 @@ public static function containsVariable($string)
return 1 === preg_match('/\$\{.+\}/', $string);
} // containsVariable()

/* ------------------------------------------------------------------------------------------
* Perform variable/macro substitution on a string using a variable map. The map keys
* are the names of the variables WITHOUT the ${} wrapper (e.g., a key of 'SCHEMA'
* matches the variable ${SCHEMA}) while the values are the destination strings.
* Optionally throw an exception if there are un-matched variables left in the string.
*
* NOTE: Map keys with a value of NULL are ignored.
*
* @param $string Target string containing variables
* @param $map Associative array containing the variable mappings. The keys are the
* names of the variables WITHOUT the ${} wrapper.
* @param $logger An optional class that extends Loggable. If this is present then an
* exception will be thrown if there are any unsubstituted variables present in the
* string.
* @param $exceptionPrefix An optional string to use in the exception message
* @param $substitutionDetails An optional array that, if present, will be populated
* with the macros that were substituted and those that were not substituted.
*
* 'substituted' => An array of variables that were substituted
* 'unsubstituted' => An array of variables that were present in the string but not
* substituted
*
* @return The string with variables substituted.
*
* @throws An exception of $logger is not NULL and there are unsubstituted macros
* found in the string
* ------------------------------------------------------------------------------------------
*/

public static function substituteVariables(
$string,
array $map,
Loggable $logger = null,
$exceptionPrefix = null,
array $substitutionDetails = null
) {

if ( null === $string ) {
return $string;
}

$exceptionForUnusedVariables = ( null !== $logger );
$trackDetails = ( null !== $substitutionDetails );

// If we are not tracking the variables that have or have not been substituted, simply
// perform a string replacement.

if ( ! $exceptionForUnusedVariables && ! $trackDetails ) {
foreach ( $map as $k => $v ) {
if ( null === $v ) {
continue;
}
$string = str_replace('${' . $k . '}', $v, $string);
}
} else {

$substitutionDetails = array(
'unsubstituted' => array(),
'substituted' => array()
);

// Perform the substitution and track variables that have been substituted

array_map(
function ($v, $k) use (&$string, &$substitutionDetails) {
if ( null === $v ) {
return;
}
$search = '${' . $k . '}';
if ( false !== strpos($string, $search) ) {
$substitutionDetails['substituted'][] = $k;
}
$string = str_replace($search, $v, $string);
},
$map,
array_keys($map)
);

// If there are any variables left in the string, track them as unsubstituted.

$matches = array();
if ( 0 !== preg_match_all('/(\$\{.+\})/', $string, $matches ) ) {
$substitutionDetails['unsubstituted'] = array_shift($matches);
}

$substitutionDetails['unsubstituted'] = array_unique($substitutionDetails['unsubstituted']);

if ( $exceptionForUnusedVariables && 0 != count($substitutionDetails['unsubstituted']) ) {
$logger->logAndThrowException(
( null !== $exceptionPrefix ? $exceptionPrefix . ": " : "Undefined macros found: " )
. implode(", ", $substitutionDetails['unsubstituted'])
);
}

} // else ( null === $substitutedVariables && null == $unsubstitutedVariables )

return $string;

} // substituteVariables()

/* ------------------------------------------------------------------------------------------
* Process a macro file. A macro is simply a text fragment containing markers that will be
* replaced with values. Markers are identified using bash variable syntax (e.g., ${macro}) and
Expand Down Expand Up @@ -248,11 +148,17 @@ public static function processMacro($string, stdClass $config)

// Replace macro arguments

$vs = new VariableStore();

if ( isset($config->args) && count($config->args) > 0 ) {
$macro = self::substituteVariables($macro, (array) $config->args);
$vs->add((array) $config->args);
$macro = $vs->substitute($macro);

}

$string = self::substituteVariables($string, array( $config->name => $macro ));
$vs->clear();
$vs->add(array($config->name => $macro));
$string = $vs->substitute($string);

return $string;

Expand Down
15 changes: 13 additions & 2 deletions classes/ETL/VariableStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,22 @@ public function __construct($store = null, Log $logger = null)
}
} // __construct()

/**
* Clear the variables in the store
*/

public function clear()
{
$this->variables = array();
}

/**
* Set a variable in the store. If a variable should be overwritten use overwrite() instead.
* Setting a variable to a NULL value will unset the variable. If the variable is already set,
* do not overwrite and issue a warning. We issue a warning because it may be the case that the
* developer is expecting a value to be updated and want to alter them that it is not.
* do not overwrite and issue a warning. This is done so that variables set early in a process
* (such as ETL) are not blindly changed causing unexpected results. We issue a warning because
* it may be the case that the developer is expecting a value to be updated and want to alert
* them that it is not.
*
* @param string $var The name of the variable to set
* @param scalar $value The value of the variable
Expand Down

0 comments on commit e56b246

Please sign in to comment.