ConsoleOutputUtils.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Knuckles\Scribe\Tools;
  3. use Illuminate\Routing\Route;
  4. use Symfony\Component\Console\Output\ConsoleOutput;
  5. use Symfony\Component\Console\Output\OutputInterface;
  6. class ConsoleOutputUtils
  7. {
  8. /**
  9. * @var \Shalvah\Clara\Clara|null
  10. */
  11. private static $clara = null;
  12. public static function bootstrapOutput(OutputInterface $outputInterface)
  13. {
  14. $showDebug = Flags::$shouldBeVerbose;
  15. self::$clara = clara('knuckleswtf/scribe', $showDebug)
  16. ->useOutput($outputInterface)
  17. ->only();
  18. }
  19. public static function warn($message)
  20. {
  21. if (!self::$clara) {
  22. self::bootstrapOutput(new ConsoleOutput);
  23. }
  24. self::$clara->warn($message);
  25. }
  26. public static function info($message)
  27. {
  28. if (!self::$clara) {
  29. self::bootstrapOutput(new ConsoleOutput);
  30. }
  31. self::$clara->info($message);
  32. }
  33. public static function debug($message)
  34. {
  35. if (!self::$clara) {
  36. self::bootstrapOutput(new ConsoleOutput);
  37. }
  38. self::$clara->debug($message);
  39. }
  40. public static function success($message)
  41. {
  42. if (!self::$clara) {
  43. self::bootstrapOutput(new ConsoleOutput);
  44. }
  45. self::$clara->success($message);
  46. }
  47. /**
  48. * Return a string representation of a route to output to the console eg [GET] /api/users
  49. * @param Route $route
  50. *
  51. * @return string
  52. */
  53. public static function getRouteRepresentation(Route $route): string
  54. {
  55. $methods = $route->methods();
  56. if (count($methods) > 1) {
  57. $methods = array_diff($route->methods(), ['HEAD']);
  58. }
  59. $routeMethods = implode(',', $methods);
  60. $routePath = $route->uri();
  61. return "[$routeMethods] $routePath";
  62. }
  63. }