PHPDaemon\Core\Bootstrap::getArgs PHP Method

getArgs() public static method

Parses command-line arguments.
public static getArgs ( array $args ) : array
$args array $_SERVER ['argv']
return array Arguments
    public static function getArgs($args)
    {
        $out = [];
        $last_arg = null;
        for ($i = 1, $il = sizeof($args); $i < $il; ++$i) {
            if (preg_match('~^--(.+)~', $args[$i], $match)) {
                $parts = explode('=', $match[1]);
                $key = preg_replace('~[^a-z0-9]+~', '', $parts[0]);
                if (isset($parts[1])) {
                    $out[$key] = $parts[1];
                } else {
                    $out[$key] = true;
                }
                $last_arg = $key;
            } elseif (preg_match('~^-([a-zA-Z0-9]+)~', $args[$i], $match)) {
                $key = null;
                for ($j = 0, $jl = mb_orig_strlen($match[1]); $j < $jl; ++$j) {
                    $key = $match[1][$j];
                    $out[$key] = true;
                }
                $last_arg = $key;
            } elseif ($last_arg !== null) {
                $out[$last_arg] = $args[$i];
            }
        }
        return $out;
    }

Usage Example

 /**
  * It allows to run your simple web-apps in spawn-fcgi/php-fpm environment.
  * @return boolean|null - Success.
  */
 public static function compatRunEmul()
 {
     Daemon::$compatMode = TRUE;
     Daemon::initSettings();
     $argv = isset($_SERVER['CMD_ARGV']) ? $_SERVER['CMD_ARGV'] : '';
     $args = \PHPDaemon\Core\Bootstrap::getArgs($argv);
     if (isset($args[$k = 'configfile'])) {
         Daemon::$config[$k] = $args[$k];
     }
     if (!Daemon::$config->loadCmdLineArgs($args)) {
         $error = true;
     }
     if (isset(Daemon::$config->configfile->value) && !Daemon::loadConfig(Daemon::$config->configfile->value)) {
         $error = true;
     }
     if (!isset(Daemon::$config->path->value)) {
         exit('\'path\' is not defined');
     }
     if ($error) {
         exit;
     }
     $appResolver = (require Daemon::$config->path->value);
     $appResolver->init();
     $req = new \stdClass();
     $req->attrs = new \stdClass();
     $req->attrs->request = $_REQUEST;
     $req->attrs->get = $_GET;
     $req->attrs->post = $_REQUEST;
     $req->attrs->cookie = $_REQUEST;
     $req->attrs->server = $_SERVER;
     $req->attrs->files = $_FILES;
     $req->attrs->session = isset($_SESSION) ? $_SESSION : null;
     $req->attrs->connId = 1;
     $req->attrs->trole = 'RESPONDER';
     $req->attrs->flags = 0;
     $req->attrs->id = 1;
     $req->attrs->paramsDone = true;
     $req->attrs->inputDone = true;
     $req = $appResolver->getRequest($req);
     while (true) {
         $ret = $req->call();
         if ($ret === 1) {
             return;
         }
     }
 }
All Usage Examples Of PHPDaemon\Core\Bootstrap::getArgs