Skip to content

Commit 9fcab55

Browse files
committed
Refactor static methods
1 parent 10b605b commit 9fcab55

File tree

2 files changed

+58
-31
lines changed

2 files changed

+58
-31
lines changed

system/HTTP/URL.php

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace CodeIgniter\HTTP;
1313

1414
use CodeIgniter\HTTP\IncomingRequest;
15+
use CodeIgniter\Router\Exceptions\RouterException;
1516
use Config\App;
1617
use Config\Services;
1718
use InvalidArgumentException;
@@ -20,7 +21,7 @@
2021
* URI wrapper to work with complete project URLs.
2122
* This class creates immutable instances.
2223
*/
23-
class URL
24+
final class URL
2425
{
2526
/**
2627
* Underlying URI instance
@@ -34,18 +35,29 @@ class URL
3435
*
3536
* @var string
3637
*/
37-
protected $relativePath;
38+
private $relativePath;
3839

3940
//--------------------------------------------------------------------
4041

4142
/**
42-
* Returns an instance representing the base URL.
43+
* Creates the base URL.
44+
*
45+
* @return static
46+
*/
47+
public static function base()
48+
{
49+
return static::public('');
50+
}
51+
52+
/**
53+
* Creates a URL to unrouted public files (typically assets).
54+
* Similar to base_url('path/to/file')
4355
*
4456
* @param string $uri Additional URI string to include
4557
*
4658
* @return static
4759
*/
48-
final public static function base(string $uri = '')
60+
public static function public(string $uri)
4961
{
5062
// Base URLs never include the index page
5163
$config = clone config('App');
@@ -55,33 +67,45 @@ final public static function base(string $uri = '')
5567
}
5668

5769
/**
58-
* Returns an instance representing a routed URL.
59-
*
60-
* @param string $uri Named route, reverse route, or URI string
70+
* Returns an instance representing the current URL.
6171
*
6272
* @return static
6373
*/
64-
final public static function to(string $uri)
74+
public static function current()
6575
{
66-
$uri = rtrim($uri, '/ ');
67-
68-
// Check for a named or reverse-route
69-
if ($uri !== '' && $route = Services::routes()->reverseRoute($uri))
70-
{
71-
return new static($route);
72-
}
76+
return static::fromRequest(Services::request());
77+
}
7378

74-
return new static($uri);
79+
/**
80+
* Creates a framework URL.
81+
*
82+
* @param string $uri
83+
*
84+
* @return static
85+
*/
86+
public static function to(string $uri)
87+
{
88+
return new static(rtrim($uri, '/ '));
7589
}
7690

7791
/**
78-
* Returns an instance representing the current URL.
92+
* Creates a URL to a named or reverse route.
93+
*
94+
* @param string $uri Named or reverse route
7995
*
8096
* @return static
97+
*
98+
* @throws RouterException
8199
*/
82-
final public static function current()
100+
public static function route(string $uri)
83101
{
84-
return static::fromRequest(Services::request());
102+
// Check for a named or reverse-route
103+
if ($route = Services::routes()->reverseRoute($uri))
104+
{
105+
return new static($route);
106+
}
107+
108+
throw RouterException::forInvalidRoute($uri);
85109
}
86110

87111
/**
@@ -92,7 +116,7 @@ final public static function current()
92116
*
93117
* @return static
94118
*/
95-
final public static function fromRequest(IncomingRequest $request)
119+
public static function fromRequest(IncomingRequest $request)
96120
{
97121
$path = $request->detectPath($request->config->uriProtocol);
98122
$query = isset($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '';
@@ -109,7 +133,7 @@ final public static function fromRequest(IncomingRequest $request)
109133
* @param string $relativePath
110134
* @param App|null $config
111135
*/
112-
final public function __construct(string $relativePath = '', App $config = null)
136+
public function __construct(string $relativePath = '', App $config = null)
113137
{
114138
$config = $config ?? config('App');
115139

@@ -175,11 +199,14 @@ public function getUri(): URI
175199

176200
/**
177201
* Returns this URL as a string.
202+
* Since this is typically for routing and
203+
* link purposes we strip any queries, but
204+
* they can be accessed via getUri().
178205
*
179206
* @return string
180207
*/
181208
public function __toString(): string
182209
{
183-
return (string) $this->uri;
210+
return (string) $this->getUri()->setQuery('');
184211
}
185212
}

tests/system/HTTP/URLTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ public function testBase(array $configs, string $baseURL, string $siteURL)
193193
$this->assertSame($baseURL, (string) $url);
194194
}
195195

196-
public function testBaseWithUri()
196+
public function testPublic()
197197
{
198-
$url = URL::base('images/cat.gif');
198+
$url = URL::public('images/cat.gif');
199199

200200
$this->assertSame('http://example.com/images/cat.gif', (string) $url);
201201
}
@@ -207,20 +207,20 @@ public function testTo()
207207
$this->assertSame('http://example.com/index.php/fruit/basket', (string) $url);
208208
}
209209

210-
public function testToNamedRoute()
210+
public function testNamedRoute()
211211
{
212-
Services::routes()->add('apples', 'Home::index', ['as' => 'home']);
212+
Services::routes()->add('apples', 'Home::index', ['as' => 'orchard']);
213213

214-
$url = URL::to('home');
214+
$url = URL::route('orchard');
215215

216216
$this->assertSame('http://example.com/index.php/apples', (string) $url);
217217
}
218218

219-
public function testToReverseRoute()
219+
public function testReverseRoute()
220220
{
221221
Services::routes()->add('oranges', 'Basket::fruit');
222222

223-
$url = URL::to('App\Controllers\Basket::fruit');
223+
$url = URL::route('App\Controllers\Basket::fruit');
224224

225225
$this->assertSame('http://example.com/index.php/oranges', (string) $url);
226226
}
@@ -367,11 +367,11 @@ public function currentProvider(): array
367367
],
368368
[
369369
'?blahblah=true',
370-
'http://example.com/index.php?blahblah=true',
370+
'http://example.com/index.php',
371371
],
372372
[
373373
'http://example.com/index.php?blahblah=true',
374-
'http://example.com/index.php?blahblah=true',
374+
'http://example.com/index.php',
375375
],
376376
];
377377
}

0 commit comments

Comments
 (0)