为什么使用PSR4自动加载功能不会在classmap/namespaces文件中添加任何类? [英] Why using PSR4 autoload doesn't add any classes into classmap/namespaces file?

查看:49
本文介绍了为什么使用PSR4自动加载功能不会在classmap/namespaces文件中添加任何类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 composer.json 文件:

{
  "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"
        }
      }
    }
  ]
}

但是,在运行 composer install 时,名称空间或类不会添加到中的 autoload_classmap.php autoload_namespaces.php 中供应商/撰写人.

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

在将以下行添加到项目的 composer.json 之前,

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\\": ""
        }
    }
}

手动执行 dump-autoload 不会产生任何效果,也不能执行以下操作:

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)

要确认是这种情况,请使用以下shell命令对其进行测试:

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

但是,直接在自己的项目文件夹中(在 vendor/queueit/KnownUser.V3.PHP/中)直接执行 composer dump-autoload -o 时,会生成类映射.文件夹).

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).

为什么从顶部文件夹中运行时,项目 composer.json 中的自动加载定义不起作用?

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

推荐答案

根据 @stof 在GitHub上发表的评论 Composer 不支持加载类型为 package 的存储库中的 composer.json 文件. package 类型的目标是下载支持 Composer 的项目.因此,从不读取文件 composer.json .

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.

解决方案是改用 vcs 类型.这是应该起作用的 composer.json :

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".

这篇关于为什么使用PSR4自动加载功能不会在classmap/namespaces文件中添加任何类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