git Composer - 使用本地存储库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17426192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:29:24  来源:igfitidea点击:

Composer - using a local repository

gitcomposer-php

提问by Banana

I am a Composer beginner and I am trying to make one project dependent of another one, while both project only exist on my local machine.

我是一名 Composer 初学者,我试图让一个项目依赖于另一个项目,而这两个项目都只存在于我的本地机器上。

The composer.json in my library project (ProjectA) is:

我的库项目(ProjectA)中的 composer.json 是:

{
    "name" : "project/util",
    "type" : "library"
}

I initialized git in the base folder of this project.

我在这个项目的基本文件夹中初始化了 git。

My composer.json in the project depending on the first one (ProjectB):

我在项目中的 composer.json 取决于第一个(ProjectB):

{
    "repositories": [
        {
            "name" : "util", 
            "type" : "git",
            "url" : "/d/workspaces/util"
        }   
    ],

    "require": {
        "project/util" : "*"
    },
}

When I run composer installfrom ProjectB, I get the following error:

当我composer install从 ProjectB运行时,出现以下错误:

[RuntimeException]
Failed to clone , could not read packages from it
fatal: repository '' does not exist
[RuntimeException]
Failed to clone , could not read packages from it
fatal: repository '' does not exist

I asume something is wrong with the url of the repository, but I am not sure what else to write there.

我假设存储库的 url 有问题,但我不确定在那里还能写什么。

回答by Mahmoud Zalt

Autoload local package using composer (without going to packagist every time you change).

使用 composer 自动加载本地包(无需每次更改时都去 packagist)。

There are many ways to do so, I will be covering 2 of them:

有很多方法可以做到这一点,我将介绍其中的两种:

In all cases we have 2 main parties:
- the local package(the code that we do not want to publish on packagist to be able to autoload it in our project composer).
- the main project(the code base that needs to use the local package code, can be another package and or any project).

在所有情况下,我们都有 2 个主要参与方:
-本地包(我们不想在 packagist 上发布的代码,以便能够在我们的项目 Composer 中自动加载它)。
-主项目(需要使用本地包代码的代码库,可以是另一个包和/或任何项目)。



Method 1: (direct namespace)

方法一:(直接命名空间)

Open the main project composer.jsonfile and autoload the local package namespaces using any method (PSR-4, PSR-0, ...).

打开主项目composer.json文件并使用任何方法(PSR-4、PSR-0...)自动加载本地包命名空间。

example:

例子:

if in the composer.json of the local package we have:

如果在本地包的 composer.json 中,我们有:

  "autoload": {
    "psr-4": {
      “Local\Pack\": "library"
    }
  },
  "autoload-dev": {
    "psr-4": {
      "Local\Pack\Tests\": "tests"
    }
  },

then in the composer.json of the main project we should have:

然后在主项目的 composer.json 中我们应该有:

  "autoload": {
    "psr-4": {
      "Mahmoudz\Project\": "src",
      "Local\Pack\": "../path/to/local/pack/library”                   << referencing the other local package
    }
  },
  "autoload-dev": {
    "psr-4": {
      "Mahmoudz\Project\Tests\": "tests"
    }
  },

Advantages:
- you don't touche the vendor directory (running composer update by mistake will not override your local changes)
- you don't need your package to be on packagist to use it
- you work in one place (the local package) and the changes are automatically loaded in the main project
Disadvantages:
- you cannot publish the composer.json on production (needs editing before publishing to require the real package)

优点:
- 你不接触供应商目录(错误地运行 composer update 不会覆盖你的本地更改)
- 你不需要你的包在 packagist 上使用它
- 你在一个地方工作(本地包)并且更改会自动加载到主项目中
缺点:
- 您无法在生产中发布 composer.json (需要在发布前进行编辑以需要真实的包)

Method 2: (local repository)

方法二:(本地仓库)

Download the local package from a local repository.

从本地存储库下载本地包。

