Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't load custom extension class #772

Closed
ghost opened this issue Nov 25, 2014 · 17 comments
Closed

Can't load custom extension class #772

ghost opened this issue Nov 25, 2014 · 17 comments

Comments

@ghost
Copy link

ghost commented Nov 25, 2014

It appears that you can no longer load customer extension classes from XML files, like this:

    <source_model>Inchoo\Stripe\Model\Source\Cctype</source_model>

Error is:
Class Inchoo\Stripe\Model\Source\Cctype does not exist

For example, In a custom extension, etc/adminhtml/system.xml there are a few lines like this:

            <field id="cctypes" translate="label" type="multiselect" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="0">
                <label>Credit Card Types</label>
                <source_model>Inchoo\Stripe\Model\Source\Cctype</source_model>
            </field>

(File exists, in /app/code/Inchoo/Stripe/Model/Source/Cctype.php, namespace and class name are OK)

See
https://github.com/ivanweiler/Inchoo_Stripe

It used to work in an earlier Magento2 build... what changed?

@antonkril
Copy link
Contributor

The error message proves that magento tries to load your class, but it can't find it. So System config works correctly and problem is with class path. Did you try running composer install?

@ghost
Copy link
Author

ghost commented Nov 25, 2014

I tried re-running composer install in the root. But no help. Do you need to preload custom extensions somehow? Or define your models somewhere? It definitely worked in an earlier version of Magento2, I can't see what changed.

/app/code/Inchoo/Stripe/Model/Source/Cctype.php

namespace Inchoo\Stripe\Model\Source;
class Cctype extends \Magento\Payment\Model\Source\Cctype
{
    // etc
}

https://github.com/Inchoo/magento2-Inchoo_Stripe

@ghost
Copy link
Author

ghost commented Nov 26, 2014

It does work if I move the module to the app/code/Mage location, rename everything and call

<source_model>Mage\Stripe\Model\Source\Cctype</source_model>

It's as if custom extension classes can't be found.

@buskamuza
Copy link
Contributor

@morningtime , you're right in your investigation.
Looks like a bug in new autoloader implementation - it doesn't include app/code in include path (comparing to previous implementation). I'll pass it to the team working on autoloder to investigate it deeper.
Thanks for the report.

@dsikkema
Copy link

@morningtime we are working on a fix that will require no code changes on your part. A solution in the meantime, and also for a little performance gain, you can add your module to composer.json's autoload section. In your example, you would go to composer.json -> autoload -> psr-4, and add

"Inchoo" : "app/code/Inchoo/"

According to the composer format:
https://getcomposer.org/doc/01-basic-usage.md#autoloading

EDIT: It is recommended to add this information to the composer.json file when using 3rd party modules.

@antonmakarenko
Copy link

@morningtime

I think an ideal solution is to adapt your extension to Magento 2 + Composer. If your package had a composer.json file with proper autoload section, there would be no need for workaround.

But in order for such a package to be recognized by Magento 2 installer and properly moved to app/code directory, it needs to define type "magento2-module". See examples at http://packages.magento.com/#!/magento/module-

@ghost
Copy link
Author

ghost commented Nov 26, 2014

Ok, if add the line to the root composer.json, it works:

"autoload": {
    "psr-4": {
        "Magento\\Framework\\": "lib/internal/Magento/Framework/",
        "Inchoo\\": "app/code/Inchoo/"
    }
},

But when I try a composer.json file in the app/code/Inchoo/Stripe/composer.json location, it doesn't work. Here's what I tried:

{
    "name": "inchoo/module-stripe",
    "description": "N/A",
    "require": {
        "php": "~5.4.11|~5.5.0",
        "magento/module-sales": "0.1.0-alpha105",
        "magento/module-payment": "0.1.0-alpha105",
        "magento/framework": "0.1.0-alpha105",
        "magento/magento-composer-installer": "*"
    },
    "autoload": {
        "psr-4": {
            "Inchoo\\": "../../app/code/Inchoo/"
        }
    },
    "type": "magento2-module",
    "version": "0.1.0-alpha105",
    "extra": {
        "map": [
            ["*", "Inchoo/Stripe"]
        ]
    }
}

@antonmakarenko
Copy link

With the Composer approach, it works in different way. You don't have the source code in app/code in the first place. Instead, you would declare in your root composer.json in "require" section something like this:

"require": {
    "inchoo/stripe": "*",
    ...
}

Then if you run composer install or composer update from root of your project, it would download the package and automatically integrate autoload.

But it takes a lot of work to do -- this extension needs to be properly packaged and registered on a packaging server (for example, packagist.org)

@buskamuza
Copy link
Contributor

@morningtime , please, use solution with root composer.json until we solve this issue.
Unfortunately, second solution is not supported now by Composer Installer.

@ghost
Copy link
Author

ghost commented Nov 27, 2014

Thank you very much. I think we need some way to add third party extensions flexibly.

@orlangur
Copy link
Contributor

To repair the old-school include_path based autoloading approach, corresponding section could be added to composer.json:

"include-path": [
    "app/code"
]

It is a deprecated feature of a Composer which is not going to be removed though.

But in order for such a package to be recognized by Magento 2 installer and properly moved to app/code directory, it needs to define type "magento2-module".

Instead of this modules' source code could reside in vendor folder and only some configuration files copied to app/code.

But it takes a lot of work to do -- this extension needs to be properly packaged and registered on a packaging server (for example, packagist.org)

Not really, you can simply load package from a git repository.

we need some way to add third party extensions flexibly

There could be more than one way to add extension, so would be nice to have some appropriate workflow described covering a couple of use cases:

  • develop new extension
  • add existing extension

@ghost
Copy link
Author

ghost commented Dec 15, 2014

Well I'm confused.

How are commercial extension going to be added, that won't have a public composer package?

How can magento 2.x users create some custom code easily, without creating a github account and a composer package? In Magento 1.x you could put some code in app/code/local to create custom extensions.

I find it a bit cumbersome to put all custom/commercial code in the /vendor location with "some configuration files" under app/code. Sounds possibly confusing. What's the point of app/code if it can only host 'Mage' extensions, not community and local contributions?

The include_path seems most practical fix, but this will be deprecated...?

In Magento 1.x you could also upload extension "packages" via the /downloader system. How will this work then in Magento 2.x?

@otoolec
Copy link
Contributor

otoolec commented Dec 15, 2014

If you put PSR-0 compliant code in app/code then it should now get picked up automatically. This is currently handled by the following code:

$autoloader->addPsr0('', [$modulesDir, $generationDir]);

which is equivalent to having the following in the root composer.json file:

    "psr-0": {
        "": ["app/code/", "var/generation/"]
    }

@verklov
Copy link
Contributor

verklov commented Dec 15, 2014

Thank you for the clue, @otoolec! We are now closing this ticket as resolved. The code with the fix must be available in the repository.

@verklov verklov closed this as completed Dec 15, 2014
@ghost
Copy link
Author

ghost commented Dec 18, 2014

Thanks so much.

@keyurshah070
Copy link
Contributor

Is there any updates on this issue ?

@mazhalai
Copy link
Contributor

@keyurshah070 please avoid posting on closed issues. Start a new one.

fe-lix- pushed a commit to fe-lix-/magento2 that referenced this issue Apr 6, 2018
MSI: 532: Adapt AdaptAssignStatusToProductPlugin to complex products
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants