2

I'm working on a project that's a WordPress plugin. This is my directory structure:

├── src/
│   ├── plugin-name.php
│   └── readme.txt
├── tests/
└── ...

I want to include a composer.json file with the plugin so that users can install with Composer if they wish. For example:

├── src/
│   ├── composer.json
│   ├── plugin-name.php
│   └── readme.txt
├── tests/
└── ...

But I also want to use the vfsStream component when I'm unit testing the plugin. This will mean I'll need to require vfsStream in a composer.json file that's in my root. For example:

├── src/
│   ├── composer.json
│   ├── plugin-name.php
│   └── readme.txt
├── tests/
├── composer.json
└── ...

I'm not sure a single project can have 2 composer.json files. Is having 2 composer.json files the right way to go about this?

henrywright
  • 10,070
  • 23
  • 89
  • 150
  • It is not clear why you are creating two composer packages? Are they both on Packagist? – jacmoe Feb 16 '16 at 12:56
  • One in my root directory so I can use a particular component in my unit tests and another in my `src` directory. The contents of `src` get uploaded to wordpress.org which means users can install my plugin via Composer (should they wish). – henrywright Feb 16 '16 at 13:21
  • In that case, I reworded my answer to encourage you to require your Wordpress Composer package in your develop project instead. – jacmoe Feb 16 '16 at 13:35

2 Answers2

2

Have one composer.json in your project root and add develop dependencies for tests using require-dev

https://getcomposer.org/doc/04-schema.md#require-dev

Michel Feldheim
  • 17,625
  • 5
  • 60
  • 77
  • 1
    This indicates I should have just one composer.json file in my root. The problem is, my root composer.json isn't distributed with my plugin. Only the contents of the `src/` directory is distributed – henrywright Feb 16 '16 at 12:44
  • OK, wasn't aware of that. You can have multiple composer.json files, that shouldn't be an issue. if you run e.g. `composer install` it will use the composer.json in the folder you're currently in – Michel Feldheim Feb 16 '16 at 15:39
1

New Answer:

I think the best way would be to turn the actual Wordpress plugin into a Composer package, and then require it in your dev-mode project.

The only difference is that it wont be in the 'src' directory but tucked away in the 'vendor' directory.

AFAIK, Composer does not handle nested 'composer.json' files yet, if ever.

Original Answer:

The right way is to make a 'require-dev' section, like this:

  "require-dev": {
    "whoever/vfsStream": "*"
},

after the require section in your one and only composer.json file.

This requires that people use the --no-dev flag when installing for production, though.

See Using "require-dev" to install packages in composer

If you use the require-dev section for develop packages, you are giving people a choice to not use them. They can just pass the 'no-dev' flag when requiring your package.

Edit: If you are using src as the main directory of your package, you can set that up in your 'composer.json' in an 'autoload' section like this:

"autoload": {
    "psr-0": {
        "yourname\\yourpackage\\": ["src/"]
    }
}

Edit Again:
It looks like using Composer to install Wordpress plugins is not totally without problems:
A Narrative of Using Composer in a WordPress Plugin

However, if you insist, there might be tricks up the sleeve of this post: Using Composer to manage Wordpress themes and plugins

Community
  • 1
  • 1
jacmoe
  • 1,637
  • 13
  • 17
  • Thanks for the info on `require-dev`. I was actually already aware of that and am using it. The problem is, my root composer.json isn't distributed with my plugin. Only the contents of the `src/` directory is distributed – henrywright Feb 16 '16 at 12:46
  • Why? I mean, if you have a composer package set up in the root, then I don't understand the need to put a second composer.json in the 'src' directory. – jacmoe Feb 16 '16 at 12:53
  • The contents of my `src` directory get uploaded to wordpress.org whereas the stuff in my project root such as composer.json, the `tests` directory etc don't . – henrywright Feb 16 '16 at 13:00
  • I understand that part. What I don't understand, however, is how installing a Wordpress plugin by means of Composer works. I think that is the main issue :) – jacmoe Feb 16 '16 at 13:03
  • OK. I just read up on how people are using Composer and Wordpress together - and it does not seem to be all harmony: http://wptavern.com/a-narrative-of-using-composer-in-a-wordpress-plugin – jacmoe Feb 16 '16 at 13:08
  • 1
    you're right, using composer inside a WordPress plugin does have many many problems. – henrywright Feb 16 '16 at 13:19