phpwordpressautoloadpsr-4

Autoload with psr-4 my own classes in a monorepo


I'm writing a WordPress plugin within a monorepo architecture, this is my directory structure:

packages/
  ├── plugin
  └── dependencie-1

In dependencie-1 I have a psr-4 autoload configured in composer.json:

"autoload": {
    "psr-4": {
        "Mdorim\\": "src/php/"
    }
}

And in plugin my composer.json is as follow:

"autoload": {
    "psr-4": {
        "LCDR\\": "./src/php",
        "LCDR\\Tests\\": "./tests/Pest"
    }
},
"repositories": [
    {
        "type": "path",
        "url": "../mdorim"
    }
],
"require": {
    "monorepo/mdorim": "@dev"
},

At the main file of plugin I'm requiring the autoloader from vendor

require '../vendor/autoload.php'

While I can access all the classes from plugin with the autoloader configured, I can't use the classes from dependencie-1 and I keep getting this error:

PHP Fatal error:  Uncaught Error: Class "Mdorim\Core" not found in /var/www/html/wp-content/plugins/elucidario-art/lcdr.php:52

Obviously the classes from dependencies are not at the main path of plugin but in its vendor directory. The psr-4 should not resolve this? Am I missing something?

update

It's probably something with the docker not resolving the symlinked directory, because it's working properly in a phpunit configured test. But nothing happend if I use symlink option false at repositories configuration for dependencie-1 at composer.json:

"repositories": [
    {
        "type": "path",
        "url": "../mdorim",
        "options": {
            "symlink": false
        }
    }
],

Solution

  • I actually solved the problem with symlink option marked as false, but I had to delete composer.lock and run composer install again.