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

Unable to load a vendor library for custom module #1225

Closed
arkadiyasuratov opened this issue Feb 6, 2019 · 7 comments
Closed

Unable to load a vendor library for custom module #1225

arkadiyasuratov opened this issue Feb 6, 2019 · 7 comments
Assignees

Comments

@arkadiyasuratov
Copy link
Contributor

I've installed ramsey/uuid in my custom module path. When I try to use the package

use Ramsey\Uuid\Uuid;

I get an error Espo.ERROR: Uncaught Exception Error: "Class 'Ramsey\Uuid\Uuid' not found"

Here's my autoload.json file

{
    "Ramsey\\Uuid\\": "application/Espo/Modules/MyModule/vendor/ramsey/uuid/src"
}

What am I doing wrong?

@bandtank
Copy link
Contributor

bandtank commented Feb 6, 2019

Did you clear the cache?

@yurikuzn
Copy link
Contributor

yurikuzn commented Feb 6, 2019

Maybe you should try:

"RamseyUuid": "application/Espo/Modules/MyModule/vendor/ramsey/uuid/src"

I'm surprised that we have the ability to define custom vendor in modules. I forgot. Cool.

@arkadiyasuratov
Copy link
Contributor Author

@bandtank hey, yes I did. I've run it through xDebug and it seems the method initAutoloads() is actually loading the file. That is why I am thinking maybe I incorrectly referencing my package.

@yurikuzn thanks, I will try that and let you know.

@arkadiyasuratov
Copy link
Contributor Author

@yurikuzn unfortunately no luck. I dug a little bit deeper and I think I know what is the problem.

This:

{
    "Ramsey\\Uuid\\": "application/Espo/Modules/MyModule/vendor/ramsey/uuid/src"
}

seems to be a correct way to load a package.

The reason it doesn't work is because ramsey/uuid adhere to PSR-4 standard while ClassLoader() is using add() method instead of addPsr4().

This causes the Composer to look for files not in directory

vendor/ramsey/uuid/src

but in

vendor/ramsey/uuid/src/Ramsey/Uuid

Posible solutions

  1. Manually including packages using PSR-0 style autoloading
vendor/ramsey/uuid/Ramsey/Uuid
  1. Changing ClassLoader() method from add() to addPsr4(). But this would be a temporary solution.

Using Composer's target-dir

I have also tried this method of installing the package

{
    "repositories": [
        {
            "type": "package",
            "package": {
                "name": "ramsey/uuid",
                "version": "dev-3.x",
                "source": {
                    "type": "git",
                    "url": "https://github.com/ramsey/uuid.git",
                    "reference": "3.x"
                },
                "autoload": {
                    "psr-0": {
                        "Ramsey\\Uuid\\": ""
                    }
                },
                "target-dir": "Ramsey/Uuid"
            }
        }
    ],
    "require": {
        "ramsey/uuid": "dev-3.x"
    }
}

But it did get me nowhere. If I understood the Composer's docs correctly, this method would only work with packages with file structure similar tu symfony/yaml. ramsey/uuid is mapping namespace to src/ directory, which causes the error

Got error 'PHP message: PHP Fatal error:  Interface 'Ramsey\\Uuid\\UuidInterface' not found in /home/vagrant/domains/espocrm/application/Espo/Modules/MyModule/vendor/ramsey/uuid/Ramsey/Uuid/src/Uuid.php on line 37

I consider myself a newbie with this stuff, Composer is still relatively new to me. So what would be the best way to include this package at the moment? Right now I am more inclined doing this manually, but I am really looking for a way to manage dependencies in my module via Composer.

@yurikuzn
Copy link
Contributor

yurikuzn commented Feb 8, 2019

We're going to add this format supported:

{
    "psr-4": {
          "Ramsey\\Uuid\\": "application/Espo/Modules/MyModule/vendor/ramsey/uuid/src"
    }
}

@arkadiyasuratov
Copy link
Contributor Author

Wow, that's great! I'm looking forward then for future releases.

@yurikuzn
Copy link
Contributor

yurikuzn commented Jun 25, 2020

Already implemented. I believe. 6c6d1a7#diff-34e859ca9a61c2985f98a983592b5153

@yurikuzn yurikuzn reopened this Jun 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants