Scribe.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace Knuckles\Scribe;
  3. use Knuckles\Camel\Extraction\ExtractedEndpointData;
  4. use Knuckles\Scribe\Commands\GenerateDocumentation;
  5. use Knuckles\Scribe\Tools\Globals;
  6. use Symfony\Component\HttpFoundation\Request;
  7. class Scribe
  8. {
  9. public const VERSION = '4.39.0';
  10. /**
  11. * Specify a callback that will be executed just before a response call is made
  12. * (after configuring the environment and starting a transaction).
  13. *
  14. * @param callable(Request, ExtractedEndpointData): mixed $callable
  15. */
  16. public static function beforeResponseCall(callable $callable)
  17. {
  18. Globals::$__beforeResponseCall = $callable;
  19. }
  20. /**
  21. * Specify a callback that will be executed just after a response call is done
  22. * (allowing to modify the response).
  23. *
  24. * @param callable(Request, ExtractedEndpointData, mixed): mixed $callable
  25. */
  26. public static function afterResponseCall(callable $callable)
  27. {
  28. Globals::$__afterResponseCall = $callable;
  29. }
  30. /**
  31. * Specify a callback that will be executed just before the generate command is executed
  32. *
  33. * @param callable(GenerateDocumentation): mixed $callable
  34. */
  35. public static function bootstrap(callable $callable)
  36. {
  37. Globals::$__bootstrap = $callable;
  38. }
  39. /**
  40. * Specify a callback that will be executed when Scribe is done generating your docs.
  41. * This callback will receive a map of all the output paths generated, that looks like this:
  42. * [
  43. * 'postman' => '/absolute/path/to/postman/collection',
  44. * 'openapi' => '/absolute/path/to/openapi/spec',
  45. * // If you're using `laravel` type, `html` will be null, and vice versa for `blade`.
  46. * 'html' => '/absolute/path/to/index.html/',
  47. * 'blade' => '/absolute/path/to/blade/view',
  48. * // These are paths to asset folders
  49. * 'assets' => [
  50. * 'js' => '/path/to/js/assets/folder',
  51. * 'css' => '/path/to/css/assets/folder',
  52. * 'images' => '/path/to/images/assets/folder',
  53. * ]
  54. * ]
  55. *
  56. * If you disabled `postman` or `openapi`, their values will be null.
  57. *
  58. * @param callable(array): mixed $callable
  59. */
  60. public static function afterGenerating(callable $callable)
  61. {
  62. Globals::$__afterGenerating = $callable;
  63. }
  64. /**
  65. * Specify a callback that will be used by all FormRequest strategies
  66. * to instantiate Form Requests. his callback takes the name of the form request class,
  67. * the current Laravel route being processed, and the controller method.
  68. *
  69. * @param ?callable(string,\Illuminate\Routing\Route,\ReflectionFunctionAbstract): mixed $callable
  70. */
  71. public static function instantiateFormRequestUsing(?callable $callable)
  72. {
  73. Globals::$__instantiateFormRequestUsing = $callable;
  74. }
  75. /**
  76. * Specify a callback that will be called when instantiating an `ExtractedEndpointData` object
  77. * in order to normalize the URL. The default normalization tries to convert URL parameters from
  78. * Laravel resource-style (`users/{user}/projects/{project}`)
  79. * to a general style (`users/{user_id}/projects/{id}`).
  80. * The callback will be passed the default Laravel URL, the route object, the controller method and class.
  81. *
  82. * @param ?callable(string,\Illuminate\Routing\Route,\ReflectionFunctionAbstract,?\ReflectionClass): string $callable
  83. */
  84. public static function normalizeEndpointUrlUsing(?callable $callable)
  85. {
  86. Globals::$__normalizeEndpointUrlUsing = $callable;
  87. }
  88. }