From 87d972b1422dac3c4e7925c4795ed73a2230927e Mon Sep 17 00:00:00 2001 From: Dmitry Mamontov Date: Tue, 24 Nov 2015 04:37:32 -0500 Subject: [PATCH] refactoring custom getopt && add lonoptions --- retailcrm/app.php | 7 +- retailcrm/src/Helpers/DataHelper.php | 45 ------------- retailcrm/src/Helpers/OptHelper.php | 96 ++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 47 deletions(-) create mode 100644 retailcrm/src/Helpers/OptHelper.php diff --git a/retailcrm/app.php b/retailcrm/app.php index 63ac7aa..5bcc4c9 100644 --- a/retailcrm/app.php +++ b/retailcrm/app.php @@ -10,10 +10,13 @@ if ( require_once 'bootstrap.php'; -$options = getopt('dluce:m:p:r:h:'); +$shortopts = 'dluce:m:p:r:h:'; + +$options = getopt($shortopts); if (!$options || $options == -1) { - $options = DataHelper::getOpt(); + $opt = new OptHelper($shortopts); + $options = $opt->get(); } if (isset($options['e'])) { diff --git a/retailcrm/src/Helpers/DataHelper.php b/retailcrm/src/Helpers/DataHelper.php index 9932aea..352a5df 100644 --- a/retailcrm/src/Helpers/DataHelper.php +++ b/retailcrm/src/Helpers/DataHelper.php @@ -78,49 +78,4 @@ class DataHelper return $uids; } - - public static function getOpt() - { - if (!array_key_exists('argv', $_SERVER)) { - return false; - } - - $result = array(); - - $params = $_SERVER['argv']; - - foreach ($params as $key => $param) { - if ($param{0} == '-') { - $name = substr($param, 1); - $value = true; - - if ($name{0} == '-') { - $name = substr($name, 1); - if (strpos($param, '=') !== false) { - $long = explode('=', substr($param, 2), 2); - $name = $long[0]; - $value = $long[1]; - unset($long); - } - } - - if ( - isset($params[$key + 1]) && - $value === true && - $params[$key + 1] !== false && - $params[$key + 1]{0} != '-' - ) { - $value = $params[$key + 1]; - } - - $result[$name] = $value; - } else { - $result[] = $param; - } - } - - unset($params); - - return empty($result) ? false : $result; - } } diff --git a/retailcrm/src/Helpers/OptHelper.php b/retailcrm/src/Helpers/OptHelper.php new file mode 100644 index 0000000..74a7ead --- /dev/null +++ b/retailcrm/src/Helpers/OptHelper.php @@ -0,0 +1,96 @@ +shortopts = preg_split( + '@([a-z0-9][:]{0,2})@i', + $shortopts, 0, + PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY + ); + } + + if (!empty($longopts)) { + $this->longopts = $longopts; + } + } + + public function get() + { + if (!array_key_exists('argv', $_SERVER)) { + return false; + } + + $result = array(); + + $params = $_SERVER['argv']; + + foreach ($params as $key => $param) { + if ($param{0} == '-') { + $name = substr($param, 1); + $value = true; + + if ($name{0} == '-') { + $name = substr($name, 1); + if (strpos($param, '=') !== false) { + $long = explode('=', substr($param, 2), 2); + $name = $long[0]; + $value = $long[1]; + unset($value); + } + } + + if ( + isset($params[$key + 1]) && + $value === true && + $params[$key + 1] !== false && + $params[$key + 1]{0} != '-' + ) { + $value = $params[$key + 1]; + } + + $result[$name] = $value; + } else { + $result[] = $param; + } + } + + unset($params); + + return empty($result) ? false : $this->filter($result); + } + + private function filter($params) + { + $result = array(); + + $opts = array_merge($this->shortopts, $this->longopts); + + foreach ($opts as $opt) { + if (substr($opt, -2) === '::') { + $key = substr($opt, 0, -2); + + if (isset($params[$key]) && !empty($params[$key])) { + $result[$key] = $params[$key]; + } elseif (isset($params[$key])) { + $result[$key] = true; + } + } elseif (substr($opt, -1) === ':') { + $key = substr($opt, 0, -1); + + if (isset($params[$key]) && !empty($params[$key])) { + $result[$key] = $params[$key]; + } + } elseif (ctype_alnum($opt) && isset($params[$opt])) { + $result[$opt] = true; + } + } + + return $result; + } +}