Composer - 使用本地存储库 [英] Composer - using a local repository

查看:24
本文介绍了Composer - 使用本地存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

{名称":项目/工具",类型":库"}

我在这个项目的基础文件夹中初始化了 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":{"Mahmoudz\Project\": "src","Local\Pack\": "../path/to/local/pack/library" << 引用其他本地包}},自动加载开发":{psr-4":{Mahmoudz\Project\Tests\":测试"}},

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

方法二:(本地仓库)

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

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

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

  1. 作曲家转储自动加载

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

 存储库":[{类型":vcs","url": "/full/path/to/the/local/package/package-name"}],要求": {供应商名称/包名称":开发主机"},

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

注意:每当您在本地包(而不是供应商)中进行更改时,您需要 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": {
      "Local\Pack\": "library"
    }
  },
  "autoload-dev": {
    "psr-4": {
      "Local\Pack\Tests\": "tests"
    }
  },

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

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

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"  

  1. 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"
  },

  1. composer update vendor-name/package-name
  2. 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)

这篇关于Composer - 使用本地存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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