Source of file Request.php
Size: 9,675 Bytes - Last Modified: 2017-11-08T13:54:24+00:00
/home/travis/build/bluzphp/framework/src/Proxy/Request.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 | <?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ declare(strict_types=1); namespace Bluz\Proxy; use Bluz\Common\Exception\ComponentException; use Bluz\Http\RequestMethod; use Bluz\Request\RequestFactory; use Psr\Http\Message\UriInterface; use Zend\Diactoros\ServerRequest as Instance; /** * Proxy to Request * * Example of usage * <code> * use Bluz\Proxy\Request; * * Request::getParam('foo'); * </code> * * @package Bluz\Proxy * @author Anton Shevchuk * * @todo Proxy class should be clean * * @method static Instance getInstance() * * @method static UriInterface getUri() * @see \Zend\Diactoros\RequestTrait::getUri() */ final class Request { use ProxyTrait; /** * @const string HTTP content types */ const TYPE_ANY = '*/*'; const TYPE_HTML = 'text/html'; const TYPE_JSON = 'application/json'; /** * @var array|null Accepted type */ static private $accept; /** * Init instance * * @throws ComponentException */ private static function initInstance() { throw new ComponentException('Class `Proxy\\Request` required external initialization'); } /** * Retrieve a member of the $_GET super global * * If no $key is passed, returns the entire $_GET array. * * @param string $key * @param string $default Default value to use if key not found * * @return string Returns null if key does not exist */ public static function getQuery($key = null, $default = null) { return RequestFactory::get($key, self::getInstance()->getQueryParams(), $default); } /** * Retrieve a member of the $_POST super global * * If no $key is passed, returns the entire $_POST array. * * @param string $key * @param string $default Default value to use if key not found * * @return string Returns null if key does not exist */ public static function getPost($key = null, $default = null) { return RequestFactory::get($key, (array)self::getInstance()->getParsedBody(), $default); } /** * Retrieve a member of the $_SERVER super global * * If no $key is passed, returns the entire $_SERVER array. * * @param string $key * @param string $default Default value to use if key not found * * @return string Returns null if key does not exist */ public static function getServer($key = null, $default = null) { return RequestFactory::get($key, self::getInstance()->getServerParams(), $default); } /** * Retrieve a member of the $_COOKIE super global * * If no $key is passed, returns the entire $_COOKIE array. * * @param string $key * @param string $default Default value to use if key not found * * @return string Returns null if key does not exist */ public static function getCookie($key = null, $default = null) { return RequestFactory::get($key, self::getInstance()->getCookieParams(), $default); } /** * Retrieve a member of the $_ENV super global * * If no $key is passed, returns the entire $_ENV array. * * @param string $key * @param string $default Default value to use if key not found * * @return string Returns null if key does not exist */ public static function getEnv($key = null, $default = null) { return RequestFactory::get($key, $_ENV, $default); } /** * Search for a header value * * @param string $header * @param mixed $default * * @return string */ public static function getHeader($header, $default = null) { return RequestFactory::getHeader($header, self::getInstance()->getHeaders(), $default); } /** * Access values contained in the superglobals as public members * Order of precedence: 1. GET, 2. POST * * @param string $key * @param null $default * * @return string|null * @link http://msdn.microsoft.com/en-us/library/system.web.httprequest.item.aspx */ public static function getParam($key, $default = null) { return self::getQuery($key) ?? self::getPost($key) ?? $default; } /** * Get all params from GET and POST or PUT * * @return array */ public static function getParams() { $body = (array)self::getInstance()->getParsedBody(); $query = (array)self::getInstance()->getQueryParams(); return array_merge($body, $query); } /** * Get uploaded file * * @param string $name * * @return \Zend\Diactoros\UploadedFile */ public static function getFile($name) { return RequestFactory::get($name, self::getInstance()->getUploadedFiles()); } /** * Get the client's IP address * * @param bool $checkProxy * * @return string */ public static function getClientIp($checkProxy = true) { $result = null; if ($checkProxy) { $result = self::getServer('HTTP_CLIENT_IP') ?? self::getServer('HTTP_X_FORWARDED_FOR') ?? null; } return $result ?? self::getServer('REMOTE_ADDR'); } /** * Get module * * @return string */ public static function getModule(): string { return self::getParam('_module', Router::getDefaultModule()); } /** * Get controller * * @return string */ public static function getController(): string { return self::getParam('_controller', Router::getDefaultController()); } /** * Get method * * @return string */ public static function getMethod(): string { return self::getParam('_method', self::getInstance()->getMethod()); } /** * Get Accept MIME Type * * @return array */ public static function getAccept(): array { if (!self::$accept) { // save to static variable self::$accept = []; // get header from request $header = self::getHeader('accept'); // nothing ... if (!$header) { return self::$accept; } // make array if types $header = explode(',', $header); $header = array_map('trim', $header); foreach ($header as $a) { // the default quality is 1. $q = 1; // check if there is a different quality if (strpos($a, ';q=') or strpos($a, '; q=')) { // divide "mime/type;q=X" into two parts: "mime/type" i "X" list($a, $q) = preg_split('/;([ ]?)q=/', $a); } // remove other extension if (strpos($a, ';')) { $a = substr($a, 0, strpos($a, ';')); } // mime-type $a is accepted with the quality $q // WARNING: $q == 0 means, that mime-type isn’t supported! self::$accept[$a] = (float)$q; } arsort(self::$accept); } return self::$accept; } /** * Reset accept for tests * * @return void */ public static function resetAccept() { self::$accept = null; } /** * Check Accept header * * @param array $allowTypes * * @return string|false */ public static function checkAccept(array $allowTypes = []) { $accept = self::getAccept(); // if no parameter was passed, just return first mime type from parsed data if (empty($allowTypes)) { return current(array_keys($accept)); } $allowTypes = array_map('strtolower', $allowTypes); // let’s check our supported types: foreach ($accept as $mime => $quality) { if ($quality && in_array($mime, $allowTypes)) { return $mime; } } // no mime-type found return false; } /** * Check CLI * * @return bool */ public static function isCli(): bool { return (PHP_SAPI === 'cli'); } /** * Check HTTP * * @return bool */ public static function isHttp(): bool { return (PHP_SAPI !== 'cli'); } /** * Is this a GET method request? * * @return bool */ public static function isGet(): bool { return (self::getInstance()->getMethod() === RequestMethod::GET); } /** * Is this a POST method request? * * @return bool */ public static function isPost(): bool { return (self::getInstance()->getMethod() === RequestMethod::POST); } /** * Is this a PUT method request? * * @return bool */ public static function isPut(): bool { return (self::getInstance()->getMethod() === RequestMethod::PUT); } /** * Is this a DELETE method request? * * @return bool */ public static function isDelete(): bool { return (self::getInstance()->getMethod() === RequestMethod::DELETE); } /** * Is the request a Javascript XMLHttpRequest? * * @return bool */ public static function isXmlHttpRequest(): bool { return (self::getHeader('X-Requested-With') === 'XMLHttpRequest'); } } |