CoreFiles by mySites.guru
ExtensionDisableCommand.php216 lines, 6,020 bytes, from Joomla_6.1.2-Stable-Full_Package.zip
Raw History Compare with 6.1.1
md5
8a9fd4088e791e367d199d5db4b8600a
sha1
4e5a15ffe7f347c9884f99187ab4b363b64f4d72
sha256
a48402be9bdcf85c13a5d66e3d9f6150fd102ef2f601e3b1295a946b3169276d
<?php

/**
 * Joomla! Content Management System
 *
 * @copyright  (C) 2025 Open Source Matters, Inc. <https://www.joomla.org>
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 */

namespace Joomla\CMS\Console;

use Joomla\CMS\Table\Extension;
use Joomla\Component\Templates\Administrator\Table\StyleTable;
use Joomla\Console\Command\AbstractCommand;
use Joomla\Database\DatabaseAwareTrait;
use Joomla\Database\DatabaseInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
 * Console command to disable extensions
 *
 * @since  6.1.0
 */
class ExtensionDisableCommand extends AbstractCommand
{
    use DatabaseAwareTrait;

    /**
     * The default command name
     *
     * @var    string
     * @since  6.1.0
     */
    protected static $defaultName = 'extension:disable';

    /**
     * @var InputInterface
     * @since 6.1.0
     */
    private $cliInput;

    /**
     * @var SymfonyStyle
     * @since 6.1.0
     */
    private $ioStyle;

    /**
     * Exit Code for extensions already enabled\disabled
     * @since 6.1.0
     */
    public const DISABLE_NOCHANGE = 3;

    /**
     * Exit Code for extensions enable\disable failure
     * @since 6.1.0
     */
    public const DISABLE_FAILED = 1;

    /**
     * Exit Code for disable parent template with child failure
     * @since 6.1.0
     */
    public const DISABLE_WITHCHILD_NOT_PERMITTED = 5;

    /**
     * Exit Code for disable home template failure
     * @since 6.1.0
     */
    public const DISABLE_HOME_NOT_PERMITTED = 6;

    /**
     * Exit Code for extensions protected enable\disable failure
     * @since 6.1.0
     */
    public const DISABLE_PROTECTED = 4;

    /**
     * Exit Code for extensions not found
     * @since 6.1.0
     */
    public const DISABLE_NOT_FOUND = 2;

    /**
     * Exit Code for extensions enable\disable success
     * @since 6.1.0
     */
    public const DISABLE_SUCCESSFUL = 0;

    /**
     * Command constructor.
     *
     * @param   DatabaseInterface  $db  The database
     *
     * @since   6.1.0
     */
    public function __construct(DatabaseInterface $db)
    {
        parent::__construct();

        $this->setDatabase($db);
    }

    /**
     * Configures the IO
     *
     * @param   InputInterface   $input   Console Input
     * @param   OutputInterface  $output  Console Output
     *
     * @return void
     *
     * @since 6.1.0
     *
     */
    private function configureIO(InputInterface $input, OutputInterface $output): void
    {
        $this->cliInput = $input;
        $this->ioStyle  = new SymfonyStyle($input, $output);
    }

    /**
     * Initialise the command.
     *
     * @return  void
     *
     * @since   6.1.0
     */
    protected function configure(): void
    {
        $this->addArgument(
            'extensionId',
            InputArgument::REQUIRED,
            'ID of extension to be disabled (run extension:list command to check)'
        );

        $help = "<info>%command.name%</info> is used to disable an extension.
		\nThe command requires one argument, the ID of the extension to disable.
		\nYou may find this ID by running the <info>extension:list</info> command.
		\nUsage: <info>php %command.full_name% <extension_id></info>";

        $this->setDescription('Disable an extension');
        $this->setHelp($help);
    }

    /**
     * Internal function to execute the command.
     *
     * @param   InputInterface   $input   The input to inject into the command.
     * @param   OutputInterface  $output  The output to inject into the command.
     *
     * @return  integer  The command exit code
     *
     * @since   6.1.0
     */
    protected function doExecute(InputInterface $input, OutputInterface $output): int
    {
        $this->configureIO($input, $output);

        $extensionId = $this->cliInput->getArgument('extensionId');
        $this->ioStyle->title('Disable Extension');

        // Get a table object for the extension type
        $table = new Extension($this->getDatabase());

        if ((int) $extensionId === 0 || !$table->load($extensionId)) {
            $this->ioStyle->error("Extension with ID of $extensionId not found.");

            return self::DISABLE_NOT_FOUND;
        }

        if ($table->type === 'template') {
            $style = new StyleTable($this->getDatabase());

            if ($style->load(['template' => $table->element, 'client_id' => $table->client_id, 'home' => 1])) {
                $this->ioStyle->note("Template with ID of $extensionId $table->element is home.");
                return self::DISABLE_HOME_NOT_PERMITTED;
            }

            // Parent template cannot be disabled if there are children
            if ($style->load(['parent' => $table->element, 'client_id' => $table->client_id])) {
                $this->ioStyle->note("Template with ID of $extensionId $table->element have child.");
                return self::DISABLE_WITHCHILD_NOT_PERMITTED;
            }
        }

        $type = ucfirst($table->type);

        if ($table->protected === 1) {
            $this->ioStyle->error("$type with ID of $extensionId $table->name is protectd.");
            return self::DISABLE_PROTECTED;
        }

        if ($table->enabled === 0) {
            $this->ioStyle->warning("$type with ID of $extensionId $table->name already is disabled.");
            return self::DISABLE_NOCHANGE;
        }

        $table->enabled = 0;

        if (!$table->store()) {
            $this->ioStyle->error("$type with ID of $extensionId $table->name not disabled.");
            return self::DISABLE_FAILED;
        }

        $this->ioStyle->success("$type with ID of $extensionId $table->name disabled.");

        return self::DISABLE_SUCCESSFUL;
    }
}

The same content is in 29 releases, Joomla 6.1.0-alpha3 to 6.2.1-rc1. See them all, or every version of this file.