Utils.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace Knuckles\Scribe\Tools;
  3. use Closure;
  4. use Exception;
  5. use Illuminate\Routing\Route;
  6. use Knuckles\Scribe\Tools\ConsoleOutputUtils as c;
  7. use League\Flysystem\Adapter\Local;
  8. use League\Flysystem\Filesystem;
  9. use ReflectionClass;
  10. use ReflectionException;
  11. use ReflectionFunction;
  12. use ReflectionFunctionAbstract;
  13. class Utils
  14. {
  15. public static function getFullUrl(Route $route, array $urlParameters = []): string
  16. {
  17. $uri = $route->uri();
  18. return self::replaceUrlParameterPlaceholdersWithValues($uri, $urlParameters);
  19. }
  20. public static function getRouteClassAndMethodNames(Route $route): array
  21. {
  22. $action = $route->getAction();
  23. $uses = $action['uses'];
  24. if ($uses !== null) {
  25. if (is_array($uses)) {
  26. return $uses;
  27. } elseif (is_string($uses)) {
  28. return explode('@', $uses);
  29. } elseif (static::isInvokableObject($uses)) {
  30. return [$uses, '__invoke'];
  31. }
  32. }
  33. if (array_key_exists(0, $action) && array_key_exists(1, $action)) {
  34. return [
  35. 0 => $action[0],
  36. 1 => $action[1],
  37. ];
  38. }
  39. throw new Exception("Couldn't get class and method names for route ". c::getRouteRepresentation($route).'.');
  40. }
  41. /**
  42. * Transform parameters in URLs into real values (/users/{user} -> /users/2).
  43. * Uses @urlParam values specified by caller, otherwise just uses '1'.
  44. *
  45. * @param string $uri
  46. * @param array $urlParameters Dictionary of url params and example values
  47. *
  48. * @return mixed
  49. */
  50. public static function replaceUrlParameterPlaceholdersWithValues(string $uri, array $urlParameters)
  51. {
  52. $matches = preg_match_all('/{.+?}/i', $uri, $parameterPaths);
  53. if (!$matches) {
  54. return $uri;
  55. }
  56. foreach ($parameterPaths[0] as $parameterPath) {
  57. $key = trim($parameterPath, '{?}');
  58. if (isset($urlParameters[$key])) {
  59. $example = $urlParameters[$key];
  60. $uri = str_replace($parameterPath, $example, $uri);
  61. }
  62. }
  63. // Remove unbound optional parameters with nothing
  64. $uri = preg_replace('#{([^/]+\?)}#', '', $uri);
  65. // Replace any unbound non-optional parameters with '1'
  66. $uri = preg_replace('#{([^/]+)}#', '1', $uri);
  67. return $uri;
  68. }
  69. public static function deleteDirectoryAndContents($dir, $base = null)
  70. {
  71. $dir = ltrim($dir, '/');
  72. $adapter = new Local($base ?: realpath(__DIR__ . '/../../'));
  73. $fs = new Filesystem($adapter);
  74. $fs->deleteDir($dir);
  75. }
  76. /**
  77. * @param mixed $value
  78. *
  79. * @return bool
  80. */
  81. public static function isInvokableObject($value): bool
  82. {
  83. return is_object($value) && method_exists($value, '__invoke');
  84. }
  85. /**
  86. * Returns the route method or closure as an instance of ReflectionMethod or ReflectionFunction
  87. *
  88. * @param array $routeControllerAndMethod
  89. *
  90. * @throws ReflectionException
  91. *
  92. * @return ReflectionFunctionAbstract
  93. */
  94. public static function getReflectedRouteMethod(array $routeControllerAndMethod): ReflectionFunctionAbstract
  95. {
  96. [$class, $method] = $routeControllerAndMethod;
  97. if ($class instanceof Closure) {
  98. return new ReflectionFunction($class);
  99. }
  100. return (new ReflectionClass($class))->getMethod($method);
  101. }
  102. public static function getModelFactory(string $modelName, array $states = [])
  103. {
  104. if (version_compare(app()->version(), '8.0.0', '>=')) {
  105. $factory = call_user_func_array([$modelName, 'factory'], []);
  106. if (count($states)) {
  107. foreach ($states as $state) {
  108. $factory = $factory->$state();
  109. }
  110. }
  111. } else {
  112. $factory = factory($modelName);
  113. if (count($states)) {
  114. $factory = $factory->states($states);
  115. }
  116. }
  117. return $factory;
  118. }
  119. }