AppServiceProvider.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace App\Providers;
  3. use App\Billing\Stripe;
  4. use App\Post;
  5. use App\Tag;
  6. use Illuminate\Foundation\Application;
  7. use Illuminate\Support\ServiceProvider;
  8. class AppServiceProvider extends ServiceProvider
  9. {
  10. const STRIPE = 'billing.stripe';
  11. const STRIPE_KEY = 'services.stripe.key';
  12. protected $defer = true;
  13. /**
  14. * Bootstrap any application services.
  15. *
  16. * @return void
  17. */
  18. public function boot()
  19. {
  20. view()->composer('layouts.sidebar', function ($view) {
  21. $archives = Post::archives();
  22. $tags = Tag::has('posts')->pluck(Tag::LABEL_KEY);
  23. $view->with(compact(['archives', 'tags']));
  24. });
  25. }
  26. /**
  27. * Register any application services.
  28. *
  29. * @return void
  30. */
  31. public function register()
  32. {
  33. // Use app->singleton(Stripe::class) instead to obtain a reused instance.
  34. // Use app->instance(Stripe::class, $stripe) to replace the instance with another object.
  35. $app2 = $this->app;
  36. $this->app->bind(Stripe::class, function (Application $app) use($app2) {
  37. // Yes, there are 3 ways to get to the app in a service closure.
  38. return new Stripe($this->app['config'][static::STRIPE_KEY]);
  39. });
  40. $this->app->alias(Stripe::class, static::STRIPE);
  41. }
  42. }