master

laravel/framework

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

FileBasedMaintenanceMode.php

TLDR

The FileBasedMaintenanceMode class in the Illuminate\Foundation namespace provides methods for activating, deactivating, and checking the status of maintenance mode in an application. It uses a file located at storage_path('framework/down') to store the maintenance mode data.

Methods

activate

The activate method takes an array $payload as input and writes the JSON-encoded $payload to the maintenance mode file.

deactivate

The deactivate method removes the maintenance mode file if it exists.

active

The active method checks if the maintenance mode file exists and returns a boolean value indicating whether the application is currently down for maintenance.

data

The data method reads the contents of the maintenance mode file and returns the decoded JSON data as an array.

path

The path method returns the path to the maintenance mode file, which is storage_path('framework/down').

<?php

namespace Illuminate\Foundation;

use Illuminate\Contracts\Foundation\MaintenanceMode as MaintenanceModeContract;

class FileBasedMaintenanceMode implements MaintenanceModeContract
{
    /**
     * Take the application down for maintenance.
     *
     * @param  array  $payload
     * @return void
     */
    public function activate(array $payload): void
    {
        file_put_contents(
            $this->path(),
            json_encode($payload, JSON_PRETTY_PRINT)
        );
    }

    /**
     * Take the application out of maintenance.
     *
     * @return void
     */
    public function deactivate(): void
    {
        if ($this->active()) {
            unlink($this->path());
        }
    }

    /**
     * Determine if the application is currently down for maintenance.
     *
     * @return bool
     */
    public function active(): bool
    {
        return file_exists($this->path());
    }

    /**
     * Get the data array which was provided when the application was placed into maintenance.
     *
     * @return array
     */
    public function data(): array
    {
        return json_decode(file_get_contents($this->path()), true);
    }

    /**
     * Get the path where the file is stored that signals that the application is down for maintenance.
     *
     * @return string
     */
    protected function path(): string
    {
        return storage_path('framework/down');
    }
}