Skip to content

Commit 57c2065

Browse files
author
Fredrick Peter
committed
UrlHelper class -- Rectify using with frameworks
1 parent ef6e86e commit 57c2065

File tree

7 files changed

+147
-57
lines changed

7 files changed

+147
-57
lines changed

Asset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static public function config(?string $base_path = null, ?bool $cache = true)
8080
'cache' => $cache,
8181
'server' => self::formatWithBaseDirectory($base_path),
8282
'domain' => rtrim(
83-
self::cleanServerPath("{$server['domain']}/{$base_path}"),
83+
self::cleanServerPath("{$server['domain']}{$base_path}"),
8484
'/'
8585
),
8686
]);

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ env_update('DB_CHARSET', 'utf8', false);
138138

139139
### Get Servers
140140
- Returns assoc arrays of Server
141-
- `server\|domain\|protocol`
141+
- `server\|domain`
142142

143143
```
144144
use Tamedevelopers\Support\Server;

Tame.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ static public function class_exists($class, callable $function = null)
7878
}
7979
}
8080

81+
/**
82+
* Check if at least one class exists
83+
*
84+
* @param array $classNames Array of class names to check
85+
* @return bool True if at least one class exists, false otherwise
86+
*/
87+
static public function checkAnyClassExists(array $classNames)
88+
{
89+
foreach ($classNames as $className) {
90+
if (class_exists($className)) {
91+
return true;
92+
}
93+
}
94+
return false;
95+
}
96+
8197
/**
8298
* PHP Version Compare
8399
*

Tests/url.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
use Tamedevelopers\Support\Translator;
4+
5+
require_once __DIR__ . '/../vendor/autoload.php';
6+
7+
// Powerful support system form any PHP application without the need of setup
8+
// URL Helpers
9+
10+
11+
12+
dd(
13+
domain(),
14+
15+
domain('admin'),
16+
17+
asset('js/app.js'),
18+
);
19+

Traits/ServerTrait.php

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
namespace Tamedevelopers\Support\Traits;
66

77
use ReflectionClass;
8+
use Tamedevelopers\Support\Env;
89
use Tamedevelopers\Support\Str;
10+
use Tamedevelopers\Support\UrlHelper;
911

1012

1113
trait ServerTrait{
@@ -112,10 +114,10 @@ static public function formatWithDomainURI($path = null)
112114
*
113115
* @param string|null $mode
114116
* - [optional] get direct info of data
115-
* - server|domain|protocol
117+
* - server|domain
116118
*
117119
* @return mixed
118-
* - An associative array containing\ server|domain|protocol
120+
* - An associative array containing\ server|domain
119121
*/
120122
static public function getServers($mode = null)
121123
{
@@ -126,15 +128,11 @@ static public function getServers($mode = null)
126128
$serverPath = self::cleanServerPath(
127129
self::createAbsolutePath()
128130
);
129-
130-
// Replace Document root inside server path
131-
$domainPath = self::createAbsoluteDomain($serverPath);
132-
131+
133132
// Data
134133
$data = [
135134
'server' => $serverPath,
136-
'domain' => $domainPath['domain'],
137-
'protocol' => $domainPath['protocol'],
135+
'domain' => UrlHelper::url(),
138136
];
139137

140138
/*
@@ -151,7 +149,6 @@ static public function getServers($mode = null)
151149
$data = [
152150
'server' => $serverData['server'],
153151
'domain' => $serverData['domain'],
154-
'protocol' => $serverData['protocol'],
155152
];
156153
}
157154

@@ -197,7 +194,7 @@ static public function pathReplacer($path, $replacer = '/')
197194
*
198195
* @return string
199196
*/
200-
static private function createAbsolutePath()
197+
static public function createAbsolutePath()
201198
{
202199
// get direct root path
203200
$projectRootPath = self::getDirectRootPath();
@@ -211,38 +208,6 @@ static private function createAbsolutePath()
211208
return $projectRootPath;
212209
}
213210

214-
/**
215-
* Create Server Absolute Path
216-
* @param string|null $serverPath
217-
*
218-
* @return array
219-
*/
220-
static private function createAbsoluteDomain($serverPath = null)
221-
{
222-
// Determine the protocol (http or https)
223-
$protocol = Str::lower($_SERVER['HTTPS'] ?? null) !== 'off'
224-
? 'https://'
225-
: 'http://';
226-
227-
// The Document root path
228-
$docRoot = $_SERVER['DOCUMENT_ROOT'];
229-
230-
// Get the server name (hostname)
231-
$serverName = $_SERVER['SERVER_NAME'] ?? null;
232-
233-
// Replace Document root inside server path
234-
$domainPath = str_replace($docRoot, '', $serverPath);
235-
236-
// trim(string, '/) - Trim forward slash from left and right
237-
// we using right trim only
238-
$domainPath = rtrim((string) $domainPath, '/');
239-
240-
return [
241-
'domain' => "{$protocol}{$serverName}{$domainPath}",
242-
'protocol' => $protocol,
243-
];
244-
}
245-
246211
/**
247212
* Get Root path with vendor helper
248213
*

UrlHelper.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tamedevelopers\Support;
6+
7+
use Tamedevelopers\Support\Server;
8+
9+
class UrlHelper {
10+
11+
/**
12+
* Get the URL
13+
*
14+
* @return string
15+
*/
16+
static public function url()
17+
{
18+
$baseURL = self::getBaseURL();
19+
$scriptName = $_SERVER['SCRIPT_NAME'];
20+
21+
// generate url
22+
$url = $baseURL['full_path'] . self::getURLPath($scriptName);
23+
24+
// if App is Core PHP
25+
if(!AppIsNotCorePHP()){
26+
$url = self::localUrl($baseURL);
27+
}
28+
29+
return trim($url, '\/') . '/';
30+
}
31+
32+
/**
33+
* Get the base URL
34+
*
35+
* @return array
36+
*/
37+
static private function getBaseURL()
38+
{
39+
$http = isset($_SERVER['HTTPS']) && Str::lower($_SERVER['HTTPS']) !== 'off' ? 'https://' : 'http://';
40+
$hostname = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost';
41+
42+
return [
43+
'http' => $http,
44+
'hostname' => $hostname,
45+
'full_path' => "{$http}{$hostname}",
46+
];
47+
}
48+
49+
/**
50+
* Get the URL path
51+
*
52+
* @param string $scriptName
53+
* @return string
54+
*/
55+
static private function getURLPath($scriptName)
56+
{
57+
$dir = str_replace(basename($scriptName), '', $scriptName);
58+
return $dir;
59+
}
60+
61+
/**
62+
* Create Server Absolute Path
63+
* @param array $baseURL
64+
*
65+
* @return array
66+
*/
67+
static private function localUrl($baseURL)
68+
{
69+
// create server path
70+
$serverPath = Server::cleanServerPath(
71+
Server::createAbsolutePath()
72+
);
73+
74+
// Get the server name (hostname)
75+
$serverName = $_SERVER['SERVER_NAME'] ?? null;
76+
77+
// Replace Document root inside server path
78+
$domainPath = str_replace($_SERVER['DOCUMENT_ROOT'], '', $serverPath);
79+
80+
// trim(string, '/) - Trim forward slash from left and right
81+
// we using right trim only
82+
$domainPath = rtrim((string) $domainPath, '\/');
83+
84+
return "{$baseURL['http']}{$serverName}{$domainPath}";
85+
}
86+
87+
}

helpers.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,22 @@
1414
use Tamedevelopers\Support\Capsule\FileCache;
1515

1616

17-
if (! function_exists('TameIsLaravelDetect')) {
17+
if (! function_exists('AppIsNotCorePHP')) {
1818
/**
19-
* Check if Package is inside Laravel Application
19+
* Check if Application is not Core PHP
20+
* If running on other frameworks
2021
*
2122
* @return bool
2223
*/
23-
function TameIsLaravelDetect()
24+
function AppIsNotCorePHP()
2425
{
25-
if(class_exists('Illuminate\Foundation\Application') || class_exists('Illuminate\Container\Container')){
26-
return true;
27-
}
28-
29-
return false;
26+
// get_declared_classes()
27+
// using the above `get_declared_classes()` function will return all classes in your project
28+
// Check if any classe exist
29+
return Tame::checkAnyClassExists([
30+
'Illuminate\Foundation\Application',
31+
'Illuminate\Container\Container',
32+
]);
3033
}
3134
}
3235

@@ -90,7 +93,7 @@ function PDF()
9093
}
9194
}
9295

93-
if (! TameIsLaravelDetect() && ! function_exists('bcrypt')) {
96+
if (! AppIsNotCorePHP() && ! function_exists('bcrypt')) {
9497
/**
9598
* Password Encrypter.
9699
* This function encrypts a password using bcrypt with a generated salt.
@@ -148,7 +151,7 @@ function autoload_register(string|array $directory)
148151
}
149152
}
150153

151-
if (! TameIsLaravelDetect() && ! function_exists('config')) {
154+
if (! AppIsNotCorePHP() && ! function_exists('config')) {
152155
/**
153156
* Get the value of a configuration option.
154157
*
@@ -171,7 +174,7 @@ function config($key, $default = null, ?string $base_folder = 'config')
171174
}
172175
}
173176

174-
if (! TameIsLaravelDetect() && ! function_exists('env')) {
177+
if (! AppIsNotCorePHP() && ! function_exists('env')) {
175178
/**
176179
* Get ENV (Enviroment) Data
177180
* - If .env was not used,
@@ -213,7 +216,7 @@ function env_update(?string $key = null, string|bool $value = null, ?bool $quote
213216
}
214217
}
215218

216-
if (! TameIsLaravelDetect() && ! function_exists('asset')) {
219+
if (! AppIsNotCorePHP() && ! function_exists('asset')) {
217220
/**
218221
* Create assets Real path url
219222
*
@@ -250,7 +253,7 @@ function asset_config($base_path = null, ?bool $cache = true)
250253
}
251254
}
252255

253-
if (! TameIsLaravelDetect() && ! function_exists('__')) {
256+
if (! AppIsNotCorePHP() && ! function_exists('__')) {
254257
/**
255258
* Translate the given message.
256259
*

0 commit comments

Comments
 (0)