|
@@ -4,6 +4,7 @@ namespace Knuckles\Scribe\Tools;
|
|
|
|
|
|
use Closure;
|
|
|
use Exception;
|
|
|
+use FastRoute\RouteParser\Std;
|
|
|
use Illuminate\Routing\Route;
|
|
|
use Illuminate\Support\Str;
|
|
|
use Knuckles\Scribe\Tools\ConsoleOutputUtils as c;
|
|
@@ -16,38 +17,13 @@ use ReflectionFunctionAbstract;
|
|
|
|
|
|
class Utils
|
|
|
{
|
|
|
- public static function getFullUrl(Route $route, array $urlParameters = []): string
|
|
|
+ public static function getUrlWithBoundParameters(Route $route, array $urlParameters = []): string
|
|
|
{
|
|
|
$uri = $route->uri();
|
|
|
|
|
|
return self::replaceUrlParameterPlaceholdersWithValues($uri, $urlParameters);
|
|
|
}
|
|
|
|
|
|
- public static function getRouteClassAndMethodNames(Route $route): array
|
|
|
- {
|
|
|
- $action = $route->getAction();
|
|
|
-
|
|
|
- $uses = $action['uses'];
|
|
|
-
|
|
|
- if ($uses !== null) {
|
|
|
- if (is_array($uses)) {
|
|
|
- return $uses;
|
|
|
- } elseif (is_string($uses)) {
|
|
|
- return explode('@', $uses);
|
|
|
- } elseif (static::isInvokableObject($uses)) {
|
|
|
- return [$uses, '__invoke'];
|
|
|
- }
|
|
|
- }
|
|
|
- if (array_key_exists(0, $action) && array_key_exists(1, $action)) {
|
|
|
- return [
|
|
|
- 0 => $action[0],
|
|
|
- 1 => $action[1],
|
|
|
- ];
|
|
|
- }
|
|
|
-
|
|
|
- throw new Exception("Couldn't get class and method names for route " . c::getRouteRepresentation($route) . '.');
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* Transform parameters in URLs into real values (/users/{user} -> /users/2).
|
|
|
* Uses @urlParam values specified by caller, otherwise just uses '1'.
|
|
@@ -63,8 +39,23 @@ class Utils
|
|
|
return $uri;
|
|
|
}
|
|
|
|
|
|
- foreach ($urlParameters as $parameterName => $example) {
|
|
|
- $uri = preg_replace('#\{' . $parameterName . '\??}#', $example, $uri);
|
|
|
+ if (self::isLumen()) {
|
|
|
+ $boundUri = '';
|
|
|
+ $possibilities = (new Std)->parse($uri);
|
|
|
+ // See https://github.com/nikic/FastRoute#overriding-the-route-parser-and-dispatcher
|
|
|
+ $possibilityWithAllSegmentsPresent = end($possibilities);
|
|
|
+ foreach ($possibilityWithAllSegmentsPresent as $part) {
|
|
|
+ if (!is_array($part)) {
|
|
|
+ // It's just a path segment, not a URL parameter'
|
|
|
+ $boundUri .= $part;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ $name = $part[0];
|
|
|
+ $boundUri .= $urlParameters[$name];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $boundUri;
|
|
|
}
|
|
|
|
|
|
// Remove unbound optional parameters with nothing
|
|
@@ -75,6 +66,31 @@ class Utils
|
|
|
return $uri;
|
|
|
}
|
|
|
|
|
|
+ public static function getRouteClassAndMethodNames(Route $route): array
|
|
|
+ {
|
|
|
+ $action = $route->getAction();
|
|
|
+
|
|
|
+ $uses = $action['uses'];
|
|
|
+
|
|
|
+ if ($uses !== null) {
|
|
|
+ if (is_array($uses)) {
|
|
|
+ return $uses;
|
|
|
+ } elseif (is_string($uses)) {
|
|
|
+ return explode('@', $uses);
|
|
|
+ } elseif (static::isInvokableObject($uses)) {
|
|
|
+ return [$uses, '__invoke'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (array_key_exists(0, $action) && array_key_exists(1, $action)) {
|
|
|
+ return [
|
|
|
+ 0 => $action[0],
|
|
|
+ 1 => $action[1],
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ throw new Exception("Couldn't get class and method names for route " . c::getRouteRepresentation($route) . '.');
|
|
|
+ }
|
|
|
+
|
|
|
public static function deleteDirectoryAndContents($dir, $base = null)
|
|
|
{
|
|
|
$dir = ltrim($dir, '/');
|
|
@@ -98,9 +114,9 @@ class Utils
|
|
|
*
|
|
|
* @param array $routeControllerAndMethod
|
|
|
*
|
|
|
+ * @return ReflectionFunctionAbstract
|
|
|
* @throws ReflectionException
|
|
|
*
|
|
|
- * @return ReflectionFunctionAbstract
|
|
|
*/
|
|
|
public static function getReflectedRouteMethod(array $routeControllerAndMethod): ReflectionFunctionAbstract
|
|
|
{
|
|
@@ -142,4 +158,18 @@ class Utils
|
|
|
return $factory;
|
|
|
}
|
|
|
|
|
|
+ public static function isLumen(): bool
|
|
|
+ {
|
|
|
+ // See https://github.com/laravel/lumen-framework/blob/99330e6ca2198e228f5894cf84d843c2a539a250/src/Application.php#L163
|
|
|
+ $app = app();
|
|
|
+ if ($app
|
|
|
+ && is_callable([$app, 'version'])
|
|
|
+ && Str::startsWith($app->version(), 'Lumen')
|
|
|
+ ) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
}
|