1

TLDR - Is there a reason I can't use preference for classes in Magento\Setup namespace, specifically during the setup:upgrade processing, OR is there something I am missing with the following preferences? They aren't being implemented.

I am trying to create a temporary module to override a couple classes under the Magento\Setup namespace. This reason behind this is because I am waiting for this PR to be deployed (https://github.com/magento/magento2/pull/18075) and in the meantime I need this functionality to work. So I'm creating a local module to implement this to avoid hacking core.

I have added two simple preferences same as I would in any other situation like this.

app/code/Company/Module/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Setup\Model\Installer" type="Company\Module\Model\Installer" />
    <preference for="Magento\Setup\Validator\DbValidator" type="Company\Module\Validator\DbValidator" />
</config>

I have created each of those classes and added the function I want to override with a simple logging in the first line. However neither of them are taking effect. The classes/functions are being called as part of the bin/magento setup:upgrade command. Below are shortened versions of my classes.

app/code/Company/Module/Validator/DbValidator.php

<?php
declare(strict_types=1);
namespace Company\Module\Validator;

class DbValidator extends \Magento\Setup\Validator\DbValidator
{
    //...construct and props declared here...

    public function checkDatabaseConnection($dbName, $dbHost, $dbUser, $dbPass = '')
    {
        $this->log->log("====Company::checkDatabaseConnection====");
        //...rest of the code to implement...
    }
}

app/code/Company/Module/Model/Installer.php

<?php
declare(strict_types=1);
namespace Company\Module\Model;

class Installer extends \Magento\Setup\Model\Installer
{
    //...construct and props declared here...

    private function assertDbAccessible()
    {
        $this->log->log("======Company::assertDbAccessible=====");
        //...
    }
}

I'm wondering if they are not taking effect because perhaps I'm trying to override Magento\Setup classes during the setup:upgrade functionality. Maybe there's something in the depths of this functionality that I'm missing that would cause this not to work.

UPDATE

I just found that the Magento\Setup component is actually being registered as a "setup" component instead of a "module" (found in setup/src/Magento/Setup/registration.php). Due to this it is bootstrapped differently than modules, and as such the typical Plugin (interceptor) and Preference mechanisms will not work. So now I'm stuck trying to figure out how I can extend/modify/override any functionality from setup without modifying the core files. Is there perhaps a unique way to use dependency injection or otherwise modify this "setup" component type with Magento?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.