Skip to content

Commit b2fb9e0

Browse files
UPDATE
1 parent 0df965e commit b2fb9e0

21 files changed

+1092
-221
lines changed

Asset.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Tamedevelopers\Support\Str;
88
use Tamedevelopers\Support\Traits\ServerTrait;
9+
use Tamedevelopers\Support\Process\HttpRequest;
910

1011
class Asset{
1112

@@ -100,7 +101,7 @@ public static function config(?string $base_path = null, ?bool $cache = false, $
100101
// if not defined
101102
if(!defined('ASSET_BASE_DIRECTORY')){
102103
// url helper class
103-
$urlFromhelper = UrlHelper::url();
104+
$urlFromhelper = HttpRequest::url();
104105

105106
// if base path is set
106107
if(!empty($base_path)){
@@ -112,7 +113,7 @@ public static function config(?string $base_path = null, ?bool $cache = false, $
112113
$baseForUrlPath = $base_path;
113114

114115
// check if accessed from default ip:address
115-
if(UrlHelper::isIpAccessedVia127Port()){
116+
if(HttpRequest::isIpAccessedVia127Port()){
116117
$baseForUrlPath = '';
117118
}
118119

@@ -128,7 +129,7 @@ public static function config(?string $base_path = null, ?bool $cache = false, $
128129
self::cleanServerPath($urlFromhelper),
129130
'/'
130131
),
131-
'removeDomain' => UrlHelper::http() . UrlHelper::host()
132+
'removeDomain' => HttpRequest::http() . HttpRequest::host()
132133
]);
133134
}
134135
}

Cookie.php

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tamedevelopers\Support;
66

7+
use Tamedevelopers\Support\Str;
78
use Tamedevelopers\Support\Time;
89

910
class Cookie{
@@ -45,7 +46,7 @@ class Cookie{
4546
*/
4647
static protected function init()
4748
{
48-
self::$name = strtolower(str_replace([' '], '', env('APP_NAME', '')));
49+
self::$name = Str::lower(Str::replace([' '], '', env('APP_NAME', '')));
4950
self::$timeName = "__time_" . self::$name;
5051
self::$expireName = "__expire_" . self::$name;
5152
self::$timeFormat = Time::timestamp('next year', 'Y-m-d');
@@ -91,16 +92,34 @@ static protected function init()
9192
public static function set($name, $value = null, $minutes = 0, $path = null, $domain = null, $secure = null, $httponly = null, $force = null)
9293
{
9394
// minutes
94-
$minutes = self::minutesToExpire($minutes);
95+
$expires = self::minutesToExpire($minutes);
9596

9697
// create default values
9798
[$path, $value, $domain, $secure, $httponly, $force] = self::getDefaultPathAndDomain(
9899
$path, $value, $domain, $secure, $httponly, $force
99100
);
100101

101-
// set cookie
102-
if ( !headers_sent() || $force === true) {
103-
@setcookie($name, $value, $minutes, $path, $domain, $secure, $httponly);
102+
// Prefer new setcookie signature with array options (PHP 7.3+), fallback otherwise
103+
if (!headers_sent() || $force === true) {
104+
$options = [
105+
'expires' => $expires,
106+
'path' => $path,
107+
'domain' => $domain ?: '',
108+
'secure' => (bool) $secure,
109+
'httponly' => (bool) $httponly,
110+
'samesite' => 'Lax', // sensible default
111+
];
112+
113+
// Try modern signature; if it fails (older PHP), fallback to legacy
114+
try {
115+
if (PHP_VERSION_ID >= 70300) {
116+
@setcookie($name, (string) $value, $options);
117+
} else {
118+
@setcookie($name, (string) $value, (int) $expires, (string) $path, (string) $domain, (bool) $secure, (bool) $httponly);
119+
}
120+
} catch (\Throwable $e) {
121+
@setcookie($name, (string) $value, (int) $expires, (string) $path, (string) $domain, (bool) $secure, (bool) $httponly);
122+
}
104123
}
105124
}
106125

@@ -143,8 +162,7 @@ public static function expire($name, $path = null, $domain = null)
143162
*/
144163
public static function setTime()
145164
{
146-
self::init()
147-
->set(self::$timeName, self::$timeFormat);
165+
self::init()->set(self::$timeName, self::$timeFormat);
148166
}
149167

150168
/**
@@ -154,8 +172,7 @@ public static function setTime()
154172
*/
155173
public static function setExpire()
156174
{
157-
self::init()
158-
->set(self::$expireName, self::$expireFormat);
175+
self::init()->set(self::$expireName, self::$expireFormat);
159176
}
160177

161178
/**
@@ -200,9 +217,7 @@ public static function has($name = null)
200217
*/
201218
public static function get($name = null)
202219
{
203-
return self::has($name)
204-
? $_COOKIE[(string) $name]
205-
: null;
220+
return self::has($name) ? $_COOKIE[(string) $name] : null;
206221
}
207222

208223
/**
@@ -226,24 +241,23 @@ public static function all($name = null)
226241
*/
227242
private static function minutesToExpire($minutes = 0)
228243
{
229-
// options
230-
if(empty($minutes)){
231-
$minutes = 0;
232-
} elseif(is_numeric($minutes)){
233-
$minutes = time() + (((int) $minutes) * 60);
234-
} else{
235-
$minutes = strtotime($minutes);
236-
if ($minutes === false || $minutes === -1) {
237-
// Invalid timestamp format, default to 0 (end of session)
238-
return 0;
239-
}
244+
if (empty($minutes)) {
245+
return 0;
240246
}
241-
242-
return (int) $minutes;
247+
if (is_numeric($minutes)) {
248+
return time() + (((int) $minutes) * 60);
249+
}
250+
251+
$ts = strtotime((string) $minutes);
252+
if ($ts === false || $ts === -1) {
253+
// Invalid timestamp format, default to 0 (end of session)
254+
return 0;
255+
}
256+
return (int) $ts;
243257
}
244258

245259
/**
246-
* Get the path and domain, or the default values.
260+
* Get default path/domain and flags.
247261
*
248262
* @param string|null $path
249263
* @param string|null $value
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tamedevelopers\Support\Process\Concerns;
6+
7+
/**
8+
* Defines a lightweight, framework-agnostic HTTP request contract.
9+
*/
10+
interface RequestInterface
11+
{
12+
/**
13+
* Get the HTTP method (e.g., GET, POST).
14+
* @return string
15+
*/
16+
public static function method(): string;
17+
18+
/**
19+
* Get the request URI
20+
*
21+
* @return string
22+
*/
23+
public static function url(): string;
24+
25+
/**
26+
* Get the raw request URI (path + query string).
27+
* @return string
28+
*/
29+
public static function uri(): string;
30+
31+
/**
32+
* Get the request path without the query string.
33+
* @param string|null $path
34+
* @return string
35+
*/
36+
public static function path($path = null): string;
37+
38+
/**
39+
* Get Server Path
40+
* @return string
41+
*/
42+
public static function server(): string;
43+
44+
/**
45+
* Get Request Url
46+
* @return string
47+
*/
48+
public static function request(): string;
49+
50+
/**
51+
* Get Referral Url
52+
* @return string|null
53+
*/
54+
public static function referral(): ?string;
55+
56+
/**
57+
* Get URL HTTP
58+
* @return string
59+
*/
60+
public static function http(): string;
61+
62+
/**
63+
* Get URL Host
64+
* @return string
65+
*/
66+
public static function host(): string;
67+
68+
/**
69+
* Get URL Fullpath
70+
* @return string
71+
*/
72+
public static function full(): string;
73+
74+
/**
75+
* Retrieve a value from the query string or all query params.
76+
* @param string|int|null $key Key to retrieve, or null to get all.
77+
* @param mixed $default Default value if key is missing.
78+
* @return mixed
79+
*/
80+
public static function query($key = null, $default = null);
81+
82+
/**
83+
* Retrieve a value from POST body or all POST params.
84+
* @param string|int|null $key Key to retrieve, or null to get all.
85+
* @param mixed $default Default value if key is missing.
86+
* @return mixed
87+
*/
88+
public static function post($key = null, $default = null);
89+
90+
/**
91+
* Retrieve from merged input (POST first, then GET) or all inputs.
92+
* @param string|int|null $key Key to retrieve, or null to get all.
93+
* @param mixed $default Default value if key is missing.
94+
* @return mixed
95+
*/
96+
public static function input($key = null, $default = null);
97+
98+
/**
99+
* Get a specific HTTP header value (case-insensitive).
100+
* @param string $key Header name.
101+
* @param mixed $default Default value if missing.
102+
* @return mixed
103+
*/
104+
public static function header(string $key, $default = null);
105+
106+
/**
107+
* Get all HTTP headers as an associative array.
108+
* @return array<string, string>
109+
*/
110+
public static function headers(): array;
111+
112+
/**
113+
* Get a specific cookie value.
114+
* @param string $key Cookie name.
115+
* @param mixed $default Default value if missing.
116+
* @return mixed
117+
*/
118+
public static function cookie(string $key, $default = null);
119+
120+
/**
121+
* Get all cookies.
122+
* @return array<string, string>
123+
*/
124+
public static function cookies(): array;
125+
126+
/**
127+
* Attempt to determine the client IP address.
128+
* @return string|null
129+
*/
130+
public static function ip(): ?string;
131+
132+
/**
133+
* Determine if the request was made via AJAX.
134+
* @return bool
135+
*/
136+
public static function isAjax(): bool;
137+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tamedevelopers\Support\Process\Concerns;
6+
7+
/**
8+
* Defines a lightweight, framework-agnostic session contract.
9+
*/
10+
interface SessionInterface
11+
{
12+
/**
13+
* Start the session if not already started.
14+
* @return void
15+
*/
16+
public function start(): void;
17+
18+
/**
19+
* Get the current session ID.
20+
* @return string|null The session ID or null if none.
21+
*/
22+
public function id(): ?string;
23+
24+
/**
25+
* Regenerate the session ID.
26+
* @param bool $deleteOldSession When true, deletes the old session.
27+
* @return bool True on success, false otherwise.
28+
*/
29+
public function regenerate(bool $deleteOldSession = false): bool;
30+
31+
/**
32+
* Retrieve a value from the session.
33+
* @param string $key The key to retrieve.
34+
* @param mixed $default Default value if key is missing.
35+
* @return mixed
36+
*/
37+
public function get(string $key, $default = null);
38+
39+
/**
40+
* Store a value in the session.
41+
* @param string $key The key to store under.
42+
* @param mixed $value The value to store.
43+
* @return void
44+
*/
45+
public function put(string $key, $value): void;
46+
47+
/**
48+
* Determine if the given key exists in the session.
49+
* @param string $key The key to check.
50+
* @return bool
51+
*/
52+
public function has(string $key): bool;
53+
54+
/**
55+
* Remove a key from the session.
56+
* @param string $key The key to remove.
57+
* @return void
58+
*/
59+
public function forget(string $key): void;
60+
61+
/**
62+
* Get all session data.
63+
* @return array
64+
*/
65+
public function all(): array;
66+
67+
/**
68+
* Destroy the session and clear stored data.
69+
* @return void
70+
*/
71+
public function destroy(): void;
72+
}

Process/Http.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tamedevelopers\Support\Process;
6+
7+
use Tamedevelopers\Support\Process\HttpRequest;
8+
9+
/**
10+
* Native PHP request implementation for RequestInterface.
11+
*/
12+
final class Http extends HttpRequest
13+
{
14+
15+
}

0 commit comments

Comments
 (0)