<?php

namespace App\Providers;

use App\Billing\Stripe;
use App\Post;
use App\Tag;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    const STRIPE = 'billing.stripe';
    const STRIPE_KEY = 'services.stripe.key';

    protected $defer = true;

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        view()->composer('layouts.sidebar', function ($view) {
            $archives = Post::archives();
            $tags = Tag::has('posts')->pluck(Tag::LABEL_KEY);

            $view->with(compact(['archives', 'tags']));
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        // Use app->singleton(Stripe::class) instead to obtain a reused instance.
        // Use app->instance(Stripe::class, $stripe) to replace the instance with another object.

        $app2 = $this->app;
        $this->app->bind(Stripe::class, function (Application $app) use($app2) {
            // Yes, there are 3 ways to get to the app in a service closure.
            return new Stripe($this->app['config'][static::STRIPE_KEY]);
        });
        $this->app->alias(Stripe::class, static::STRIPE);
    }
}