master

laravel/framework

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

ClosureCommand.php

TLDR

This file defines the ClosureCommand class, which extends the Command class from the Laravel framework. It allows users to create console commands with a closure callback as the command logic.

Methods

__construct($signature, Closure $callback)

The constructor of the ClosureCommand class. It accepts a string $signature which represents the command signature, and a closure $callback which is the command logic.

execute(InputInterface $input, OutputInterface $output): int

This method is responsible for executing the console command. It receives an instance of InputInterface and OutputInterface as its parameters. It merges the command arguments and options in the input, then iterates over the parameters of the provided closure to check if the inputs match the parameter names. It then calls the closure with the provided parameters using the call() method of the Laravel application. Returns an integer representing the exit code of the command.

purpose($description): ClosureCommand

This method sets the description for the command and returns the instance of the ClosureCommand class. It is an alias for the describe() method.

describe($description): ClosureCommand

This method sets the description for the command and returns the instance of the ClosureCommand class.

schedule($parameters = []): \Illuminate\Console\Scheduling\Event

This method creates a new scheduled event for the command. It receives an optional array of parameters and returns an instance of the Event class from the Laravel framework.

__call($method, $parameters): mixed

This method proxies calls to a new scheduled event. It dynamically forwards method calls to the schedule() method and returns the result.

Classes

None

<?php

namespace Illuminate\Foundation\Console;

use Closure;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Schedule;
use Illuminate\Support\Traits\ForwardsCalls;
use ReflectionFunction;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * @mixin \Illuminate\Console\Scheduling\Event
 */
class ClosureCommand extends Command
{
    use ForwardsCalls;

    /**
     * The command callback.
     *
     * @var \Closure
     */
    protected $callback;

    /**
     * Create a new command instance.
     *
     * @param  string  $signature
     * @param  \Closure  $callback
     * @return void
     */
    public function __construct($signature, Closure $callback)
    {
        $this->callback = $callback;
        $this->signature = $signature;

        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @param  \Symfony\Component\Console\Input\InputInterface  $input
     * @param  \Symfony\Component\Console\Output\OutputInterface  $output
     * @return int
     */
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $inputs = array_merge($input->getArguments(), $input->getOptions());

        $parameters = [];

        foreach ((new ReflectionFunction($this->callback))->getParameters() as $parameter) {
            if (isset($inputs[$parameter->getName()])) {
                $parameters[$parameter->getName()] = $inputs[$parameter->getName()];
            }
        }

        return (int) $this->laravel->call(
            $this->callback->bindTo($this, $this), $parameters
        );
    }

    /**
     * Set the description for the command.
     *
     * @param  string  $description
     * @return $this
     */
    public function purpose($description)
    {
        return $this->describe($description);
    }

    /**
     * Set the description for the command.
     *
     * @param  string  $description
     * @return $this
     */
    public function describe($description)
    {
        $this->setDescription($description);

        return $this;
    }

    /**
     * Create a new scheduled event for the command.
     *
     * @param  array  $parameters
     * @return \Illuminate\Console\Scheduling\Event
     */
    public function schedule($parameters = [])
    {
        return Schedule::command($this->name, $parameters);
    }

    /**
     * Dynamically proxy calls to a new scheduled event.
     *
     * @param  string  $method
     * @param  array  $parameters
     * @return mixed
     *
     * @throws \BadMethodCallException
     */
    public function __call($method, $parameters)
    {
        return $this->forwardCallTo($this->schedule(), $method, $parameters);
    }
}