PHPDaemon\Config\Object::parseCfgUri PHP Méthode

parseCfgUri() public static méthode

Checks if property exists
public static parseCfgUri ( $uri, $source = null ) : boolean
Résultat boolean Exists?
    public static function parseCfgUri($uri, $source = null)
    {
        if (mb_orig_strpos($uri, '://') === false) {
            if (strncmp($uri, 'unix:', 5) === 0) {
                $e = explode(':', $uri);
                if (sizeof($e) === 4) {
                    $uri = 'unix://' . $e[1] . ':' . $e[2] . '@localhost' . $e[3];
                } elseif (sizeof($e) === 3) {
                    $uri = 'unix://' . $e[1] . '@localhost' . $e[2];
                } else {
                    $uri = 'unix://localhost' . $e[1];
                }
            } else {
                $uri = 'tcp://' . $uri;
            }
        }
        if (stripos($uri, 'unix:///') === 0) {
            $uri = 'unix://localhost/' . substr($uri, 8);
        }
        $zeroPortNum = false;
        $uri = preg_replace_callback('~:0(?:$|/)~', function () use(&$zeroPortNum) {
            $zeroPortNum = true;
            return '';
        }, $uri);
        $u = parse_url($uri);
        $u['host'] = trim($u['host'], '][');
        $u['uri'] = $uri;
        if ($zeroPortNum) {
            $u['port'] = 0;
        }
        if (!isset($u['scheme'])) {
            $u['scheme'] = '';
        }
        $u['params'] = [];
        if (!isset($u['fragment'])) {
            return $u;
        }
        $hash = '#' . $u['fragment'];
        $error = false;
        preg_replace_callback('~(#+)(.+?)(?=#|$)|(.+)~', function ($m) use(&$u, &$error, $uri) {
            if ($error) {
                return;
            }
            list(, $type, $value) = $m;
            if ($type === '#') {
                // standard value
                $e = explode('=', $value, 2);
                if (sizeof($e) === 2) {
                    list($key, $value) = $e;
                } else {
                    $key = $value;
                    $value = true;
                }
                $u['params'][$key] = $value;
            } elseif ($type === '##') {
                // Context name
                $u['params']['ctxname'] = $value;
            } else {
                Daemon::log('Malformed URI: ' . var_export($uri, true) . ', unexpected token \'' . $type . '\'');
                $error = true;
            }
        }, $hash);
        return $error ? false : $u;
    }

Usage Example

Exemple #1
0
 /**
  * Bind given socket
  * @param  string  $uri Address to bind
  * @return boolean      Success
  */
 public function bindSocket($uri)
 {
     $u = \PHPDaemon\Config\Object::parseCfgUri($uri);
     $scheme = $u['scheme'];
     if ($scheme === 'unix') {
         $socket = new \PHPDaemon\BoundSocket\UNIX($u);
     } elseif ($scheme === 'udp') {
         $socket = new \PHPDaemon\BoundSocket\UDP($u);
         if (isset($this->config->port->value)) {
             $socket->setDefaultPort($this->config->port->value);
         }
     } elseif ($scheme === 'tcp') {
         $socket = new \PHPDaemon\BoundSocket\TCP($u);
         if (isset($this->config->port->value)) {
             $socket->setDefaultPort($this->config->port->value);
         }
     } else {
         Daemon::log(get_class($this) . ': enable to bind \'' . $uri . '\': scheme \'' . $scheme . '\' is not supported');
         return false;
     }
     $socket->attachTo($this);
     if ($socket->bindSocket()) {
         if ($this->enabled) {
             $socket->enable();
         }
         return true;
     }
     return false;
 }
All Usage Examples Of PHPDaemon\Config\Object::parseCfgUri