Composer - 使用本地存储库Composer

由网友(老来伴)分享简介:我是 Composer 初学者,我正在尝试使一个项目依赖于另一个项目,而这两个项目仅存在于我的本地计算机上.我的库项目(ProjectA)中的composer.json是:{名称":项目/工具",类型":库"}我在这个项目的基础文件夹中初始化了 git.我在项目中的composer.json取决于第一个(Project...

我是 Composer 初学者,我正在尝试使一个项目依赖于另一个项目,而这两个项目仅存在于我的本地计算机上.

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

{名称":项目/工具",类型":库"}
Composer下载安装使用

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

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

{存储库":[{名称":工具",类型":git",网址":/d/workspaces/util"}],要求": {项目/工具":*"},}

当我从 ProjectB 运行 composer install 时,我收到以下错误:

[RuntimeException]克隆失败,无法从中读取包致命:存储库"不存在

我认为存储库的 url 有问题,但我不知道还有什么要写的.

解决方案

使用composer自动加载本地包(每次更改都不需要去packagist).

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

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

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

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

示例:

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

 "自动加载": {psr-4":{本地Pack":库"}},自动加载开发":{psr-4":{本地包测试":测试"}},

那么在主项目的composer.json中我们应该有:

 "自动加载": {psr-4":{"MahmoudzProject": "src","LocalPack": "../path/to/local/pack/library" << 引用其他本地包}},自动加载开发":{psr-4":{MahmoudzProjectTests":测试"}},

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

方法二:(本地仓库)

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

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

"name": "vendor-name/package-name","autoload": { ...//使用你喜欢的任何方法,但要确保它被正确加载最低稳定性":开发"

composer 更新供应商名称/包名称现在检查您的供应商目录,您应该会看到供应商名称/包名称

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

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

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.

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

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

I initialized git in the base folder of this project.

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

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

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

When I run composer install from ProjectB, I get the following error:

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

解决方案

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

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

Method 1: (direct namespace)

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

example:

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

  "autoload": {
    "psr-4": {
      "LocalPack": "library"
    }
  },
  "autoload-dev": {
    "psr-4": {
      "LocalPackTests": "tests"
    }
  },

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

  "autoload": {
    "psr-4": {
      "MahmoudzProject": "src",
      "LocalPack": "../path/to/local/pack/library"                   << referencing the other local package
    }
  },
  "autoload-dev": {
    "psr-4": {
      "MahmoudzProjectTests": "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)

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:

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

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

"minimum-stability": "dev"  

composer dump-autoload

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

  "repositories": [
    {
      "type": "vcs",
      "url": "/full/path/to/the/local/package/package-name"
    }
  ],
  "require": {
    "vendor-name/package-name": "dev-master"
  },

composer update vendor-name/package-name now check your vendor directory you should see the vendor-name/package- name

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.

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)

阅读全文

相关推荐

最新文章