Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

php-simputils-130 UrlObject improve usability #134

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/images/UrlObject url handling explained
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<mxfile host="Electron" modified="2023-03-04T15:58:22.384Z" agent="5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/13.7.9 Chrome/85.0.4183.121 Electron/10.1.3 Safari/537.36" etag="c65FcEAqoxJbDwMy9J1s" version="13.7.9" type="device"><diagram id="LhCH7mreOIoQ4Aile4XN" name="Страница 1">7Vlbc5s6EP41fowHIa6PidPLQzvTmWSmzwrIoKlARJZrO7++KyRsMFDbPaHNae1kjFhJu9JePn3gGV4U2w+SVPlnkVI+c510O8P3M9dFgR/ARUt2RhJ7nhFkkqV20EHwwF6oFTpWumYpXXUGKiG4YlVXmIiypInqyIiUYtMdthS8a7UiGe0JHhLC+9KvLFW5kUZueJB/pCzLG8soiE1PQZrBdiernKRi0xLhdzO8kEIo0yq2C8q18xq/mHnvR3r3C5O0VOdMeMYvbPn0gT0+foqrr3KL0HNxY4PxnfC13bBdrNo1HsikWFd2GJWKbof8Tp6a4U5/XWi/W0gTKgqq5A6GWEWenbHr3m4O3g59K8vbno6tkNgIZ3vNBydAw/rhAp+gAZ8EHMzeLQVsCZKIJKYjeF7r6N0VohSrSkv3Imhl9lpPfToWWF2J4EJ2lM1c7DheEsd9ZblSkPZWAezN6LB68a3etv4/x0AQOE7fAN2SouJ0nojiZ2ZOG0iSYQMRlM6Y5rPU6pUPefl9yiQyF9dc8OgOznJRDJ+hHRCZIRgL2QHmAnCXlplvmbmmZ3yL+BzTAXyWy75pQBBZ7U64D8S9XOsNPSpxKGqTXgUUyj3SppQU3+jCrq4UJdVrZpwfiQhnWQm3nC61Bg0QDMDz1ooLlqbayN0mZ4o+VKZwNnBUgAxwpUyprkQH7jJOVivb1ou1RwGK9gseQJ82yozjWx96ToELngpb3NN4C1GudDNZS767kyT5RtWIB1sBOzgTnXDmUchyIdkLDCGNJigdAHQmyjrKUvvp9QPwE7B3B+LhThUPPIr1OeqEpalFXSo31oW3uoBh+1QOQLVYqVb5aW3dmqz+k/qPtXqnd7pA3bIyG8YCJyGlObEVYbpFav+onDYeaJ8t1QRooc9IvTx87x/uHgWk8v2N64wkuYC0W/KaOeUAJ7S8CDqOsv3VE9lqccMuiwn6iY2iIRYzGdL4f57Z4ajjEzc6k9phZyqnBH+c2sVU/41Ru4bDtXkYvr2Spitp+p2kyf810uSFcxxEHjy6+26AkR9NVMThv8ahRuLRHD3B0QO0DkMYxV5oooF6oOsFQ6CL5n5rluP7E4UvmopyVfqNy3SU60ut/kLKBWU/MIVISXYjM45Jmij57pil1es0212YBCLFSseqTM2Lpi7iXancJfXkOd16CqN57J/J5kI0d6aqm/jNETp/gOX+ZkLXvCb+XzK6Ifp14Fm/wPSu9OtvoF/x26ZfaOj9+F/Nv0YC0pwXuM+/wh4uuvEI55osTEOvGl+JZxm6MSHTsnzmDXGtK8N6xYqJ/B7D8lrQFZzHtlzHn9dI1zy2XM684PbwA2zd1/oZG7/7AQ==</diagram></mxfile>
Binary file added docs/images/UrlObject url handling explained.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 20 additions & 25 deletions src/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,41 +208,36 @@ public static function init(null|array|Box|BasicInitConfig $args = null): BasicI
return $config;
}

private static $_cached_current_url = null;

