-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequest.php
109 lines (97 loc) · 2.7 KB
/
Request.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php declare(strict_types = 1);
namespace noxkiwi\core;
use JetBrains\PhpStorm\Pure;
use noxkiwi\core\Helper\WebHelper;
use noxkiwi\core\Interfaces\RequestInterface;
use noxkiwi\core\Request\CliRequest;
use noxkiwi\core\Request\HttpRequest;
use noxkiwi\core\Traits\DatacontainerTrait;
use noxkiwi\core\Traits\LanguageImprovementTrait;
use function date;
use function json_encode;
use function md5;
use const PHP_SAPI;
/**
* I am the Core's Request representation.
* The Request object is singleton inherited.
* It is used to get information into the Context.
*
* @package noxkiwi\core
* @author Jan Nox <[email protected]>
* @license https://nox.kiwi/license
* @copyright 2016 - 2021 noxkiwi
* @version 1.0.2
* @link https://nox.kiwi/
*/
abstract class Request implements RequestInterface
{
use DatacontainerTrait;
use LanguageImprovementTrait;
public const REQUEST_HTTP = WebHelper::PROTOCOL_HTTPS;
public const REQUEST_CLI = 'cli';
/** @var \noxkiwi\core\Request I am the instance. */
private static Request $inst;
/**
* If the single instance does not exist, create it.
* Return the single instance then.
*
* @return \noxkiwi\core\Request
*/
public static function getInstance(): static
{
if (! isset(static::$inst)) {
$className = static::getRequestType();
static::$inst = new $className();
static::$inst->build();
}
return static::$inst;
}
/**
* Returns the Request type that is used for this Request
*
* @return string
*/
#[Pure] private static function getRequestType(): string
{
if (static::isCli()) {
return CliRequest::class;
}
return HttpRequest::class;
}
/**
* returns true if the current php process is being run from a command line interface.
*
* @return bool
*/
public static function isCli(): bool
{
return PHP_SAPI === 'cli';
}
/**
* @inheritDoc
*/
public function build(): Request
{
return $this;
}
/**
* I will return whether the current request is a POST request.
* @return bool
*/
final public static function isPost(): bool
{
return $_SERVER['REQUEST_METHOD'] === WebHelper::METHOD_POST;
}
/**
* I will return the identifier of this distinct request.
* @return string
*/
final public function getIdentifier(): string
{
if (empty($this->identifier)) {
$id = md5(json_encode($this->get()) . date('Y-m-d H:i:s:u'));
$this->identifier = $id;
}
return $this->identifier;
}
}