Skip to content

Using classes from MagentoSetup namespace in custom module

This is for Magento 2.4.13-p3, on PHP 7.4.

I’m working on a custom console command class. It uses another custom class named Analyzer in its constructor. I’m trying to inject a MagentoSetupModelInstallerFactory dependency into the Analyzer class constructor, and it doesn’t work:

<?php

namespace UtpcExplainerModel;

use MagentoSetupModelInstallerFactory;

class Analyzer {

    protected InstallerFactory $installerFactory;

    public function __construct(
        InstallerFactory $installerFactory
    ) {
        $this->installerFactory = $installerFactory;
    }

    // etc....

With that constructor, when you run bin/magento it throws this:

Fatal error: Uncaught Error: Cannot instantiate interface LaminasServiceManagerServiceLocatorInterface in /var/www/html/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:50
Stack trace:
#0 /var/www/html/vendor/magento/framework/ObjectManager/ObjectManager.php(70): MagentoFrameworkObjectManagerFactoryDynamicDeveloper->create('Laminas\Service...')
#1 /var/www/html/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(170): MagentoFrameworkObjectManagerObjectManager->get('Laminas\Service...')
#2 /var/www/html/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(276): MagentoFrameworkObjectManagerFactoryAbstractFactory->resolveArgument(Array, 'Laminas\Service...', NULL, 'serviceLocator', 'Magento\Setup\M...')
#3 /var/www/html/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(239): MagentoFrameworkObjectManagerFactoryAbstractFactory->getResolvedArgument('Magento\Setup\M...', Array, Array)
#4 /var/www/html/vendor/magento/framework/ObjectMana in /var/www/html/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php on line 50

It seems to do this if you try adding any class in MagentoSetup... as a dependency. It also happens if I try creating the InstallerFactory class using object manager directly instead of adding it as a dependency. Anyone know why?