Why using PSR4 autoload doesn't add any classes into classmap/namespaces file?

早过忘川 提交于 2020-01-14 03:16:28

问题


I've got the following composer.json file:

{
  "require-dev": {
    "queueit/KnownUser.V3.PHP": "dev-master"
  },
  "repositories": [
    {
      "type": "package",
      "package": {
        "name": "queueit/KnownUser.V3.PHP",
        "version": "dev-master",
        "source": {
          "type": "git",
          "url": "https://github.com/kenorb-contrib/KnownUser.V3.PHP.git",
          "reference": "task/composer-autoloader"
        }
      }
    }
  ]
}

However upon running composer install, the namespaces or classes aren't added into autoload_classmap.php or autoload_namespaces.php in vendor/composer.

Before that I've added into project's composer.json these lines:

"autoload": {
    "psr-4": {
        "QueueIT\\": ""
    }
}

in order to scan for the class/namespaces within the current folder and the file looks like:

$ cat vendor/queueit/KnownUser.V3.PHP/composer.json 
{
    "name": "queueit/knownuserv3",
    "description": "The Queue-it Security Framework is used to ensure that end users cannot bypass the queue by adding a server-side integration to your server.",
    "require": {
        "php": ">=5.3.3"
    },
    "license":"LGPL-3.0",
    "autoload": {
        "psr-4": {
            "QueueIT\\": ""
        }
    }
}

Executing dump-autoload manually doesn't take any effect as well as follow:

$ composer dump-autoload -o
Generating optimized autoload files
$ grep -R QueueIT vendor/composer/
(no results)

To confirm that's the case, here is the shell command to test it:

$ php -r 'require __DIR__ . "/vendor/autoload.php"; use QueueIT\KnownUserV3\SDK\KnownUser; new KnownUser;'
Fatal error: Uncaught Error: Class 'QueueIT\KnownUserV3\SDK\KnownUser' not found in Command line code:1

However the classmap is generated when executing composer dump-autoload -o directly in the project folder it-self (within vendor/queueit/KnownUser.V3.PHP/ folder).

Why my autoload definition in project's composer.json doesn't take any effect when run from the top folder?


回答1:


As per @stof comment at GitHub, Composer doesn't support loading of composer.json file from the repositories of the type package. The goal of a package type is to download projects which are not supporting Composer. Therefore composer.json file is never read.

Solution is to use vcs type instead. Here is the composer.json which should work:

{
  "require": {
    "queueit/knownuserv3": "dev-master"
  },
  "repositories": [
    {
      "type": "vcs",
      "url": "https://github.com/queueit/KnownUser.V3.PHP"
    }
  ]
}

Otherwise "you would have to copy the project metadata entirely in your package repository, and this is harder to maintain".



来源:https://stackoverflow.com/questions/47497818/why-using-psr4-autoload-doesnt-add-any-classes-into-classmap-namespaces-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!