master

laravel/framework

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

ForgetCommand.php

TLDR

The ForgetCommand.php file is a class that represents a console command to remove an item from the cache. It extends the Command class and is part of the Illuminate\Cache\Console namespace.

Classes

ForgetCommand

The ForgetCommand class represents a console command to remove an item from the cache. It extends the Command class and is used to remove a specific cache item based on a given key. The class has the following properties:

  • $signature: A string that defines the command signature, including the command name and optional arguments.
  • $description: A string that describes the purpose of the command.
  • $cache: An instance of the Illuminate\Cache\CacheManager class, used to interact with the cache system.

The class has the following methods:

  • __construct(CacheManager $cache): A constructor method that initializes the class by injecting an instance of the CacheManager class.
  • handle(): A method that executes the console command. It retrieves the key and store arguments from the command line, and then uses the CacheManager instance to remove the specified cache item. Finally, it outputs a message indicating that the item has been removed from the cache.
<?php

namespace Illuminate\Cache\Console;

use Illuminate\Cache\CacheManager;
use Illuminate\Console\Command;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'cache:forget')]
class ForgetCommand extends Command
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $signature = 'cache:forget {key : The key to remove} {store? : The store to remove the key from}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Remove an item from the cache';

    /**
     * The cache manager instance.
     *
     * @var \Illuminate\Cache\CacheManager
     */
    protected $cache;

    /**
     * Create a new cache clear command instance.
     *
     * @param  \Illuminate\Cache\CacheManager  $cache
     * @return void
     */
    public function __construct(CacheManager $cache)
    {
        parent::__construct();

        $this->cache = $cache;
    }

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        $this->cache->store($this->argument('store'))->forget(
            $this->argument('key')
        );

        $this->components->info('The ['.$this->argument('key').'] key has been removed from the cache.');
    }
}