From b079105b4f9f95df04fd55e226913abd435271f4 Mon Sep 17 00:00:00 2001 From: Dmitry Mamontov Date: Mon, 23 Nov 2015 09:31:55 -0500 Subject: [PATCH] add custom getopt --- retailcrm/app.php | 6 +++++ retailcrm/src/Helpers/CommandHelper.php | 5 ++++ retailcrm/src/Helpers/DataHelper.php | 33 +++++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/retailcrm/app.php b/retailcrm/app.php index 7583def..63ac7aa 100644 --- a/retailcrm/app.php +++ b/retailcrm/app.php @@ -12,9 +12,15 @@ require_once 'bootstrap.php'; $options = getopt('dluce:m:p:r:h:'); +if (!$options || $options == -1) { + $options = DataHelper::getOpt(); +} + if (isset($options['e'])) { $command = new Command($options); $command->run(); +} elseif (!$options) { + CommandHelper::notWorkGetOptNotice(); } else { CommandHelper::runHelp(); } diff --git a/retailcrm/src/Helpers/CommandHelper.php b/retailcrm/src/Helpers/CommandHelper.php index 5d9604c..cf98de8 100644 --- a/retailcrm/src/Helpers/CommandHelper.php +++ b/retailcrm/src/Helpers/CommandHelper.php @@ -30,6 +30,11 @@ class CommandHelper echo "\033[0;31mUnfortunately for the database can not be used to make the dump\033[0m\n"; } + public static function notWorkGetOptNotice() + { + echo "\033[0;31mDoes not function getopt. It is used to obtain the parameters from the command line. Please refer to the server administrator.\033[0m\n"; + } + public static function updateNotice() { echo "\033[0;31mFull update is not allowed, please select one of the following flags: limit, set of identifiers or a specific id\033[0m\n"; diff --git a/retailcrm/src/Helpers/DataHelper.php b/retailcrm/src/Helpers/DataHelper.php index 352a5df..bdcfe24 100644 --- a/retailcrm/src/Helpers/DataHelper.php +++ b/retailcrm/src/Helpers/DataHelper.php @@ -78,4 +78,37 @@ class DataHelper return $uids; } + + public static function getOpt() + { + if (!array_key_exists('argv', $_SERVER)) { + return false; + } + + $result = array(); + $params = $_SERVER['argv']; + + while (list(, $p) = each($params)) { + if ($p{0} == '-') { + $pname = substr($p, 1); + $value = true; + if ($pname{0} == '-') { + $pname = substr($pname, 1); + if (strpos($p, '=') !== false) { + list($pname, $value) = explode('=', substr($p, 2), 2); + } + } + + $nextparm = current($params); + if ($value === true && $nextparm !== false && $nextparm{0} != '-') { + list(, $value) = each($params); + } + $result[$pname] = $value; + } else { + $result[] = $p; + } + } + + return empty($result) ? false : $result; + } }