/** * Creates a new URI instance from the global variables. * * @return \Elegant\Http\Message\Uri */ public static final function createFromGlobals() { $scheme = empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === 'off' ? 'http' : 'https'; $host = ''; $port = null; if (isset($_SERVER['HTTP_HOST'])) { if (Str::contains($_SERVER['HTTP_HOST'], ':')) { list($host, $port) = explode(':', $_SERVER['HTTP_HOST'], 2); } else { $host = $_SERVER['HTTP_HOST']; } } else { if (isset($_SERVER['SERVER_NAME'])) { $host = $_SERVER['SERVER_NAME']; } if (isset($_SERVER['SERVER_PORT'])) { $port = $_SERVER['SERVER_PORT']; } } if (isset($_SERVER['SCRIPT_NAME'])) { $basePath .= dirname($_SERVER['SCRIPT_NAME']); } else { $basePath = '/'; } $path = ''; if (isset($_SERVER['PATH_INFO'])) { $path = $_SERVER['PATH_INFO']; } elseif (isset($_SERVER['REQUEST_URI'])) { if ($p = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)) { if (Str::icontains($p, $basePath)) { $path = mb_substr($p, mb_strlen($basePath)); } else { $path = $p; } } } $query = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : ''; $user = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : ''; $password = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : null; $uri = $this->create(); $uri = $uri->withScheme($scheme); $uri = $uri->withHost($host); $uri = $uri->withPort($port); $uri = $uri->withBasePath($basePath); $uri = $uri->withPath($path); $uri = $uri->withQuery($query); $uri = $uri->withUserInfo($user, $password); return $uri; }
/** * Calls the callable. * * @param callable|string $callable * @param array $params * * @return mixed */ public final function call($callable, array $params = []) { if (!is_callable($callable) and is_string($callable) and Str::contains($callable, '@')) { list($class, $method) = explode('@', $callable, 2); $callable = [$this->create($class), $method]; } return call_user_func_array($callable, $params); }
/** * Parses the path for geting namespace. * * @param string $path * * @return array */ private function parsePath($path) { return Str::contains($path, '::') ? explode('::', $path, 2) : [null, $path]; }
/** * Dispatches the route. * * @param \Psr\Http\Message\ServerRequestInterface $request * @param \Elegant\Http\Route\Route $route * * @return \Psr\Http\Message\ResponseInterface */ private function dispatchRoute(ServerRequestInterface $request, $route) { $callback = $route->getCallback(); if (is_string($callback) and Str::contains($callback, '@')) { list($class, $method) = explode('@', $controller, 2); $object = $this->getContainer()->create($class); if ($object instanceof Controller) { return $this->processController($request, $object, $method); } $callback = [$object, $method]; } return $this->dispatch($request, $callback); }