/**
* @param $refresh
*
*
* @return ?UrlObject
*/
static function currentUrl($refresh = false) {
static function currentUrl() {
if (static::isCLI() && !defined('CURRENT_URL_PRETEND_NOT_CLI')) {
return null;
}
if (!static::$_cached_current_url || $refresh) {
$info = static::info();
$serv = $info->server_var;
$protocol = empty($serv['HTTPS']) || $serv['HTTPS']?'https':'http';
$host = $serv['SERVER_NAME'] ?? null;
if (empty($host)) {
$host = $serv['HTTP_HOST'] ?? null;
}

$server_port = $serv['SERVER_PORT'] ?? null
?intval($serv['SERVER_PORT'])
:null;
$info = static::info();
$serv = $info->server_var;
$protocol = empty($serv['HTTPS']) || $serv['HTTPS']?'https':'http';
$host = $serv['SERVER_NAME'] ?? null;
if (empty($host)) {
$host = $serv['HTTP_HOST'] ?? null;
}

$uri = $serv['REQUEST_URI'] ?? null;
$server_port = $serv['SERVER_PORT'] ?? null
?intval($serv['SERVER_PORT'])
:null;

static::$_cached_current_url = static::url(
host: $host,
path: $uri,
protocol: $protocol,
port: $server_port
);
}
$uri = $serv['REQUEST_URI'] ?? null;

return static::$_cached_current_url;
return static::url(
host: $host,
path: $uri,
protocol: $protocol,
port: $server_port
);
}

public static function metaMagicSpell(string|object $ref, $spell, ...$args) {
Expand Down Expand Up @@ -926,7 +921,7 @@ public static function isCLI(): bool {
static function url(
null|UrlCompatible|string|Box|array $host = null,
null|Box|array|string $path = null,
null|Box|array $params = null,
null|Box|array|string $params = null,
?string $protocol = null,
?string $processor = null,
?string $port = null,
Expand Down
2 changes: 1 addition & 1 deletion src/basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ function ic(?string $name = null): null|InitConfig|BasicInitConfig {
function url(
null|UrlCompatible|string|Box|array $host = null,
null|Box|array|string $path = null,
null|Box|array $params = null,
null|Box|array|string $params = null,
?string $protocol = null,
?string $processor = null,
?string $port = null,
Expand Down
46 changes: 7 additions & 39 deletions src/generic/BasicProtocolProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,16 @@

namespace spaf\simputils\generic;

use spaf\simputils\interfaces\UrlCompatible;
use spaf\simputils\models\Box;
use spaf\simputils\models\UrlObject;
use spaf\simputils\attributes\markers\Deprecated;

/**
* @property-read ?string $protocol
* @deprecated
*/
abstract class BasicProtocolProcessor extends SimpleObject {
#[Deprecated(
'Wrong naming "protocol" instead of commonly used "scheme"',
'\spaf\simputils\generic\BasicSchemeProcessor'
)]
abstract class BasicProtocolProcessor extends BasicSchemeProcessor {

static ?string $default_protocol = null;

/**
* Returns array of supported protocol names
*
* @return Box|string[]|array|null
*/
abstract static function supportedProtocols();

abstract static function parse(UrlCompatible|string $value);

abstract static function generateForSystem(UrlObject $url): string;
abstract static function generateForUser(UrlObject $url): string;
abstract static function generateRelative(UrlObject $url): string;

// FIX
//
// #[Property(type: 'get')]
// protected ?string $_protocol = null;
//
// function __construct(string $protocol) {
// $this->_protocol = $protocol;
// }
//
// abstract function parse(UrlCompatible|string|Box|array $value, bool $is_preparsed = false, $data = null);
//
// abstract function generateForSystem($host, $path, $params, $data): string;
//
// abstract function generateForUser($host, $path, $params, $data): string;
//
// abstract function generateRelative($host, $path, $params, $data): string;

// function __toString(): string {
// return PHP::objToNaiveString($this, ['protocol' => $this->_protocol]);//@codeCoverageIgnore
// }
}
70 changes: 70 additions & 0 deletions src/generic/BasicSchemeProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace spaf\simputils\generic;

use spaf\simputils\attributes\markers\Deprecated;
use spaf\simputils\interfaces\UrlCompatible;
use spaf\simputils\models\Box;
use spaf\simputils\models\UrlObject;

/**
* @property-read ?string $protocol
*/
abstract class BasicSchemeProcessor extends SimpleObject {

static ?string $default_protocol = null;

/**
* Returns array of supported protocol names
*
* @deprecated
* @return Box|string[]|array|null
*
* @noinspection PhpAttributeCanBeAddedToOverriddenMemberInspection*
*/
#[Deprecated(
'Wrong naming "protocol" instead of commonly used "scheme"',
'\spaf\simputils\generic\BasicProtocolProcessor::supportedSchemes'
)]
static function supportedProtocols() {
return null;
}

/**
* Returns array of supported schemes names
*
* TODO Mark it as abstract when deprecated previous code is removed
*
* @return Box|string[]|array|null
*/
static function supportedSchemes() {
return static::supportedProtocols();
}

abstract static function parse(UrlCompatible|string $value);

abstract static function generateForSystem(UrlObject $url): string;
abstract static function generateForUser(UrlObject $url): string;
abstract static function generateRelative(UrlObject $url): string;

// FIX
//
// #[Property(type: 'get')]
// protected ?string $_protocol = null;
//
// function __construct(string $protocol) {
// $this->_protocol = $protocol;
// }
//
// abstract function parse(UrlCompatible|string|Box|array $value, bool $is_preparsed = false, $data = null);
//
// abstract function generateForSystem($host, $path, $params, $data): string;
//
// abstract function generateForUser($host, $path, $params, $data): string;
//
// abstract function generateRelative($host, $path, $params, $data): string;

// function __toString(): string {
// return PHP::objToNaiveString($this, ['protocol' => $this->_protocol]);//@codeCoverageIgnore
// }
}
Loading