-
Notifications
You must be signed in to change notification settings - Fork 185
/
utils.php
172 lines (160 loc) Β· 4.2 KB
/
utils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
declare(strict_types = 1);
namespace LanguageServer;
use Throwable;
use InvalidArgumentException;
use Sabre\Event\{Loop, Promise, EmitterInterface};
use Sabre\Uri;
/**
* Transforms an absolute file path into a URI as used by the language server protocol.
*
* @param string $filepath
* @return string
*/
function pathToUri(string $filepath): string
{
$filepath = trim(str_replace('\\', '/', $filepath), '/');
$parts = explode('/', $filepath);
// Don't %-encode the colon after a Windows drive letter
$first = array_shift($parts);
if (substr($first, -1) !== ':') {
$first = rawurlencode($first);
}
$parts = array_map('rawurlencode', $parts);
array_unshift($parts, $first);
$filepath = implode('/', $parts);
return 'file:///' . $filepath;
}
/**
* Transforms URI into file path
*
* @param string $uri
* @return string
*/
function uriToPath(string $uri)
{
$fragments = parse_url($uri);
if ($fragments === false || !isset($fragments['scheme']) || $fragments['scheme'] !== 'file') {
throw new InvalidArgumentException("Not a valid file URI: $uri");
}
$filepath = urldecode($fragments['path']);
if (strpos($filepath, ':') !== false) {
if ($filepath[0] === '/') {
$filepath = substr($filepath, 1);
}
$filepath = str_replace('/', '\\', $filepath);
}
return $filepath;
}
/**
* Throws an exception on the next tick.
* Useful for letting a promise crash the process on rejection.
*
* @param Throwable $err
* @return void
*/
function crash(Throwable $err)
{
Loop\nextTick(function () use ($err) {
throw $err;
});
}
/**
* Returns a promise that is resolved after x seconds.
* Useful for giving back control to the event loop inside a coroutine.
*
* @param int $seconds
* @return Promise <void>
*/
function timeout($seconds = 0): Promise
{
$promise = new Promise;
Loop\setTimeout([$promise, 'fulfill'], $seconds);
return $promise;
}
/**
* Returns a promise that is fulfilled once the passed event was triggered on the passed EventEmitter
*
* @param EmitterInterface $emitter
* @param string $event
* @return Promise
*/
function waitForEvent(EmitterInterface $emitter, string $event): Promise
{
$p = new Promise;
$emitter->once($event, [$p, 'fulfill']);
return $p;
}
/**
* Returns the part of $b that is not overlapped by $a
* Example:
*
* stripStringOverlap('whatever<?', '<?php') === 'php'
*
* @param string $a
* @param string $b
* @return string
*/
function stripStringOverlap(string $a, string $b): string
{
$aLen = strlen($a);
$bLen = strlen($b);
for ($i = 1; $i <= $bLen; $i++) {
if (substr($b, 0, $i) === substr($a, $aLen - $i)) {
return substr($b, $i);
}
}
return $b;
}
/**
* Use for sorting an array of URIs by number of segments
* in ascending order.
*
* @param array $uriList
* @return void
*/
function sortUrisLevelOrder(&$uriList)
{
usort($uriList, function ($a, $b) {
return substr_count(Uri\parse($a)['path'], '/') - substr_count(Uri\parse($b)['path'], '/');
});
}
/**
* Checks a document against the composer.json to see if it
* is a vendored document
*
* @param PhpDocument $document
* @param \stdClass|null $composerJson
* @return bool
*/
function isVendored(PhpDocument $document, \stdClass $composerJson = null): bool
{
$path = Uri\parse($document->getUri())['path'];
$vendorDir = getVendorDir($composerJson);
return strpos($path, "/$vendorDir/") !== false;
}
/**
* Check a given URI against the composer.json to see if it
* is a vendored URI
*
* @param string $uri
* @param \stdClass|null $composerJson
* @return string|null
*/
function getPackageName(string $uri, \stdClass $composerJson = null)
{
$vendorDir = str_replace('/', '\/', getVendorDir($composerJson));
preg_match("/\/$vendorDir\/([^\/]+\/[^\/]+)\//", $uri, $matches);
return $matches[1] ?? null;
}
/**
* Helper function to get the vendor directory from composer.json
* or default to 'vendor'
*
* @param \stdClass|null $composerJson
* @return string
*/
function getVendorDir(\stdClass $composerJson = null): string
{
return $composerJson->config->{'vendor-dir'} ?? 'vendor';
}