/** * 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); }
/** * Gets the request content type params. * * @return array */ public final function getContentTypeParams() { $parts = $this->getContentTypeParts(); $params = []; if ($parts) { $partsCount = count($parts); for ($i = 1; $i < $partsCount; ++$i) { $part = $parts[$i]; if (Str::contain($part, '=')) { $paramParts = explode('=', $part, 2); $params[strtolower($paramParts[0])] = $paramParts[1]; } } } return $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); }
protected function column($column) { if ($column instanceof Aggregate) { return $this->aggregate($column); } if ($column instanceof Raw) { return $this->raw($column); } if ($column instanceof Query) { return $this->query($column); } if (Str::icontains('->', $column)) { return $this->json(explode('->', $column)); } $wrapped = []; $segments = explode('.', $column); if (1 < count($segments)) { $wrapped[] = $this->table(array_unshift($segments)); } foreach ($segments as $key => $segment) { if ($segment === '*') { $wrapped[] = $segment; } else { $wrapped[] = $this->id($segment); } } return implode('.', $wrapped); }
/** * {@inheritdoc} */ public final function __toString() { if (null !== $this->string) { return $this->string; } $string = ''; $scheme = $this->getScheme(); if ('' !== $scheme) { $string .= $scheme . ':'; } $authority = $this->getAuthority(); if ('' !== $authority) { $string .= '//' . $authority; } $path = $this->getPath(); if (!Str::startsWith($path, '/') and '' !== ($basePath = $this->getBasePath())) { $path = $basePath . ('/' === $basePath ? '' : '/') . $path; } if ('' !== $path) { if ($authority and !Str::startsWith($path, '/')) { $path = '/' . $path; } elseif (!$authority and Str::startsWith($path, '//')) { $path = '/' . ltrim($path, '/'); } $string .= $path; } $query = $this->getQuery(); if ('' !== $query) { $string .= '?' . $query; } $fragment = $this->getFragment(); if ('' !== $fragment) { $string .= '#' . $fragment; } $this->string = $string; return $string; }