ScribeServiceProvider.php 3.7 KB

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