composer从两个composer.json文件安装

时间:2017-10-05 07:50:51

标签: php laravel-5 composer-php vendor

我有多个composer.json具有多个单独的依赖关系,并希望使用单个composer.json命令在composer install中安装所有依赖项。

位置是这样的:

| - composer.json
| - Custom
    | - Package1
        | - composer.json

首先 composer.json

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=5.6.4",
        "barryvdh/laravel-debugbar": "^2.3",
        "laravel/framework": "5.4.*",
        "laravel/tinker": "~1.0",
        "laravelcollective/html": "^5.4.0"
    },
    "require-dev": {
        "fzaninotto/faker": "~1.4",
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "~5.7",
        "symfony/css-selector": "3.1.*",
        "symfony/dom-crawler": "3.1.*"
    },
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/",
        }
    },
    "autoload-dev": {
        "psr-4": {
          "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-root-package-install": [
            "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ],
        "post-install-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postInstall",
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postUpdate",
            "php artisan optimize"
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true
    }
}

Package1目录中的第二个 composer.json

{
    "name": "custom/package1",
    "description": "",
    "require": {
        "php": ">=5.6",
        "composer/installers": "~1.0",
        "lavary/laravel-menu": "1.7.1"
    },
    "autoload": {
        "psr-4": {
            "Custom\\Package1\\": ""
        }
    }
}

我想在lavary/laravel-menu内安装Package1到安装了所有默认软件包的主供应商目录。

|- vendor  //<==want here
| - composer.json
    | - Custom
        | - Package1
            | - vendor  //<== not here
            | - composer.json

我测试了这个解决方案:

  

https://stackoverflow.com/a/27735674

像这样:

{
    "config": {
        "vendor-dir": "../../vendor/"
    }
}

这会安装软件包但我们需要进入第二个composer.json而不是main composer.json,并从第一个composer.json中删除已安装的软件包。

如何在不进入单个供应商目录中的第二个或多个composer.json的情况下从主composer.json安装所有依赖项?

1 个答案:

答案 0 :(得分:6)

经过一些研究和建议,我发现有多种方法可以实现这个解决方案。

  1. 使用外部包来维护依赖性。
  2. 感谢rickdenhaan让我知道

    Composer Merge插件

      

    https://github.com/wikimedia/composer-merge-plugin

    首先我们需要这个包:

    composer require wikimedia/composer-merge-plugin
    

    然后我的 composer.json 变成这样:

    {
        "name": "laravel/laravel",
        "description": "The Laravel Framework.",
        "keywords": ["framework", "laravel"],
        "license": "MIT",
        "type": "project",
        "require": {
            "php": ">=5.6.4",
            "barryvdh/laravel-debugbar": "^2.3",
            "laravel/framework": "5.4.*",
            "laravel/tinker": "~1.0",
            "wikimedia/composer-merge-plugin": "^1.4"
        },
        "require-dev": {
            "fzaninotto/faker": "~1.4",
            "mockery/mockery": "0.9.*",
            "phpunit/phpunit": "~5.7",
            "symfony/css-selector": "3.1.*",
            "symfony/dom-crawler": "3.1.*"
        },
        "autoload": {
            "classmap": [
                "database"
            ],
            "psr-4": {
                "App\\": "app/"
            }
        },
        "autoload-dev": {
            "psr-4": {
              "Tests\\": "tests/"
            }
        },
        "scripts": {
            "post-root-package-install": [
                "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
            ],
            "post-create-project-cmd": [
                "php artisan key:generate"
            ],
            "post-install-cmd": [
                "Illuminate\\Foundation\\ComposerScripts::postInstall",
                "php artisan optimize"
            ],
            "post-update-cmd": [
                "Illuminate\\Foundation\\ComposerScripts::postUpdate",
                "php artisan optimize"
            ]
        },
        "config": {
            "preferred-install": "dist",
            "sort-packages": true,
            "optimize-autoloader": true
        },
        "extra": {
            "merge-plugin": {
                "include": [
                    "Custom/*/composer.json"
                ],
                "recurse": true,
                "replace": false,
                "ignore-duplicates": true,
                "merge-dev": true,
                "merge-extra": false,
                "merge-extra-deep": false,
                "merge-scripts": true
            }
        }
    }
    

    现在,  跑

    composer install
    

     composer update
    

    然后,每个目录中的composer.json将合并到默认的vendor目录中。

    1. 下一个解决方案可能是将包发布到packagist并在composer.json中需要,并且在composer安装期间将安装所有依赖项。

      就像Asgardcms已经完成的那样。

    2. 将私有存储库添加到composer.json。

        

      Configuring composer.json with private bitbucket mercurial repository

    3. 此选项需要将metapackage的composer.json添加到主composer.json文件中。

    4. (我不确定该选项,但感谢JParkinson1991知道此解决方案的人可以在此选项上添加解释。只是为了让某人知道此解决方案存在。)

      以下是示例解决方案:

        

      PHP & Composer, how do I combine composer.json files

      在这方面,第一个解决方案适合我的情况。希望这可以帮助那些花费大量时间进行搜索的人。