【问题标题】:Composer package duplicating directories when installingComposer包在安装时复制目录
【发布时间】:2018-12-30 11:15:58
【问题描述】:

我有一个本地依赖项,它托管在私有 Gitlab 存储库上。但是,我很难通过 Composer 将其拉入我的项目中。

我的composer.json

"require": {
    "crmpicco/GolfBundle": "dev-master"
},
"repositories": [
    {
        "type": "package",
        "package": {
        "name": "crmpicco/GolfBundle",
        "version": "dev-master",
        "source": {
            "url": "https://git.crmpicco.com/rfc1872/golfbundle.git",
            "type": "git",
            "reference": "master"
        },
        "autoload": {
            "psr-4": {
                "crmpicco\\GolfBundle\\": ""
            }
        }
        }
    }
],

当我查看供应商目录时,当我没想到时,这些目录会加倍,例如 /vendor/crmpicco/GolfBundle/crmpicco/GolfBundle

当我运行 composer update crmpicco\GolfBundle 时,当 Symfony 尝试进行缓存时出现以下错误:清除:

Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache 处理 post-update-cmd 事件因异常而终止

[运行时异常]
执行“'cache:clear --no-warmup'”命令时出错:

PHP Fatal error:  Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted   
  to load class "crmpiccoGolfBundle from namespace "crmpicco\GolfBundle".         
  Did you forget a "use" statement for "crmpicco\GolfBundle\crmpiccoGolfBundle"?   
  in /var/www/crmpicco/symfony/app/AppKernel.php:31   

我在composer.json 设置中遗漏了什么/做错了什么?

捆绑目录结构:

/crmpicco
   /GolfBundle
      /Component      
      /DependencyInjection
      crmpiccoGolfBundle.php

捆绑composer.json:

{
  "name": "crmpicco/GolfBundle",
  "type": "library",
  "description": "A Symfony 2 bundle which provides an easy way to handle billing and subscriptions.",
  "license": "MIT", 
  "require": {
    "php": ">=7.0",
    "symfony/config": "~2.8.34",
    "symfony/dependency-injection": "~2.8.34",
    "symfony/http-kernel": "~2.8.34",
  },
  "autoload": {
    "psr-4": {
      "crmpicco\\GolfBundle\\": ""
    }
  },
  "extra": {
    "symfony-app-dir": "app",
    "symfony-web-dir": "web",
    "symfony-assets-install": "relative"
  }
}

【问题讨论】:

  • 你能把你的 GolfBundle 的目录结构粘贴到这里吗,它是composer.json
  • @NikitaLeshchev 嗨,当然。我已经更新了这个问题。我的包目录中没有composer.json
  • @NikitaLeshchev 为混淆道歉,我已将属于我的捆绑包的composer.json 添加到上述问题中。

标签: php symfony composer-php dependency-management


【解决方案1】:

好的。如我所见,您在捆绑包的composer.json 中有错误的psr-4 自动加载配置 您必须将其更改为以下内容:

"autoload": {
    "psr-4": {
        "crmpicco\\GolfBundle\\": "crmpicco/GolfBundle"
    }
}

此外,如果您不想复制目录,请将捆绑包的内容移动到根目录,然后不要更改 composer.json 内容。目录重复,因为 Composer 基于 name 属性创建目录结构,在您的情况下也是 crmpicco/GolfBundle

【讨论】:

  • 这很有帮助,我已经设法摆脱了重复的目录,但是我似乎无法将包加载到我的 AppKernel 中。在registerBundles 里面我正在这样做:new crmpicco\GolfBundle\crmpiccoGolfBundle(), 并且在我的vendor 目录中我有:/vendor/crmpicco/GolfBundle 其中包含所有代码,包括crmpiccoGolfBundle.php,我得到了错误:PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "crmpiccoGolfBundle" from namespace "crmpicco\GolfBundle". 我没有看看我哪里错了……
  • 捆绑包的 composer.json autoload 部分如下所示:"autoload": { "psr-4": { "crmpicco\\GolfBundle\\": "" } },。我还应该指出,我已经用composer clear-cache 刷新了作曲家缓存。非常感谢任何帮助。
  • @crmpicco 你用的是哪个版本的 symfony?
  • 我使用的是 2.8 LTS。
  • @crmpicco 试试composer dump-autoload。还要检查你的班级crmpiccoGolfBundle 的命名空间。可能类命名或命名空间有任何错误
【解决方案2】:

对于包含有效 composer.json 的存储库,您不应使用 package 类型。这个类型是为没有composer.json的包设计的,所以这个文件将被完全忽略,就像你包中的更新一样。

在您的情况下,最好将其定义为git

"repositories": [
    {
        "type": "git",
        "url": "https://git.crmpicco.com/rfc1872/golfbundle.git"
    }
],

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-11
    • 2012-11-01
    • 2012-06-21
    • 2022-10-09
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    相关资源
    最近更新 更多