master

laravel/framework

Last updated at: 29/12/2023 09:23

MailServiceProvider.php

TLDR

The MailServiceProvider.php file is part of the Illuminate\Mail namespace in the Demo Projects project. It is a service provider for the Laravel Mail component. This file provides methods to register the Illuminate mailer and the Markdown renderer, as well as the services they provide.

Methods

register

This method is responsible for registering the service provider. It calls the registerIlluminateMailer and registerMarkdownRenderer methods.

registerIlluminateMailer

This method registers the Illuminate mailer instance. It creates a singleton instance of the MailManager class and binds it to the 'mail.manager' key in the application container. It also binds the 'mailer' key to the mailer instance returned by the MailManager.

registerMarkdownRenderer

This method registers the Markdown renderer instance. If the application is running in the console, it publishes the Markdown views to the application's resource views directory. It then creates a singleton instance of the Markdown class and binds it to the application container using the 'view' service and the configuration options for the Markdown theme and paths.

provides

This method returns an array of services provided by the provider. The services include 'mail.manager', 'mailer', and the Markdown class.

Classes

There are no classes defined in this file.

<?php

namespace Illuminate\Mail;

use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;

class MailServiceProvider extends ServiceProvider implements DeferrableProvider
{
    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->registerIlluminateMailer();
        $this->registerMarkdownRenderer();
    }

    /**
     * Register the Illuminate mailer instance.
     *
     * @return void
     */
    protected function registerIlluminateMailer()
    {
        $this->app->singleton('mail.manager', function ($app) {
            return new MailManager($app);
        });

        $this->app->bind('mailer', function ($app) {
            return $app->make('mail.manager')->mailer();
        });
    }

    /**
     * Register the Markdown renderer instance.
     *
     * @return void
     */
    protected function registerMarkdownRenderer()
    {
        if ($this->app->runningInConsole()) {
            $this->publishes([
                __DIR__.'/resources/views' => $this->app->resourcePath('views/vendor/mail'),
            ], 'laravel-mail');
        }

        $this->app->singleton(Markdown::class, function ($app) {
            $config = $app->make('config');

            return new Markdown($app->make('view'), [
                'theme' => $config->get('mail.markdown.theme', 'default'),
                'paths' => $config->get('mail.markdown.paths', []),
            ]);
        });
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return [
            'mail.manager',
            'mailer',
            Markdown::class,
        ];
    }
}