mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-11 09:48:40 +00:00
Uri and request fixes
Making uri a request parameter. Uri can be more easily initialized by passing uri as constructor parameter
This commit is contained in:
parent
f501f1d793
commit
e7f2e1d71c
|
|
@ -16,12 +16,10 @@
|
|||
namespace phpOMS\Message\Http;
|
||||
|
||||
use phpOMS\Localization\Localization;
|
||||
|
||||
|
||||
use phpOMS\Message\RequestAbstract;
|
||||
use phpOMS\Message\RequestMethod;
|
||||
use phpOMS\Uri\Http;
|
||||
use phpOMS\Uri\UriFactory;
|
||||
use phpOMS\Uri\UriInterface;
|
||||
|
||||
/**
|
||||
* Request class.
|
||||
|
|
@ -80,16 +78,15 @@ class Request extends RequestAbstract
|
|||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $rootPath relative installation path
|
||||
* @param UriInterface $uri Uri
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function __construct(string $rootPath)
|
||||
public function __construct(UriInterface $uri)
|
||||
{
|
||||
$this->uri = new Http($rootPath);
|
||||
$this->uri = $uri;
|
||||
$this->l11n = new Localization();
|
||||
UriFactory::setQuery('/root', $rootPath);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -97,7 +94,7 @@ class Request extends RequestAbstract
|
|||
*
|
||||
* This is used in order to either initialize the current http request or a batch of GET requests
|
||||
*
|
||||
* @param string $uri URL
|
||||
* @param mixed $uri URL
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
|
|
@ -106,7 +103,7 @@ class Request extends RequestAbstract
|
|||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function init(string $uri = null)
|
||||
public function init($uri = null)
|
||||
{
|
||||
if ($uri === null) {
|
||||
$this->data = $_GET ?? [];
|
||||
|
|
@ -128,7 +125,7 @@ class Request extends RequestAbstract
|
|||
|
||||
$this->uri->set(Http::getCurrent());
|
||||
} else {
|
||||
$this->setMethod($uri['type']); // TODO: is this correct?
|
||||
$this->setMethod($uri['type']);
|
||||
$this->uri->set($uri['uri']);
|
||||
}
|
||||
|
||||
|
|
@ -177,21 +174,6 @@ class Request extends RequestAbstract
|
|||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set request type.
|
||||
*
|
||||
* @param RequestMethod $type Request type
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function setMethod(RequestMethod $type)
|
||||
{
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate request hash.
|
||||
*
|
||||
|
|
@ -338,11 +320,11 @@ class Request extends RequestAbstract
|
|||
*/
|
||||
public function getMethod() : string
|
||||
{
|
||||
if (!isset($this->type)) {
|
||||
$this->type = $_SERVER['REQUEST_METHOD'];
|
||||
if (!isset($this->method)) {
|
||||
$this->method = $_SERVER['REQUEST_METHOD'];
|
||||
}
|
||||
|
||||
return $this->type;
|
||||
return $this->method;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -15,9 +15,7 @@
|
|||
*/
|
||||
namespace phpOMS\Message\Http;
|
||||
|
||||
use phpOMS\Datatypes\Exception\InvalidEnumValue;
|
||||
use phpOMS\Message\RequestMethod;
|
||||
use phpOMS\Uri\InvalidUriException;
|
||||
|
||||
/**
|
||||
* Rest request class.
|
||||
|
|
@ -35,49 +33,21 @@ class Rest
|
|||
/**
|
||||
* Url.
|
||||
*
|
||||
* @var string
|
||||
* @var Request
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private $url = '';
|
||||
|
||||
/**
|
||||
* Method.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private $method = RequestMethod::POST;
|
||||
private $request = '';
|
||||
|
||||
/**
|
||||
* Set url.
|
||||
*
|
||||
* @param string $url Url
|
||||
* @param Request $request Request
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn
|
||||
*/
|
||||
public function setUrl(string $url) {
|
||||
if (filter_var($url, FILTER_VALIDATE_URL) === false) {
|
||||
throw new InvalidUriException('$url');
|
||||
}
|
||||
|
||||
$this->url = $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set method.
|
||||
*
|
||||
* @param string $method Method
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn
|
||||
*/
|
||||
public function setMethod(string $method) {
|
||||
if(!RequestMethod::isValidValue($method)) {
|
||||
throw new InvalidEnumValue($method);
|
||||
}
|
||||
|
||||
$this->method = $method;
|
||||
public function setRequest(Request $request) {
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -94,7 +64,7 @@ class Rest
|
|||
{
|
||||
$curl = curl_init();
|
||||
|
||||
switch ($this->method) {
|
||||
switch ($this->request->getMethod()) {
|
||||
case RequestMethod::POST:
|
||||
curl_setopt($curl, CURLOPT_POST, 1);
|
||||
|
||||
|
|
@ -105,16 +75,12 @@ class Rest
|
|||
case RequestMethod::PUT:
|
||||
curl_setopt($curl, CURLOPT_PUT, 1);
|
||||
break;
|
||||
default:
|
||||
if ($data) {
|
||||
$this->url = sprintf("%s?%s", $this->url, http_build_query($data));
|
||||
}
|
||||
}
|
||||
|
||||
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
|
||||
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
|
||||
|
||||
curl_setopt($curl, CURLOPT_URL, $this->url);
|
||||
curl_setopt($curl, CURLOPT_URL, $this->request->getUri()->__toString());
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
||||
|
||||
$result = curl_exec($curl);
|
||||
|
|
|
|||
|
|
@ -44,12 +44,20 @@ abstract class RequestAbstract implements RequestInterface
|
|||
protected $uri = null;
|
||||
|
||||
/**
|
||||
* Request type.
|
||||
* Request method.
|
||||
*
|
||||
* @var \phpOMS\Message\RequestMethod
|
||||
* @var string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected $type = null;
|
||||
protected $method = null;
|
||||
|
||||
/**
|
||||
* Root.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected $rootPath = null;
|
||||
|
||||
/**
|
||||
* Request data.
|
||||
|
|
@ -125,6 +133,14 @@ abstract class RequestAbstract implements RequestInterface
|
|||
return $this->uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setUri(UriInterface $uri)
|
||||
{
|
||||
return $this->uri = $uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
@ -158,6 +174,13 @@ abstract class RequestAbstract implements RequestInterface
|
|||
*/
|
||||
abstract public function getMethod() : string;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setMethod(string $method) {
|
||||
$this->method = $method;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -51,6 +51,16 @@ interface RequestInterface extends MessageInterface
|
|||
*/
|
||||
public function getMethod() : string;
|
||||
|
||||
/**
|
||||
* Set request method.
|
||||
*
|
||||
* @param string $method
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function setMethod(string $method);
|
||||
|
||||
/**
|
||||
* Get request uri.
|
||||
*
|
||||
|
|
@ -61,10 +71,20 @@ interface RequestInterface extends MessageInterface
|
|||
*/
|
||||
public function getUri() : UriInterface;
|
||||
|
||||
/**
|
||||
* Set request uri.
|
||||
*
|
||||
* @param UriInterface $uri
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function setUri(UriInterface $uri);
|
||||
|
||||
/**
|
||||
* Get request hash.
|
||||
*
|
||||
* @return string
|
||||
* @return array
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
|
|
|
|||
28
Uri/Http.php
28
Uri/Http.php
|
|
@ -124,14 +124,22 @@ class Http implements UriInterface
|
|||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $rootPath Root path for subdirectory
|
||||
* @param string $uri Root path for subdirectory
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function __construct(string $rootPath)
|
||||
public function __construct(string $uri)
|
||||
{
|
||||
$this->rootPath = $rootPath;
|
||||
$this->set($uri);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setRootPath(string $root)
|
||||
{
|
||||
return $this->rootPath = $root;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -310,13 +318,6 @@ class Http implements UriInterface
|
|||
$this->base = $this->scheme . '://' . $this->host . $this->rootPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function parse($uri)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
@ -325,13 +326,6 @@ class Http implements UriInterface
|
|||
return $this->uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function resolve($base)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -41,6 +41,16 @@ interface UriInterface
|
|||
*/
|
||||
public static function isValid(string $uri) : bool;
|
||||
|
||||
/**
|
||||
* Set uri
|
||||
*
|
||||
* @param string $uri Uri string
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function set(string $uri);
|
||||
|
||||
/**
|
||||
* Get scheme.
|
||||
*
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user