local package:
1. initialize git in the package (even if you don't want to use it - no need to commit anything)
2. add composer.json file. In the file make sure you have the following:

本地包:
1. 在包中初始化 git(即使你不想使用它 - 不需要提交任何东西)
2. 添加 composer.json 文件。在文件中确保您有以下内容:

"name": "vendor-name/package-name",  

"autoload": { …   // use whichever method you prefer, but make sure it's being loaded correctly

"minimum-stability": "dev"  
  1. composer dump-autoload
  1. composer dump-autoload

main project:
1. edit your composer.json to contain the following:

主要项目:
1. 编辑您的 composer.json 以包含以下内容:

  "repositories": [
    {
      "type": "vcs",
      "url": "/full/path/to/the/local/package/package-name"
    }
  ],
  "require": {
    "vendor-name/package-name": "dev-master"
  },
  1. composer update vendor-name/package-name
  2. now check your vendor directory you should see the vendor-name/package- name
  1. 作曲家更新供应商名称/包名称
  2. 现在检查您的供应商目录,您应该会看到供应商名称/包名称

NOTE: whenever you make change in the local package (not the vendor) you need to git commit then you can composer update the main project, it will get the latest copy of the repo to the main project vendor directory.

注意:每当您在本地包(而不是供应商)中进行更改时,您都需要 git commit 然后您可以对主项目进行 Composer 更新,它将获得最新的 repo 副本到主项目供应商目录。

Advantage:
- you don't touch the vendor directory (running composer update by mistake will not override your local changes) - you don't need your package to be on packagist to use it
Disadvantage:
- you have to keep committing your changes (in the local package) and then running composer update in the main project
- you cannot publish the composer.json on production (needs editing before publishing to require the real package)

优点:
- 你不接触供应商目录(错误地运行 composer update 不会覆盖你的本地更改) - 你不需要你的包在 packagist 上使用它
缺点:
- 你必须不断提交你的更改(在本地包中)然后在主项目中运行 composer update
- 您不能在生产中发布 composer.json (需要在发布前进行编辑以需要真正的包)

回答by Danack

I think you've just got the syntax wrong. The type should just be VCS, and then composer figures out what type of VCS it is.

我认为您只是语法错误。类型应该只是 VCS,然后 composer 找出它是什么类型的 VCS。

So in your project B, the entry for repositories should be:

因此,在您的项目 B 中,存储库的条目应为:

"repositories": [
    {
        "type": "vcs",
        "url" : "/d/workspaces/util"
    }
],

You don't need to name what library is available in /d/workspaces/util. Composer will scan the composer.json file in that directory and know what project name is available there, and use the project from that directory in preference to a version listed on packagist or other repository.

您无需命名/d/workspaces/util. Composer 将扫描该目录中的 composer.json 文件并知道那里可用的项目名称,并优先使用该目录中的项目而不是 packagist 或其他存储库中列出的版本。

回答by Max

In addition to Danack's solution: Changing the path from /d/ to d:/ worked for me.

除了 Danack 的解决方案:将路径从 /d/ 更改为 d:/ 对我有用。

Like this:

像这样:

"repositories": [
    {
        "type": "vcs",
        "url" : "d:/workspaces/util"
    }
],

回答by Friendly Code

I found this tutorial really useful: https://johannespichler.com/developing-composer-packages-locally/when I was facing issues with local package production

当我遇到本地包生产问题时,我发现本教程非常有用:https: //johannespichler.com/developing-composer-packages-locally/

Note the symlinkcondition allowing the vendor folder to be a symlink which then means you no longer need to composer updateeach time you want to see change

请注意symlink允许供应商文件夹成为符号链接的条件,这意味着您不再需要composer update每次想要查看更改

"options": {
        "symlink": true
      }

回答by IonV

The easiest way is to use type=path https://getcomposer.org/doc/05-repositories.md#path

最简单的方法是使用 type=path https://getcomposer.org/doc/05-repositories.md#path

{
"repositories": [
    {
        "type" : "path",
        "url" : "/d/workspaces/util"
    }   
],

"require": {
    "project/util" : "*"
},

}

}