1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Router\Generator\Traits; |
4
|
|
|
|
5
|
|
|
use Nip\Router\RequestContext; |
6
|
|
|
use Nip\Utility\Arr; |
7
|
|
|
use Nip\Utility\Str; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Trait FormattingTrait |
11
|
|
|
* @package Nip\Router\Generator\Traits |
12
|
|
|
* @method RequestContext getContext() |
13
|
|
|
*/ |
14
|
|
|
trait FormattingTrait |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* A cached copy of the URL root for the current request. |
18
|
|
|
* |
19
|
|
|
* @var string|null |
20
|
|
|
*/ |
21
|
|
|
protected $cachedRoot; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* A cached copy of the URL schema for the current request. |
25
|
|
|
* |
26
|
|
|
* @var string|null |
27
|
|
|
*/ |
28
|
|
|
protected $cachedSchema; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The forced URL root. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $forcedRoot; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The forced schema for URLs. |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $forceScheme; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The callback to use to format hosts. |
46
|
|
|
* |
47
|
|
|
* @var \Closure |
48
|
|
|
*/ |
49
|
|
|
protected $formatHostUsing; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The callback to use to format paths. |
53
|
|
|
* |
54
|
|
|
* @var \Closure |
55
|
|
|
*/ |
56
|
|
|
protected $formatPathUsing; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Format the array of URL parameters. |
60
|
|
|
* |
61
|
|
|
* @param mixed|array $parameters |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function formatParameters($parameters) |
65
|
|
|
{ |
66
|
|
|
$parameters = Arr::wrap($parameters); |
67
|
|
|
// foreach ($parameters as $key => $parameter) { |
|
|
|
|
68
|
|
|
// if ($parameter instanceof UrlRoutable) { |
69
|
|
|
// $parameters[$key] = $parameter->getRouteKey(); |
70
|
|
|
// } |
71
|
|
|
// } |
72
|
|
|
return $parameters; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the base URL for the request. |
77
|
|
|
* |
78
|
|
|
* @param string $scheme |
79
|
|
|
* @param string $root |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function formatRoot($scheme, $root = null) |
83
|
|
|
{ |
84
|
|
|
if (is_null($root)) { |
85
|
|
|
if (is_null($this->cachedRoot)) { |
86
|
|
|
$this->cachedRoot = $this->forcedRoot ?: $this->getContext()->root(); |
87
|
|
|
} |
88
|
|
|
$root = $this->cachedRoot; |
89
|
|
|
} |
90
|
|
|
$start = Str::startsWith($root, 'http://') ? 'http://' : 'https://'; |
91
|
|
|
return preg_replace('~' . $start . '~', $scheme, $root, 1); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get the default scheme for a raw URL. |
96
|
|
|
* |
97
|
|
|
* @param bool|null $secure |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function formatScheme($secure) |
101
|
|
|
{ |
102
|
|
|
if (!is_null($secure)) { |
103
|
|
|
return $secure ? 'https://' : 'http://'; |
104
|
|
|
} |
105
|
|
|
if (is_null($this->cachedSchema)) { |
106
|
|
|
$this->cachedSchema = $this->forceScheme ?: $this->getContext()->getScheme() . '://'; |
107
|
|
|
} |
108
|
|
|
return $this->cachedSchema; |
109
|
|
|
} |
110
|
|
|
/** |
111
|
|
|
* Format the given URL segments into a single URL. |
112
|
|
|
* |
113
|
|
|
* @param string $root |
114
|
|
|
* @param string $path |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function format($root, $path) |
118
|
|
|
{ |
119
|
|
|
$path = '/' . trim($path, '/'); |
120
|
|
|
if ($this->formatHostUsing) { |
121
|
|
|
$root = call_user_func($this->formatHostUsing, $root); |
122
|
|
|
} |
123
|
|
|
if ($this->formatPathUsing) { |
124
|
|
|
$path = call_user_func($this->formatPathUsing, $path); |
125
|
|
|
} |
126
|
|
|
return trim($root . $path, '/'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Remove the index.php file from a path. |
131
|
|
|
* |
132
|
|
|
* @param string $root |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
protected function removeIndex($root) |
136
|
|
|
{ |
137
|
|
|
$i = 'index.php'; |
138
|
|
|
return Str::contains($root, $i) ? str_replace('/' . $i, '', $root) : $root; |
139
|
|
|
} |
140
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.