Skip to content

preAutoloadDump doesn't handle vendor-namespaced app plugins (Vendor/Plugin) #79

@steinkel

Description

@steinkel

When baking a vendor-namespaced plugin (bin/cake bake plugin YourVendor/YourPlugin) the plugin class is never
actually loaded, and in 5.3 you now get a deprecation warning about it:

The class does exist (bake generated it). The problem is the autoloader doesn't know about it.

In CakePHP 4 / bake 2.x this worked because bake generated a composer.json inside the plugin directory with the PSR-4
mapping. In bake 3.x that file is no longer generated for app plugins, presumably because preAutoloadDump() in
plugin-installer v2 was meant to cover it. And it does but only for flat plugins (plugins/YourPlugin/src/).

For a vendor-namespaced plugin the structure is plugins/YourVendor/YourPlugin/src/. preAutoloadDump() iterates one
level deep, finds YourVendor/, checks whether plugins/YourVendor/src/ exists (it doesn't), and moves on. The
YourVendor\YourPlugin\ namespace never gets registered.

The fix is to detect that a first-level directory has no src/ and recurse one level deeper to find plugins inside it.
The existing !isset() guard already means manually-added entries in composer.json take precedence, so it wouldn't
break anything.

Workaround for now: manually add the mapping to the root composer.json:

  "autoload": {
      "psr-4": {                                                                                                       
          "App\\": "src/",
          "YourVendor\\YourPlugin\\": "plugins/YourVendor/YourPlugin/src/"                                             
      }                                                     
  }

Then composer dump-autoload.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions