Merge pull request #5 from dmamontov/master

add custom getopt & orders builder fix
This commit is contained in:
Alex Lushpai 2015-11-26 12:41:53 +03:00
commit 4556d3e85b
4 changed files with 112 additions and 2 deletions

View File

@ -10,11 +10,20 @@ 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) {
$opt = new OptHelper($shortopts);
$options = $opt->get();
}
if (isset($options['e'])) {
$command = new Command($options);
$command->run();
} elseif (!$options) {
CommandHelper::notWorkGetOptNotice();
} else {
CommandHelper::runHelp();
}

View File

@ -43,8 +43,8 @@ class OrdersBuilder extends Builder
$query = $this->rule->getSQL('orders_uid');
$handler = $this->rule->getHandler('OrdersHandler');
$this->sql = $this->container->db->prepare($query);
$this->sql->bindParam(':orderIds', $uids);
$uids = DataHelper::explodeUids($uidString);
$this->sql->bindParam(':orderIds', $uids);
return $this->build($handler);
}

View File

@ -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";

View File

@ -0,0 +1,96 @@
<?php
class OptHelper
{
private $shortopts = array();
private $longopts = array();
public function __construct($shortopts = '', $longopts = array())
{
if (!empty($shortopts)) {
$this->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;
}
}