ScribeServiceProvider.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace Knuckles\Scribe;
  3. use Illuminate\Support\ServiceProvider;
  4. use Knuckles\Scribe\Commands\GenerateDocumentation;
  5. use Knuckles\Scribe\Commands\MakeStrategy;
  6. use Knuckles\Scribe\Commands\Upgrade;
  7. use Knuckles\Scribe\Matching\RouteMatcher;
  8. use Knuckles\Scribe\Matching\RouteMatcherInterface;
  9. use Knuckles\Scribe\Tools\BladeMarkdownEngine;
  10. use Knuckles\Scribe\Tools\Utils;
  11. class ScribeServiceProvider extends ServiceProvider
  12. {
  13. /**
  14. * Bootstrap the application events.
  15. *
  16. * @return void
  17. */
  18. public function boot()
  19. {
  20. // Register custom Markdown Blade compiler so we can automatically have MD views converted to HTML
  21. $this->app->view->getEngineResolver()
  22. ->register('blademd', fn() => new BladeMarkdownEngine($this->app['blade.compiler']));
  23. $this->app->view->addExtension('md.blade.php', 'blademd');
  24. $this->loadViewsFrom(__DIR__ . '/../resources/views/', 'scribe');
  25. // Publish views in separate, smaller groups for ease of end-user modifications
  26. $viewGroups = [
  27. 'views' => '',
  28. 'examples' => 'partials/example-requests',
  29. 'themes' => 'themes',
  30. 'markdown' => 'markdown',
  31. ];
  32. foreach ($viewGroups as $group => $path) {
  33. $this->publishes([
  34. __DIR__ . "/../resources/views/$path" => $this->app->basePath("resources/views/vendor/scribe/$path"),
  35. ], "scribe-$group");
  36. }
  37. $this->publishes([
  38. __DIR__ . '/../config/scribe.php' => $this->app->configPath('scribe.php'),
  39. ], 'scribe-config');
  40. $this->mergeConfigFrom(__DIR__ . '/../config/scribe.php', 'scribe');
  41. $this->bootRoutes();
  42. if ($this->app->runningInConsole()) {
  43. $this->commands([
  44. GenerateDocumentation::class,
  45. MakeStrategy::class,
  46. Upgrade::class,
  47. ]);
  48. }
  49. // Bind the route matcher implementation
  50. $this->app->bind(RouteMatcherInterface::class, config('scribe.routeMatcher', RouteMatcher::class));
  51. if (!class_exists('Str')) {
  52. // Lumen may not have the aliases set up, and we don't want to have to use the FQN in our blade files.
  53. class_alias(\Illuminate\Support\Str::class, 'Str');
  54. }
  55. }
  56. /**
  57. * Add docs routes for users that want their docs to pass through their Laravel app.
  58. */
  59. protected function bootRoutes()
  60. {
  61. if (
  62. config('scribe.type', 'static') === 'laravel' &&
  63. config('scribe.laravel.add_routes', false)
  64. ) {
  65. $routesPath = Utils::isLumen() ? __DIR__ . '/../routes/lumen.php' : __DIR__ . '/../routes/laravel.php';
  66. $this->loadRoutesFrom($routesPath);
  67. }
  68. }
  69. }