master

laravel/framework

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

LoadEnvironmentVariables.php

TLDR

This file, LoadEnvironmentVariables.php, is part of the Illuminate\Foundation\Bootstrap namespace in the Demo Projects project. It contains a single class, LoadEnvironmentVariables, which is responsible for bootstrapping the application by loading environment variables.

Classes

LoadEnvironmentVariables

This class is responsible for bootstrapping the application by loading environment variables. It contains the following methods:

bootstrap

This method is used to bootstrap the application. It checks if the application's configuration is cached, and if not, it calls the checkForSpecificEnvironmentFile method and attempts to load the environment variables using the createDotenv method. If there is an invalid file exception, it calls the writeErrorAndDie method.

checkForSpecificEnvironmentFile

This method is used to detect if a custom environment file matching the APP_ENV exists. If the application is running in console and the --env option is provided, it sets the environment file path accordingly. Otherwise, it checks if the APP_ENV is set and sets the environment file path based on that.

setEnvironmentFilePath

This method is used to set the environment file path. It checks if the specified file exists and loads it in the application if it does.

createDotenv

This method is used to create a Dotenv instance. It takes the repository, environment path, and environment file from the application and returns a new instance of the Dotenv class.

writeErrorAndDie

This method is used to write the error information to the screen and exit the application. It takes an InvalidFileException as an argument and outputs the error message to the console.

<?php

namespace Illuminate\Foundation\Bootstrap;

use Dotenv\Dotenv;
use Dotenv\Exception\InvalidFileException;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Env;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;

class LoadEnvironmentVariables
{
    /**
     * Bootstrap the given application.
     *
     * @param  \Illuminate\Contracts\Foundation\Application  $app
     * @return void
     */
    public function bootstrap(Application $app)
    {
        if ($app->configurationIsCached()) {
            return;
        }

        $this->checkForSpecificEnvironmentFile($app);

        try {
            $this->createDotenv($app)->safeLoad();
        } catch (InvalidFileException $e) {
            $this->writeErrorAndDie($e);
        }
    }

    /**
     * Detect if a custom environment file matching the APP_ENV exists.
     *
     * @param  \Illuminate\Contracts\Foundation\Application  $app
     * @return void
     */
    protected function checkForSpecificEnvironmentFile($app)
    {
        if ($app->runningInConsole() &&
            ($input = new ArgvInput)->hasParameterOption('--env') &&
            $this->setEnvironmentFilePath($app, $app->environmentFile().'.'.$input->getParameterOption('--env'))) {
            return;
        }

        $environment = Env::get('APP_ENV');

        if (! $environment) {
            return;
        }

        $this->setEnvironmentFilePath(
            $app, $app->environmentFile().'.'.$environment
        );
    }

    /**
     * Load a custom environment file.
     *
     * @param  \Illuminate\Contracts\Foundation\Application  $app
     * @param  string  $file
     * @return bool
     */
    protected function setEnvironmentFilePath($app, $file)
    {
        if (is_file($app->environmentPath().'/'.$file)) {
            $app->loadEnvironmentFrom($file);

            return true;
        }

        return false;
    }

    /**
     * Create a Dotenv instance.
     *
     * @param  \Illuminate\Contracts\Foundation\Application  $app
     * @return \Dotenv\Dotenv
     */
    protected function createDotenv($app)
    {
        return Dotenv::create(
            Env::getRepository(),
            $app->environmentPath(),
            $app->environmentFile()
        );
    }

    /**
     * Write the error information to the screen and exit.
     *
     * @param  \Dotenv\Exception\InvalidFileException  $e
     * @return void
     */
    protected function writeErrorAndDie(InvalidFileException $e)
    {
        $output = (new ConsoleOutput)->getErrorOutput();

        $output->writeln('The environment file is invalid!');
        $output->writeln($e->getMessage());

        http_response_code(500);

        exit(1);
    }
}