1

I have a (Not Open-Source) custom Magento 2.2x module that I'm working on, that has a dependency on an external composer package (bugsnag/bugsnag)

Here's how my project is currently structured:

Current Project Structure

Project Structure

File - module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Gsd_Test" setup_version="0.1.0">
    </module>
</config>

File - composer.json

{
    "name": "gsd/test",
    "description": "",
    "require": {
        "php": "7.*",
        "bugsnag/bugsnag":"^3.0"
    },
    "suggest": {

    },
    "type": "magento2-module",
    "version": "0.1.0",
    "license": [

    ],
    "autoload": {
        "files": [
            "registration.php"
        ],
        "psr-4": {
            "Gsd\\Test\\": "",
            "Bugsnag\\" : "vendor/bugsnag/bugsnag/src/"
        }
    },
    "extra": {
        "map": [
            [
                "*",
                "Gsd/Test"
            ]
        ]
    }
}

Controller - Gsd\Test\Controller\Adminhtml\Index\Index.php

<?php

namespace Gsd\Test\Controller\Adminhtml\Index;

class Index extends \Magento\Backend\App\Action
{
    protected $_publicActions = ['index'];

    public function execute()
    {
        echo "Hello World";
        $bugsnag = \Bugsnag\Client::make('XXXXXX');
        \Bugsnag\Handler::register($bugsnag);

        $bugsnag->notifyException("Testing ");
    }

}

Registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Gsd_Test',
    __DIR__
);

BUT, attempting to access that Controller (http://m22.localhost.net/admin/gsdtest/index/index) results in the following error:

Application Error

Looks like, for whatever reason, the Autoloader ignores the content within the app/code/Gsd/Test/vendor/bugsnag/bugsnag/src folder at run-time.

(Ironically, PhpStorm doesn't report any error during development per se)

Now, if I add "bugsnag/bugsnag" as a composer dependency at the MAGENTO_HOME folder level, i.e, if I run the following commands:

cd $MAGENTO_HOME
composer require bugsnag/bugsnag 3.*
composer update
composer dump-autoload

then my controller works just fine.

So, my questions are:

  • I recall this used to work on Magento 2.1x (i.e. having the composer dependency inside the module specific composer.json) about 2 - 3 months back. But, it no longer works on Magento 2.1x or Magento 2.2x ... What changed??

  • Is there a way to specify module specific composer.json dependencies? If not - how can one "ship" a custom-module - that has dependencies on 3rd party libraries - so that it can be deployed directly into the app/code folder? ( Requiring customers deploy the module using composer may not work for all businesses/situations )

1 Answer 1

1

If you want require dependencies with composer then your module must be installed as a composer module. At the moment it is not, just files in app/code so the solution is to develop core and public it on your packagist then write magento wrapper in app/code or add all dependencies in composer.json in root so you will be able to use them in your module in app/code. For obvious reasons the first approach is much better and recommended.

2
  • So - outside of deploying the module as a public module on packagist/github - there isn't a way to ship out a "fully-self-contained" 3rd party module in Magento 2x ?? Commented Nov 2, 2017 at 20:42
  • Not even a github, composer is a core dependencies manager for magento and "github way" or "copy/past code m1 style" will not work. Therefore your answer is correct. Unless you will do some "no-magento" way modules with private composer scope and so on but it does not make any sense. Commented Nov 2, 2017 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.