33

When I run composer install, it will install all my "require" and the "require" of the other package.

My composer.json

{
    "name": "my_app",
    "require": {
        "some/package": "0.0.0"
    }
}

The "child" dependency

{
    "name": "some/package",
    "require": {
        "zendframework/zend-mail": "2.4.*@dev",
        "soundasleep/html2text": "~0.2",
        "mpdf/mpdf": "6.0.0",
        "endroid/qrcode": "1.*@dev"
    }
}

I know that it's possible ignore the php extensions, but what about these second require package?

bwaindwain
  • 45
  • 10
Pablo
  • 1,953
  • 4
  • 28
  • 57

5 Answers5

56

You can ignore packages to be downloaded with the replace property of your composer.json: https://getcomposer.org/doc/04-schema.md#replace

That way you tell composer, that you take or took care of that package's content on your own.

This can help you to ignore a package your are sure you do not need, but it is kind of hacky. So be aware some things (like tests) might break. A better approach would be to request a patch from the maintainer of the original package to make the requirements optional (via the suggest property).

edit:
Example for "disabling" the requirement to zendframework/zend-mail:

{
    "name": "my_app",
    "require": {
        "some/package": "0.0.0"
    },
    "replace": {
        "zendframework/zend-mail": "*"
    }
}
derhasi
  • 709
  • 6
  • 7
  • 1
    I don't understand from the documentation that how I can prevent a child dependency. Can you please give an example? – Bunyamin Inan Nov 07 '17 at 19:14
  • 1
    @Bunyamin I added an example to my post. – derhasi Nov 22 '17 at 13:35
  • 1
    If any of your dependencies needs a specific version, you need to set your version of the package to the same `"version": "3.1.4"`, e.g under `"name": "some/package"`. You are basically replacing that package with the _current_ composer.json itself. – luckydonald Apr 03 '18 at 11:47
  • Is the `suggest` approach really better? A default usage of a package would probably cause an error. I'd prefer the `replace` appraoch, where one explicitly ignores a dependency, knowing what she or he is doing. – robsch Apr 04 '23 at 07:23
26

I know that it's possible ignore the php extensions, but what about these second require package?

Yes, you can use --ignore-platform-reqs to ignore php, hhvm, lib-* and ext-* platform requirements and force the installation, even if the local machine does not fulfill these.

But, i'm not sure were you are heading with your question. What's the use-case for requiring a package without its dependencies in your application? Isn't this the main reason to use Composer?

No, its not possible, unless you are looking for require --no-update, which disables the automatic update of the dependencies. Please take a look at the CLI options for require and install.

Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
  • The package that I'm using have a lot of "libs" which I use some. So those no used libs require things that I don't need, but I don't want just copy the files and loose the next updates. – Pablo Jan 06 '16 at 19:49
  • 1
    I would suggest to approach this differently: fetch everything, also the child dependencies, build your application and use only some of these dependencies and then when you build a release package of your stable application, delete the unused libraries before or ignore them during packaging. That allows to work with all dependencies during development and but shrinks stuff to the minimum for deployment. – Jens A. Koch Jan 06 '16 at 20:07
  • 1
    Right! Thank you for the advice. – Pablo Jan 06 '16 at 22:06
5

One more way to solve it:

  1. Clone package to your repository and fix dependencies
  2. In your composer.json add your repository:
"repositories": [
    { "type": "git", "url": "https://github.com/zhovtyj/mailchimp-laravel" }
],
"require": {
    "php": "^7.1.3",
    ***
    "skovmand/mailchimp-laravel": "dev-master",
},
zhovtyj
  • 61
  • 1
  • 3
2

Another option could be to modify the composer.json of the child package and to remove the required dependencies. Then you could host the zip file and add reference it by adding an extra repository for your main package.

"repositories": [
    {
        "type": "package",
        "package": {
            "name": "dr-que/x-y",
            "version": "master",
            "dist": {
                "type": "zip",
                "url": "http://xyplot.drque.net/Downloads/XY_Plot-1.4.zip",
                "reference": "master"
            },
            "autoload": {
                "classmap": ["."]
            }
        }
    }
]

Then in your require section, add your selected name as follows.

"require": {
    "dr-que/x-y": "dev-master"
}

For the autoload, I just copied the same autoload section of the child package.

The original solution can be found here

emp
  • 4,926
  • 2
  • 38
  • 50
1

Just Use

 composer install --ignore-platform-reqs
  • 5
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. You can find more information on how to write good answers in the help center: https://stackoverflow.com/help/how-to-answer . Good luck – nima Oct 03 '21 at 22:09
  • `--ignore-platform-reqs` requires an argument so, this command will fail with an error. – ObiTech Invents Aug 07 '23 at 22:30