Skip to content

Commit

Permalink
Increased use of Environment (class) in Request and Url.
Browse files Browse the repository at this point in the history
  • Loading branch information
vasiliishvakin committed Apr 16, 2017
1 parent 8838d17 commit f8421bc
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 71 deletions.
73 changes: 72 additions & 1 deletion src/Environment.php
Expand Up @@ -12,6 +12,11 @@ class Environment

protected $isEqualServerEnv;

protected $requestMethod;

protected $requestUri;

protected $queryString;

public function getSrvScheme()
{
Expand All @@ -37,7 +42,7 @@ public function isSrvHttps()
public function isHttps()
{
if (null === $this->https) {
$this->https = $this->isSrvHttps();
$this->https = $this->isSrvHttps();
}
return $this->https;
}
Expand Down Expand Up @@ -101,6 +106,7 @@ public function setPort($port)

/**
* @return mixed
* @deprecated
*/
public function isSrvEnv()
{
Expand All @@ -109,4 +115,69 @@ public function isSrvEnv()
}
return $this->isEqualServerEnv;
}

/**
* @return mixed
*/
public function getRequestMethod()
{
if (null === $this->requestMethod) {
if (isset($_SERVER["REQUEST_METHOD"])) {
$this->requestMethod = $_SERVER["REQUEST_METHOD"];
}
}
return $this->requestMethod;
}

/**
* @param mixed $requestMethod
*/
public function setRequestMethod($requestMethod)
{
$this->requestMethod = $requestMethod;
}

/**
* @return mixed
*/
public function getRequestUri()
{
if (null ===$this->requestUri) {
if (isset($_SERVER["REQUEST_URI"])) {
$this->requestUri = $_SERVER["REQUEST_URI"];
}
}
return $this->requestUri;
}

/**
* @param mixed $requestUri
*/
public function setRequestUri($requestUri)
{
$this->requestUri = $requestUri;
}

/**
* @return mixed
*/
public function getQueryString()
{
if (null === $this->queryString) {
if (isset($_SERVER["QUERY_STRING"])) {
$this->queryString = $_SERVER["QUERY_STRING"];
}
}
return $this->queryString;
}

/**
* @param mixed $queryString
*/
public function setQueryString($queryString)
{
$this->queryString = $queryString;
}


}
19 changes: 19 additions & 0 deletions src/EnvironmentIncludeInterface.php
@@ -0,0 +1,19 @@
<?php


namespace Akademiano\HttpWarp;


interface EnvironmentIncludeInterface
{
/**
* @return Environment
*/
public function getEnvironment();

/**
* @param Environment $environment
*/
public function setEnvironment(Environment $environment);

}
Expand Up @@ -2,9 +2,10 @@

namespace Akademiano\HttpWarp\Parts;

use Akademiano\HttpWarp\EnvironmentIncludeInterface;
use Akademiano\HttpWarp\Request;

