|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\Glide\Urls; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use League\Glide\Signatures\SignatureInterface; |
|
7
|
|
|
use League\Uri\Schemes\Http as HttpUri; |
|
8
|
|
|
use League\Uri\Modifiers\AppendSegment; |
|
9
|
|
|
use League\Uri\Modifiers\AddLeadingSlash; |
|
10
|
|
|
|
|
11
|
|
|
class UrlBuilder |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Whether the base URL is a relative domain. |
|
15
|
|
|
* @var Resolve |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $resolve; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The HTTP signature used to sign URLs. |
|
21
|
|
|
* @var SignatureInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $signature; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Create UrlBuilder instance. |
|
27
|
|
|
* @param string $baseUrl The base URL. |
|
28
|
|
|
* @param SignatureInterface|null $signature The HTTP signature used to sign URLs. |
|
29
|
|
|
*/ |
|
30
|
30 |
|
public function __construct($baseUrl = '', SignatureInterface $signature = null) |
|
31
|
|
|
{ |
|
32
|
30 |
|
$this->setBaseUrl($baseUrl); |
|
33
|
27 |
|
$this->setSignature($signature); |
|
34
|
27 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Set the base URL. |
|
38
|
|
|
* @param string $baseUrl The base URL. |
|
39
|
|
|
*/ |
|
40
|
30 |
|
public function setBaseUrl($baseUrl) |
|
41
|
|
|
{ |
|
42
|
30 |
|
static $addLeadingSlash; |
|
43
|
30 |
|
if (!$addLeadingSlash) { |
|
44
|
3 |
|
$addLeadingSlash = new AddLeadingSlash(); |
|
45
|
2 |
|
} |
|
46
|
|
|
|
|
47
|
30 |
|
$this->baseUrl = $addLeadingSlash->__invoke(HttpUri::createFromString($baseUrl)); |
|
|
|
|
|
|
48
|
27 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Set the HTTP signature. |
|
52
|
|
|
* @param SignatureInterface|null $signature The HTTP signature used to sign URLs. |
|
53
|
|
|
*/ |
|
54
|
27 |
|
public function setSignature(SignatureInterface $signature = null) |
|
55
|
|
|
{ |
|
56
|
27 |
|
$this->signature = $signature; |
|
57
|
27 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get the URL. |
|
61
|
|
|
* @param string $path The resource path. |
|
62
|
|
|
* @param array $params The manipulation parameters. |
|
63
|
|
|
* @return string The URL. |
|
64
|
|
|
*/ |
|
65
|
24 |
|
public function getUrl($path, array $params = []) |
|
66
|
|
|
{ |
|
67
|
24 |
|
$uri = (new AppendSegment($path))->__invoke($this->baseUrl); |
|
68
|
24 |
|
if ($this->signature) { |
|
69
|
9 |
|
$params = $this->signature->addSignature($uri->getPath(), $params); |
|
70
|
6 |
|
} |
|
71
|
|
|
|
|
72
|
24 |
|
return (string) $uri->withQuery(http_build_query($params, '', '&', PHP_QUERY_RFC3986)); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: