1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212:
<?php
namespace MvcCore\Ext\Request;
class Cli extends \MvcCore\Request
{
const VERSION = '4.2.0';
public $Console = FALSE;
public function __construct (array & $server, array & $get, array & $post) {
$this->serverGlobals = $server;
$this->getGlobals = $get;
$this->postGlobals = $post;
$this->Console = php_sapi_name() == 'cli';
$this->initScriptName();
$this->initAppRoot();
if ($this->Console) {
$this->initCliParams();
} else {
$this->initMethod();
$this->initBasePath();
$this->initProtocol();
$this->initParsedUrlSegments();
$this->initHttpParams();
$this->initPath();
$this->initReferer();
$this->initUrlCompositions();
}
unset($this->serverGlobals, $this->getGlobals, $this->postGlobals);
}
protected function initScriptName () {
$this->indexScriptName = str_replace('\\', '/', $this->serverGlobals['SCRIPT_NAME']);
if (!$this->Console)
$this->ScriptName = '/' . substr($this->indexScriptName, strrpos($this->indexScriptName, '/') + 1);
}
protected function initAppRoot () {
$indexFilePath = $this->Console
? str_replace('\\', '/', getcwd()) . '/' . $_SERVER['SCRIPT_FILENAME']
: ucfirst(str_replace('\\', '/', $this->serverGlobals['SCRIPT_FILENAME']));
if (strpos(__FILE__, 'phar://') === 0) {
$appRootFullPath = 'phar://' . $indexFilePath;
} else {
$appRootFullPath = substr($indexFilePath, 0, mb_strrpos($indexFilePath, '/'));
}
$this->AppRoot = str_replace(array('\\', '//'), '/', $appRootFullPath);
}
protected function initCliParams () {
$rawArgs = $this->serverGlobals['argv'];
if (isset($rawArgs[0]) && $rawArgs[0] == $this->indexScriptName) array_shift($rawArgs);
$params = array();
$dash = '-';
for ($i = 0, $l = count($rawArgs); $i < $l; $i += 1) {
$rawArg = $rawArgs[$i];
$nextRawArg = isset($rawArgs[$i + 1]) ? $rawArgs[$i + 1] : NULL;
$nextRawArgIsValue = !is_null($nextRawArg) && substr($nextRawArg, 0, 1) != $dash;
$rawArgLength = strlen($rawArg);
if ($rawArgLength > 1) {
$firstChar = substr($rawArg, 0, 1);
$secondChar = substr($rawArg, 1, 1);
if ($firstChar == $dash && $secondChar != $dash) {
$i = $this->initCliParamSingleDash($i, $params, $rawArg, $nextRawArg, $nextRawArgIsValue);
} else if ($firstChar == $dash && $secondChar == $dash) {
$i = $this->initCliParamDoubleDash($i, $params, $rawArg, $nextRawArg, $nextRawArgIsValue);
} else {
$this->initCliParamNoDash($params, $rawArg);
}
}
}
$this->initCliParamsCleanQuotes($params);
$this->Params = $params;
}
protected function initCliParamSingleDash ($i, & $params, & $rawArg, & $nextRawArg, & $nextRawArgIsValue) {
$param = substr($rawArg, 1);
$equalPos = strpos($param, '=');
if ($equalPos !== FALSE) {
$params[substr($param, 0, $equalPos)] = substr($param, $equalPos + 1);
} else {
if (strlen($param) > 1 && !$nextRawArgIsValue) {
for ($j = 0, $k = strlen($param); $j < $k; $j += 1) $params[$param[$j]] = TRUE;
} else {
if ($param == 'c' && $nextRawArgIsValue) {
$params['controller'] = $nextRawArg;
} else if ($param == 'a' && $nextRawArgIsValue) {
$params['action'] = $nextRawArg;
} else {
$params[$param] = $nextRawArg;
}
$i += 1;
}
}
return $i;
}
protected function initCliParamDoubleDash ($i, & $params, & $rawArg, & $nextRawArg, & $nextRawArgIsValue) {
$param = substr($rawArg, 2);
$equalPos = strpos($param, '=');
if ($equalPos !== FALSE) {
$params[substr($param, 0, $equalPos)] = substr($param, $equalPos + 1);
} else if ($nextRawArgIsValue) {
$params[$param] = $nextRawArg;
$i += 1;
} else {
$params[$param] = TRUE;
}
return $i;
}
protected function initCliParamNoDash (& $params, & $rawArg) {
$equalPos = strpos($rawArg, '=');
if ($equalPos !== FALSE) {
$params[substr($rawArg, 0, $equalPos)] = substr($rawArg, $equalPos + 1);
} else {
array_push($params, $rawArg);
}
}
protected function initCliParamsCleanQuotes (& $params) {
$quotChars = array('"', "'", '`');
foreach ($params as & $value) {
foreach ($quotChars as $quotChar) {
if (mb_strpos($value, $quotChar) !== 0) continue;
if (mb_strrpos($value, $quotChar) !== mb_strlen($value) - 1) continue;
$value = trim($value, $quotChar);
}
}
}
}