trait GetRequest
trait RequestIncludeTrait
{
/**
* @var Request
Expand All @@ -14,7 +15,7 @@ trait GetRequest
/**
* @param Request $request
*/
public function setRequest($request)
public function setRequest(Request $request)
{
$this->request = $request;
}
Expand All @@ -26,6 +27,9 @@ public function getRequest()
{
if (is_null($this->request)) {
$this->request = new Request();
if ($this instanceof EnvironmentIncludeInterface) {
$this->request->setEnvironment($this->getEnvironment());
}
}
return $this->request;
}
Expand Down
157 changes: 96 additions & 61 deletions src/Request.php
Expand Up @@ -5,17 +5,62 @@
use Akademiano\Utils\CatchError;
use Akademiano\HttpWarp\File\UploadFile;

class Request
class Request implements EnvironmentIncludeInterface
{
const METHOD_GET = "GET";
const METHOD_POST = "POST";
const METHOD_PUT = "PUT";
const METHOD_HEAD = "HEAD";
const METHOD_DELETE = "DELETE";

protected $method;
protected $params;
protected $url;
protected $uriNormal;

/** @var Environment */
protected $environment;

/** @var array */
protected $rawFiles;

/** @var File\UploadFile[] */
protected $files = [];

/**
* Request constructor.
* @param Environment $environment
*/
public function __construct(Environment $environment = null)
{
if (null !== $environment) {
$this->setEnvironment($environment);
}
}

/**
* @return Environment
*/
public function getEnvironment()
{
if (null === $this->environment) {
$this->environment = new Environment();
}
return $this->environment;
}

/**
* @param Environment $environment
*/
public function setEnvironment(Environment $environment)
{
$this->environment = $environment;
}

public function getMethod()
{
if (is_null($this->method)) {
$this->method = $_SERVER['REQUEST_METHOD'];
$this->method = $this->getEnvironment()->getRequestMethod();
}
return $this->method;
}
Expand All @@ -24,15 +69,15 @@ public function getParams($emptyStringNull = true)
{
if (is_null($this->params)) {
switch ($this->getMethod()) {
case 'GET':
case 'HEAD' :
case self::METHOD_GET:
case self::METHOD_HEAD :
$this->params = $_GET;
break;
case 'POST':
case self::METHOD_POST:
$this->params = $_POST;
break;
case 'PUT':
case 'DELETE':
case self::METHOD_PUT:
case self::METHOD_DELETE:
parse_str(file_get_contents('php://input'), $this->params);
break;
default:
Expand All @@ -59,12 +104,12 @@ public function getParam($key, $default = null)

public function isGet()
{
return $this->getMethod() === 'GET';
return $this->getMethod() === self::METHOD_GET;
}

public function isPost()
{
return $this->getMethod() === 'POST';
return $this->getMethod() === self::METHOD_POST;
}

public function hasParam($name)
Expand Down Expand Up @@ -97,20 +142,19 @@ public function setUrl($url)
public function getUrl()
{
if (is_null($this->url)) {
$url = new Url();
$url->setScheme($this->isHttps() ? "https" : "http");
$url->setDomain($_SERVER["HTTP_HOST"] ?: $_SERVER["SERVER_NAME"]);
$url->setPort($_SERVER["SERVER_PORT"]);
$url->setPath($_SERVER["REQUEST_URI"]);
if (isset($_SERVER["QUERY_STRING"])) {
$url->setQuery($_SERVER["QUERY_STRING"]);
$url = new Url(null, $this->getEnvironment());
$url->setScheme($this->getEnvironment()->getScheme());
$url->setDomain($this->getEnvironment()->getServerName());
$url->setPort($this->getEnvironment()->getPort());
$url->setPath($this->getEnvironment()->getRequestUri());
if (null !== $this->getEnvironment()->getQueryString()) {
$url->setQuery($this->getEnvironment()->getQueryString());
}
$this->url = $url;
}
return $this->url;
}


/**
* @param string $name
* @param null $type
Expand All @@ -120,56 +164,47 @@ public function getUrl()
*/
public function getFiles($name = "files", $type = null, $maxSize = null, $withErrors = false)
{
$files = [];
if (!isset($_FILES) || empty($_FILES) || !isset($_FILES[$name])) {
return $files;
}
$inFiles = $_FILES[$name];
$countFiles = count($inFiles["name"]);
if ($countFiles == 1) {
$inFiles = [$inFiles];
} elseif ($countFiles > 1) {
$newInFiles = [];
foreach ($inFiles as $key => $info) {
for ($i = 0; $i < $countFiles; $i++) {
$newInFiles[$i][$key] = $info[$i];
if (!isset($this->files[$name])) {
$this->files[$name] = [];
if (isset($_FILES) && !empty($_FILES) && isset($_FILES[$name])) {
$inFiles = $_FILES[$name];
$countFiles = count($inFiles["name"]);
if ($countFiles == 1) {
$inFiles = [$inFiles];
} elseif ($countFiles > 1) {
$newInFiles = [];
foreach ($inFiles as $key => $info) {
for ($i = 0; $i < $countFiles; $i++) {
$newInFiles[$i][$key] = $info[$i];
}
}
$inFiles = $newInFiles;
}
}
$inFiles = $newInFiles;
}

foreach ($inFiles as $fileData) {
foreach ($fileData as $key => $value) {
if (is_array($value)) {
if (count($value) > 1) {
throw new \LogicException("To many values in file param");
foreach ($inFiles as $fileData) {
foreach ($fileData as $key => $value) {
if (is_array($value)) {
if (count($value) > 1) {
throw new \LogicException("To many values in file param");
}
$fileData[$key] = reset($value);
}
}
if (!$withErrors && $fileData["error"] !== 0) {
continue;
}
$file = new UploadFile($fileData["name"], $fileData["tmp_name"], $fileData["error"]);
if ($type && !$file->checkType($type)) {
continue;
}
if ($maxSize && $file->getSize() > $maxSize) {
continue;
}
$fileData[$key] = reset($value);
$this->files[$name][] = $file;
}
}
if (!$withErrors && $fileData["error"] !== 0) {
continue;
}
$file = new UploadFile($fileData["name"], $fileData["tmp_name"], $fileData["error"]);
if ($type && !$file->checkType($type)) {
continue;
}
if ($maxSize && $file->getSize() > $maxSize) {
continue;
}
$files[] = $file;
}
return $files;
}

public function isHttps()
{
return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']==="on");
}

public function getProtocol()
{
return $this->isHttps() ? "https" : "http";
return $this->files[$name];
}

public function isCheckModified()
Expand Down

0 comments on commit f8421bc

Please sign in to comment.