1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mpociot\ApiDoc\Tools; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\Route; |
6
|
|
|
use League\Flysystem\Adapter\Local; |
7
|
|
|
use League\Flysystem\Filesystem; |
8
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\VarExporter\VarExporter; |
11
|
|
|
|
12
|
|
|
class Utils |
13
|
|
|
{ |
14
|
|
|
public static function getFullUrl(Route $route, array $urlParameters = []): string |
15
|
|
|
{ |
16
|
|
|
$uri = $route->uri(); |
17
|
|
|
|
18
|
|
|
return self::replaceUrlParameterPlaceholdersWithValues($uri, $urlParameters); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param array|Route $routeOrAction |
23
|
|
|
* |
24
|
|
|
* @return array|null |
25
|
|
|
*/ |
26
|
|
|
public static function getRouteClassAndMethodNames($routeOrAction) |
27
|
|
|
{ |
28
|
|
|
$action = $routeOrAction instanceof Route ? $routeOrAction->getAction() : $routeOrAction; |
|
|
|
|
29
|
|
|
|
30
|
|
|
if ($action['uses'] !== null) { |
31
|
|
|
if (is_array($action['uses'])) { |
32
|
|
|
return $action['uses']; |
33
|
|
|
} elseif (is_string($action['uses'])) { |
34
|
|
|
return explode('@', $action['uses']); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
if (array_key_exists(0, $action) && array_key_exists(1, $action)) { |
38
|
|
|
return [ |
39
|
|
|
0 => $action[0], |
40
|
|
|
1 => $action[1], |
41
|
|
|
]; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Transform parameters in URLs into real values (/users/{user} -> /users/2). |
47
|
|
|
* Uses @urlParam values specified by caller, otherwise just uses '1'. |
48
|
|
|
* |
49
|
|
|
* @param string $uri |
50
|
|
|
* @param array $urlParameters Dictionary of url params and example values |
51
|
|
|
* |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
|
|
public static function replaceUrlParameterPlaceholdersWithValues(string $uri, array $urlParameters) |
55
|
|
|
{ |
56
|
|
|
$matches = preg_match_all('/{.+?}/i', $uri, $parameterPaths); |
57
|
|
|
if (!$matches) { |
58
|
|
|
return $uri; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
foreach ($parameterPaths[0] as $parameterPath) { |
62
|
|
|
$key = trim($parameterPath, '{?}'); |
63
|
|
|
if (isset($urlParameters[$key])) { |
64
|
|
|
$example = $urlParameters[$key]; |
65
|
|
|
$uri = str_replace($parameterPath, $example, $uri); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
// Remove unbound optional parameters with nothing |
69
|
|
|
$uri = preg_replace('#{([^/]+\?)}#', '', $uri); |
70
|
|
|
// Replace any unbound non-optional parameters with '1' |
71
|
|
|
$uri = preg_replace('#{([^/]+)}#', '1', $uri); |
72
|
|
|
|
73
|
|
|
return $uri; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public static function dumpException(\Exception $e) |
77
|
|
|
{ |
78
|
|
|
if (class_exists(\NunoMaduro\Collision\Handler::class)) { |
79
|
|
|
$output = new ConsoleOutput(OutputInterface::VERBOSITY_VERBOSE); |
80
|
|
|
$handler = new \NunoMaduro\Collision\Handler(new \NunoMaduro\Collision\Writer($output)); |
81
|
|
|
$handler->setInspector(new \Whoops\Exception\Inspector($e)); |
82
|
|
|
$handler->setException($e); |
83
|
|
|
$handler->handle(); |
84
|
|
|
} else { |
85
|
|
|
dump($e); |
86
|
|
|
echo "You can get better exception output by installing the library \nunomaduro/collision (PHP 7.1+ only).\n"; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public static function deleteDirectoryAndContents($dir) |
91
|
|
|
{ |
92
|
|
|
$dir = ltrim($dir, '/'); |
93
|
|
|
$adapter = new Local(realpath(__DIR__ . '/../../')); |
94
|
|
|
$fs = new Filesystem($adapter); |
95
|
|
|
$fs->deleteDir($dir); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param mixed $value |
100
|
|
|
* @param int $indentationLevel |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
* @throws \Symfony\Component\VarExporter\Exception\ExceptionInterface |
104
|
|
|
* |
105
|
|
|
*/ |
106
|
|
|
public static function printPhpValue($value, int $indentationLevel = 0): string |
107
|
|
|
{ |
108
|
|
|
$output = VarExporter::export($value); |
109
|
|
|
// Padding with x spaces so they align |
110
|
|
|
$split = explode("\n", $output); |
111
|
|
|
$result = ''; |
112
|
|
|
$padWith = str_repeat(' ', $indentationLevel); |
113
|
|
|
foreach ($split as $index => $line) { |
114
|
|
|
$result .= ($index == 0 ? '' : "\n$padWith") . $line; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $result; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public static function printQueryParamsAsString(array $cleanQueryParams): string |
121
|
|
|
{ |
122
|
|
|
$qs = ''; |
123
|
|
|
foreach ($cleanQueryParams as $parameter => $value) { |
124
|
|
|
$paramName = urlencode($parameter); |
125
|
|
|
|
126
|
|
|
if (!is_array($value)) { |
127
|
|
|
$qs .= "$paramName=" . urlencode($value) . "&"; |
128
|
|
|
} else { |
129
|
|
|
if (array_keys($value)[0] === 0) { |
130
|
|
|
// List query param (eg filter[]=haha should become "filter[]": "haha") |
131
|
|
|
$qs .= "$paramName" . '[]=' . urlencode($value[0]) . '&'; |
132
|
|
|
} else { |
133
|
|
|
// Hash query param (eg filter[name]=john should become "filter[name]": "john") |
134
|
|
|
foreach ($value as $item => $itemValue) { |
135
|
|
|
$qs .= "$paramName" . '[' . urlencode($item) . ']=' . urlencode($itemValue) . '&'; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return rtrim($qs, '&'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public static function printQueryParamsAsKeyValue( |
145
|
|
|
array $cleanQueryParams, |
146
|
|
|
string $quote = "\"", |
147
|
|
|
string $delimiter = ":", |
148
|
|
|
int $spacesIndentation = 4, |
149
|
|
|
string $braces = "{}", |
150
|
|
|
int $closingBraceIndentation = 0 |
151
|
|
|
): string { |
152
|
|
|
$output = "{$braces[0]}\n"; |
153
|
|
|
foreach ($cleanQueryParams as $parameter => $value) { |
154
|
|
|
if (!is_array($value)) { |
155
|
|
|
$output .= str_repeat(" ", $spacesIndentation); |
156
|
|
|
$output .= "$quote$parameter$quote$delimiter $quote$value$quote,\n"; |
157
|
|
|
} else { |
158
|
|
|
if (array_keys($value)[0] === 0) { |
159
|
|
|
// List query param (eg filter[]=haha should become "filter[]": "haha") |
160
|
|
|
$output .= str_repeat(" ", $spacesIndentation); |
161
|
|
|
$output .= "$quote$parameter" . "[]$quote$delimiter $quote$value[0]$quote,\n"; |
162
|
|
|
} else { |
163
|
|
|
// Hash query param (eg filter[name]=john should become "filter[name]": "john") |
164
|
|
|
foreach ($value as $item => $itemValue) { |
165
|
|
|
$output .= str_repeat(" ", $spacesIndentation); |
166
|
|
|
$output .= "$quote$parameter" . "[$item]$quote$delimiter $quote$itemValue$quote,\n"; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $output . str_repeat(" ", $closingBraceIndentation) . "{$braces[1]}"; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.