Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 632527f

Browse files
committed
IMP allows caching siteUrl and baseUrl functions
1 parent af21dd2 commit 632527f

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

‎src/functions.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
use Francerz\Http\Utils\UriHelper;
66

77
if (!function_exists('\Francerz\Http\Utils\siteUrl')) {
8-
function siteUrl(?string $path = null, array $sapiVars = [])
8+
function siteUrl(?string $path = null, array $sapiVars = [], bool$cached = true)
99
{
10-
return UriHelper::getSiteUrl($path, $sapiVars);
10+
return UriHelper::getSiteUrl($path, $sapiVars, $cached);
1111
}
1212
}
1313

1414
if (!function_exists('\Francerz\Http\Utils\baseUrl')) {
15-
function baseUrl(?string $path = null, array $sapiVars = [])
15+
function baseUrl(?string $path = null, array $sapiVars = [], bool$cached = true)
1616
{
17-
return UriHelper::getBaseUrl($path, $sapiVars);
17+
return UriHelper::getBaseUrl($path, $sapiVars, $cached);
1818
}
1919
}

‎src/utils/UriHelper.php‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,18 @@ public static function prependPath(UriInterface $uri, string ...$segments): UriI
158158

159159
return $uri->withPath($prepath . $postpath);
160160
}
161+
162+
/**
163+
* @param UriInterface $uri
164+
* @param string $pattern
165+
* @return array
166+
*/
167+
public static function getPathParams(string $uri, string $pattern)
168+
{
169+
$pattern = '/([-_\w\d])/';
170+
$match = preg_match_all($pattern, $uri, $matches);
171+
return $matches;
172+
}
161173
#endregion
162174

163175
/**
@@ -193,27 +205,32 @@ public static function getPathInfo(?string $requestUri = null, ?string $scriptNa
193205
return '/' . ltrim($pathInfo, '/');
194206
}
195207

196-
public static function getSiteUrl(?string $path = null, array $sapiVars = [])
208+
public static function getSiteUrl(?string $path = null, array $sapiVars = [], bool$cached = true)
197209
{
198210
$sapiVars = array_merge($_SERVER, $sapiVars);
199211
$sapiVars['REQUEST_URI'] = $sapiVars['SCRIPT_NAME'] ?? '';
200-
$uri = static::getCurrentString($sapiVars);
212+
$uri = static::getCurrentString($sapiVars, $cached);
201213
if (isset($path)) {
202214
$uri = rtrim($uri, '/') . '/' . ltrim($path, '/');
203215
}
204216
return $uri;
205217
}
206218

207-
public static function getBaseUrl(?string $path = null, array $sapiVars = [])
219+
public static function getBaseUrl(?string $path = null, array $sapiVars = [], bool$cached = true)
208220
{
209221
$sapiVars = array_merge($_SERVER, $sapiVars);
210222
$scriptName = $sapiVars['SCRIPT_NAME'] ?? '';
211223
$sapiVars['SCRIPT_NAME'] = substr_replace($scriptName, '', strrpos($scriptName, '/'));
212-
return static::getSiteUrl($path, $sapiVars);
224+
return static::getSiteUrl($path, $sapiVars, $cached);
213225
}
214226

215-
private static function getCurrentString(array $sapiVars = [])
227+
private static function getCurrentString(array $sapiVars = [], bool$cached = true)
216228
{
229+
static $uri;
230+
if ($cached && isset($uri)) {
231+
return $uri;
232+
}
233+
217234
$sapiVars = array_merge($_SERVER, $sapiVars);
218235

219236
$scheme = 'http';
@@ -291,6 +308,7 @@ public static function getUser(UriInterface $uri): string
291308
$explode = explode(':', $uri->getUserInfo());
292309
return $explode[0];
293310
}
311+
294312
public static function getPassword(UriInterface $uri): string
295313
{
296314
$explode = explode(':', $uri->getUserInfo());

‎test/UriHelperTest.php‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,23 @@ public function testGetBaseUrl()
6161
baseUrl('/assets/css/style.css', [
6262
'HTTP_HOST' => 'localhost',
6363
'SCRIPT_NAME' => '/index.php'
64-
])
64+
], false)
6565
);
6666
$this->assertEquals(
6767
'https://localhost/assets/css/style.css',
6868
UriHelper::getBaseUrl('/assets/css/style.css', [
6969
'HTTP_HOST' => 'localhost',
7070
'HTTPS' => 'on',
7171
'SCRIPT_NAME' => '/index.php'
72-
])
72+
], false)
7373
);
7474
$this->assertEquals(
7575
'https://localhost/public/assets/css/style.css',
7676
UriHelper::getBaseUrl('/assets/css/style.css', [
7777
'HTTP_HOST' => 'localhost',
7878
'HTTPS' => 'on',
7979
'SCRIPT_NAME' => '/public/index.php'
80-
])
80+
], false)
8181
);
8282
$this->assertEquals(
8383
'https://localhost/public/assets/css/style.css',
@@ -86,7 +86,7 @@ public function testGetBaseUrl()
8686
'SERVER_NAME' => 'localhost',
8787
'SERVER_PORT' => 443,
8888
'SCRIPT_NAME' => '/public/index.php'
89-
])
89+
], false)
9090
);
9191
$this->assertEquals(
9292
'https://localhost:3000/public/assets/css/style.css',
@@ -95,7 +95,7 @@ public function testGetBaseUrl()
9595
'SERVER_NAME' => 'localhost',
9696
'SERVER_PORT' => 3000,
9797
'SCRIPT_NAME' => '/public/index.php'
98-
])
98+
], false)
9999
);
100100
}
101101

@@ -108,7 +108,14 @@ public function testGetSiteUrl()
108108
'SERVER_NAME' => 'localhost',
109109
'SERVER_PORT' => 3000,
110110
'SCRIPT_NAME' => '/public/index.php'
111-
])
111+
], false)
112112
);
113113
}
114+
115+
public function testGetPathParams()
116+
{
117+
$matches = UriHelper::getPathParams('/a/1/b/2', '/a/{a}/b/{b}');
118+
var_dump($matches);
119+
$this->assertTrue(true);
120+
}
114121
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /