|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Класс веб-приложения. |
| 4 | + * @package evas-php\evas-web |
| 5 | + * @author Egor Vasyakin <[email protected]> |
| 6 | + */ |
| 7 | +namespace Evas\Web; |
| 8 | + |
| 9 | +use \InvalidArgumentException; |
| 10 | +use Evas\Base\App as BaseApp; |
| 11 | +use Evas\Base\Help\PhpHelp; |
| 12 | +use Evas\Base\Loader; |
| 13 | +use Evas\Http\Uri; |
| 14 | +use Evas\Http\Interfaces\RequestInterface; |
| 15 | +use Evas\Http\Interfaces\ResponseInterface; |
| 16 | +use Evas\Web\WebRequest; |
| 17 | +use Evas\Web\WebResponse; |
| 18 | + |
| 19 | +class WebApp extends BaseApp |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Установка Uri приложения. |
| 23 | + * @param Uri|string uri приложения |
| 24 | + * @return static |
| 25 | + * @throws InvalidArgumentException |
| 26 | + */ |
| 27 | + public static function setUri($uri): WebApp |
| 28 | + { |
| 29 | + if (is_string($uri)) { |
| 30 | + $uri = new Uri($uri); |
| 31 | + } else if (!($uri instanceof Uri)) { |
| 32 | + throw new InvalidArgumentException(sprintf( |
| 33 | + 'The type of the application uri must be a string or an instance of the %s, |
| 34 | + %s given', |
| 35 | + Uri::class, |
| 36 | + PhpHelp::getType($uri) |
| 37 | + )); |
| 38 | + } |
| 39 | + if (empty($uri->getHost())) { |
| 40 | + $uri->withHost($_SERVER['SERVER_NAME']); |
| 41 | + } |
| 42 | + if (empty($uri->getPort())) { |
| 43 | + $uri->withPort($_SERVER['SERVER_PORT']); |
| 44 | + } |
| 45 | + if (empty($uri->getScheme())) { |
| 46 | + $uri->withScheme(empty($_SERVER['HTTPS']) ? 'http' : 'https'); |
| 47 | + } |
| 48 | + return static::set('uri', $uri); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Вычисление пути uri приложения относительно директории запуска и document_root. |
| 53 | + * @param string|null директория запуска |
| 54 | + * @param string|null директория корня сервера |
| 55 | + * @return string |
| 56 | + */ |
| 57 | + protected static function calcPath(string $runDir = null, string $documentRoot = null): string |
| 58 | + { |
| 59 | + if (!$runDir) $runDir = Loader::getRunDir(); |
| 60 | + if (!$documentRoot) $documentRoot = $_SERVER['DOCUMENT_ROOT']; |
| 61 | + $runDir = str_replace('\\', '/', $runDir); |
| 62 | + return substr(str_replace($documentRoot, '', $runDir), 0, -1); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Получение uri приложения. |
| 67 | + * @return Uri |
| 68 | + */ |
| 69 | + public static function uri(): Uri |
| 70 | + { |
| 71 | + if (!static::has('uri')) { |
| 72 | + static::setUri(static::calcPath()); |
| 73 | + } |
| 74 | + return static::get('uri'); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Получение объекта запроса приложения. |
| 79 | + * @return RequestInterface |
| 80 | + */ |
| 81 | + public static function request(): RequestInterface |
| 82 | + { |
| 83 | + if (!static::has('request')) { |
| 84 | + $instance = static::instance(); |
| 85 | + static::set('request', new WebRequest($instance)); |
| 86 | + } |
| 87 | + return static::get('request'); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Установка запроса веб-приложения. |
| 92 | + * @param RequestInterface |
| 93 | + * @return self |
| 94 | + */ |
| 95 | + public static function setRequest(RequestInterface &$request) |
| 96 | + { |
| 97 | + return static::set('request', $request); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Получение объекта ответа приложения. |
| 102 | + * @return ResponseInterface |
| 103 | + */ |
| 104 | + public static function response(): ResponseInterface |
| 105 | + { |
| 106 | + if (!static::has('response')) { |
| 107 | + static::set('response', new WebResponse); |
| 108 | + } |
| 109 | + return static::get('response'); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Редирект относительно приложения. |
| 114 | + * @param string адрес редиректа |
| 115 | + */ |
| 116 | + public static function redirectByApp(string $to) |
| 117 | + { |
| 118 | + $to = static::uri() . $to; |
| 119 | + return static::response()->redirect($to); |
| 120 | + } |
| 121 | +} |
0 commit comments