master

laravel/framework

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

ConfigPublishCommand.php

TLDR

This file ConfigPublishCommand.php is a part of the Demo Projects project and it provides a command for publishing configuration files in Laravel applications.

Methods

handle

This method executes the console command. It retrieves the base configuration files and then determines whether to publish all configuration files or a specific one based on the command arguments and options.

publish

This method publishes a given configuration file to a specified destination. It checks if the destination file already exists and whether the --force option is provided to overwrite it. If the file doesn't exist or the --force option is used, it copies the file to the destination and displays a success message.

getBaseConfigurationFiles

This method returns an array containing the base configuration files. It uses the Finder component to locate all PHP files in the config directory and stores their paths in the array, using the file names (without the .php extension) as keys.

Classes

ConfigPublishCommand

This class extends the Command class from the Illuminate\Console namespace. It represents the console command for publishing configuration files. It has properties for the command's signature, description, and method implementations for handling the command and publishing files.

<?php

namespace Illuminate\Foundation\Console;

use Illuminate\Console\Command;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Finder\Finder;

use function Laravel\Prompts\select;

#[AsCommand(name: 'config:publish')]
class ConfigPublishCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'config:publish
                    {name? : The name of the configuration file to publish}
                    {--all : Publish all configuration files}
                    {--force : Overwrite any existing configuration files}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Publish configuration files to your application';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $config = $this->getBaseConfigurationFiles();

        if (is_null($this->argument('name')) && $this->option('all')) {
            foreach ($config as $key => $file) {
                $this->publish($key, $file, $this->laravel->configPath().'/'.$key.'.php');
            }

            return;
        }

        $name = (string) (is_null($this->argument('name')) ? select(
            label: 'Which configuration file would you like to publish?',
            options: collect($config)->map(function (string $path) {
                return basename($path, '.php');
            }),
        ) : $this->argument('name'));

        if (! is_null($name) && ! isset($config[$name])) {
            $this->components->error('Unrecognized configuration file.');

            return 1;
        }

        $this->publish($name, $config[$name], $this->laravel->configPath().'/'.$name.'.php');
    }

    /**
     * Publish the given file to the given destination.
     *
     * @param  string  $name
     * @param  string  $file
     * @param  string  $destination
     * @return void
     */
    protected function publish(string $name, string $file, string $destination)
    {
        if (file_exists($destination) && ! $this->option('force')) {
            $this->components->error("The '{$name}' configuration file already exists.");

            return;
        }

        copy($file, $destination);

        $this->components->info("Published '{$name}' configuration file.");
    }

    /**
     * Get an array containing the base configuration files.
     *
     * @return array
     */
    protected function getBaseConfigurationFiles()
    {
        $config = [];

        foreach (Finder::create()->files()->name('*.php')->in(__DIR__.'/../../../../config') as $file) {
            $config[basename($file->getRealPath(), '.php')] = $file->getRealPath();
        }

        return collect($config)->sortKeys()->all();
    }
}