Skip to content
New issue

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

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

Already on GitHub? # to your account

Dependency namespace being written on every line when overriding autoloader #134

Open
begroff opened this issue Aug 16, 2021 · 1 comment

Comments

@begroff
Copy link

begroff commented Aug 16, 2021

I have a WordPress theme which uses Timber via Composer. Timber has a dependency of a version of Twig that has the following autoloader in their composer.json.

    "autoload": {
        "psr-0" : {
            "Twig_" : "lib/"
        },
        "psr-4" : {
            "Twig\\" : "src/"
        }
    },

When I attempt to run vendor\bin\mozart compose, I get an error, File already exists at path: lib/Dependencies/Twig/Extension/InitRuntimeInterface.php. I understand why this error happens, because in the twig package they have the same set of files in different directories to support both psr-0 and psr-4 and mozart is trying to process it twice.

I attempted to override this autoloader behavior in the mozart configuration, but I don't think I'm doing it right. I tried the following:

"override_autoload": {
                "twig/twig": {
                    "psr-0" : {
                        "Twig_" : "src/"
                    },
                    "psr-4" : {
                        "Twig\\" : "src/"
                    }
                }
            },

When compose ran, it copied my dependent namespace on every line of the processed files. I also tried having psr-0 with empty curly braces and got the same behavior. I am using mozart version 0.7.1.

Am I going about this the wrong way?

@gchtr
Copy link

gchtr commented Feb 11, 2022

I ran into the same error and figured out that the twig/cache-extension package is the reason why you end up with a bunch of gibberish namespaces on every line of your processed files.

I got it working the following configuration for extra:

"extra": {
  "mozart": {
    "dep_namespace": "Example\\Dependencies",
    "dep_directory": "/src/Dependencies/",
    "classmap_directory": "/src/dependencies-classes/",
    "classmap_prefix": "Example_Dependency_",
    "packages": [
      "timber/timber"
    ],
    "excluded_packages": [
      "upstatement/routes"
    ],
    "override_autoload": {
      "twig/twig": {
        "psr-4": {
          "Twig\\": "src/"
        }
      },
      "twig/cache-extension": {
        "psr-4": {
          "Twig\\": "lib/Twig/"
        }
      }
    }
  }
},

In the override_autoload config, I overwrite the twig/twig autoload configuration to prevent the
File already exists at path error.

And then I pass a custom autoload configuration for twig/cache-extension as well. Because that package will be loaded into the same dependency folder as all other files from the Twig package, we need to add use lib/Twig/ instead of lib/ only.

I ignored the upstatement/routes package, because it has some weird autoloading from the main package directory. And I don’t use that anyway, so I should be fine.

I hope that helps.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants