-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TASK] Make script compatible with PHP 7+ #73
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,70 +20,79 @@ | |
|
||
/** | ||
* Retrieves a congiguration data, and converts it to an array. | ||
* | ||
* @param string $strPath | ||
* | ||
* @param string $strPath | ||
* @return array | ||
*/ | ||
function getConfig($strPath) | ||
{ | ||
$arrRetVal = []; | ||
|
||
|
||
if(empty($strPath)) { | ||
$strPath = __DIR__ . '/config.json'; | ||
} | ||
|
||
if (is_file($strPath)) { | ||
$strExtension = pathinfo($strPath, PATHINFO_EXTENSION); | ||
|
||
switch ($strExtension) { | ||
case 'json': | ||
$strContents = file_get_contents($strPath); | ||
$config = json_decode($strContents, true); | ||
$strError = ''; | ||
|
||
switch (json_last_error()) { | ||
case JSON_ERROR_NONE: | ||
// No code should be put here. | ||
break; | ||
|
||
case JSON_ERROR_DEPTH: | ||
$strError = 'Maximum stack depth exceeded'; | ||
break; | ||
|
||
case JSON_ERROR_STATE_MISMATCH: | ||
$strError = 'Underflow or the modes mismatch'; | ||
break; | ||
|
||
case JSON_ERROR_CTRL_CHAR: | ||
$strError = 'Unexpected control character found'; | ||
break; | ||
|
||
case JSON_ERROR_SYNTAX: | ||
$strError = 'Syntax error, malformed JSON'; | ||
break; | ||
|
||
case JSON_ERROR_UTF8: | ||
$strError = 'Malformed UTF-8 characters, possibly incorrectly encoded'; | ||
break; | ||
|
||
default: | ||
$strError = 'Unknown error'; | ||
break; | ||
} | ||
|
||
$arrRetVal = is_null($config) || !empty($strError) ? [] : $config; | ||
break; | ||
|
||
case 'xml': | ||
$config = simplexml_load_file($strPath); | ||
$arrRetVal = empty($config) ? [] : get_object_vars($config); | ||
break; | ||
} | ||
} | ||
|
||
|
||
switch ($strExtension) { | ||
|
||
|
||
case 'xml': | ||
$config = simplexml_load_file($strPath); | ||
$arrRetVal = empty($config) ? [] : get_object_vars($config); | ||
break; | ||
case 'json': | ||
default: | ||
$strContents = file_get_contents($strPath); | ||
$config = json_decode($strContents, true); | ||
$strError = ''; | ||
|
||
switch (json_last_error()) { | ||
case JSON_ERROR_NONE: | ||
// No code should be put here. | ||
break; | ||
|
||
case JSON_ERROR_DEPTH: | ||
$strError = 'Maximum stack depth exceeded'; | ||
break; | ||
|
||
case JSON_ERROR_STATE_MISMATCH: | ||
$strError = 'Underflow or the modes mismatch'; | ||
break; | ||
|
||
case JSON_ERROR_CTRL_CHAR: | ||
$strError = 'Unexpected control character found'; | ||
break; | ||
|
||
case JSON_ERROR_SYNTAX: | ||
$strError = 'Syntax error, malformed JSON'; | ||
break; | ||
|
||
case JSON_ERROR_UTF8: | ||
$strError = 'Malformed UTF-8 characters, possibly incorrectly encoded'; | ||
break; | ||
|
||
default: | ||
$strError = 'Unknown error'; | ||
break; | ||
} | ||
|
||
$arrRetVal = is_null($config) || !empty($strError) ? [] : $config; | ||
break; | ||
} | ||
|
||
|
||
return $arrRetVal; | ||
} | ||
|
||
ini_set('display_errors', 'on'); | ||
|
||
// Verify the extensions are loaded before running. | ||
if (!extension_loaded('pgsql')) { | ||
echo "Postgresql not enabled: you need the 'pgsql' module.\n"; | ||
exit(1); | ||
//exit(1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
} | ||
if (!extension_loaded('pdo_mysql')) { | ||
echo "Postgresql not enabled: you need the 'pdo_mysql' module.\n"; | ||
|
@@ -99,24 +108,24 @@ function getConfig($strPath) | |
} | ||
if (ini_get('register_argc_argv') == 0) { | ||
echo "register_argc_argv is not turned on, we can't process command line arguments.\n"; | ||
exit(1); | ||
//exit(1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to read command line arguments without There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I use for my CLI scripts
|
||
} | ||
|
||
$strParam = isset($argv[1]) && !empty($argv[1]) ? $argv[1] : $_SERVER['argv'][1]; | ||
$strParam = isset($argv[1]) && !empty($argv[1]) ? $argv[1] : $_SERVER['argv'][1]; | ||
$arrConfig = getConfig($strParam); | ||
unset($strParam); | ||
|
||
if (empty($arrConfig)) { | ||
echo PHP_EOL, '-- Cannot perform a migration due to missing "config[.xml | .json]" file.', PHP_EOL; | ||
} else { | ||
spl_autoload_register(function($class) { | ||
spl_autoload_register(function ($class) { | ||
require_once 'migration/FromMySqlToPostgreSql/' . $class . '.php'; | ||
}); | ||
|
||
$arrConfig['temp_dir_path'] = __DIR__ . '/temporary_directory'; | ||
$arrConfig['logs_dir_path'] = __DIR__ . '/logs_directory'; | ||
$migration = new \FromMySqlToPostgreSql($arrConfig); | ||
|
||
$migration = new FromMySqlToPostgreSql($arrConfig); | ||
$migration->migrate(); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, but imho, it should be moved to line 106, just before calling the
getConfig($strPath)
function.
This is because
getConfig($strPath)
expects$strPath
to be notempty
.