0

I am creating a new Symfony 2 bundle which I am aiming to open source. It lives in /src/crmpicco/ChargebeeBundle.

Inside that directory I have a composer.json, which looks like this:

{
  "name": "crmpicco/ChargebeeBundle",
  "type": "library",
  "description": "A Symfony 2 bundle which provides an easy way to handle billing and subscriptions.",
  "keywords": [
    "crmpicco",
    "Chargebee",
    "Symfony",
    "Subscription"
  ],
  "license": "MIT",
  "authors": [
    {
      "name": "CRMPicco",
      "email": "picco@crmpicco.co.uk",
      "homepage": "http://www.crmpicco.co.uk",
      "role": "Analyst Developer"
    }
  ],
  "require": {
    "chargebee/chargebee-php": "^2.0"
  },
  "autoload": {
    "psr-0": {
      "": "src/",
      "SymfonyStandard": "app/"
    },
    "classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
  },
  "extra": {
    "symfony-app-dir": "app",
    "symfony-web-dir": "web",
    "symfony-assets-install": "relative"
  }
}

The dependency the bundle requires is chargebee/chargebee-php, however when I do a composer install or composer update --prefer-dist from the main Symfony project directory it does not recognise this composer.json or make any attempt to pull down that dependency.

Do I have my composer.json in the correct location and do I have my file structure set out correctly?

crmpicco
  • 16,605
  • 26
  • 134
  • 210
  • Probably not part of your problem, but the `extra` section and the autoloaders for `app/` and the `classmap` probably do not belong in your bundle, as they are defined in the actual application's composer.json. Aside from that, can you list the steps you did for adding this bundle to your project? Your bundle is not on packagist, so I assume you had to modify the project's composer.json to make it accessible? – dbrumann Feb 14 '18 at 05:50
  • The bundle only lives in our internal, private repo at the moment. So I have it sitting on my FS symlinked to the correct location in my SF project (e.g. `/src/crmpicco/ChargebeeBundle`). I do not want it on Packagist at the moment as it is a WIP. – crmpicco Feb 14 '18 at 05:52
  • 1
    That's ok, but how did you symlink it? Did you just manually do that or did you [add the path to repositories in your project's composer.son](https://getcomposer.org/doc/05-repositories.md#path)? If you just symlink it, composer will not pull in the package and therefore not resolve your bundle's composer.json. You basically bypass composer, that's why I'm asking. – dbrumann Feb 14 '18 at 05:54
  • @dbrumann I symlinked it at the CLI and not using composer if that's what you mean. i.e `ln -s`. Thanks for the link. Do you know what the `"require": { "my/package": "*" }` is referring to? Is that valid for me considering my package is unavailable on Packagist? – crmpicco Feb 14 '18 at 06:03
  • 1
    In your case you want to use the name defined in your composer.json, so `crmpicco/ChargebeeBundle`. You can also run composer with the flag `-v` to get more verbose output. That will help seeing whether composer could find the directory and the package in there. – dbrumann Feb 14 '18 at 06:58
  • Is https://stackoverflow.com/questions/17426192/composer-using-a-local-repository of any help? – Nico Haase Feb 14 '18 at 08:10

1 Answers1

1

Dependencies for the code in src are defined in the project’s root composer.json. The one in your /src/crmpicco/ChargebeeBundle is ignored.

If you want to release previously private code as shared package, you can do the following:

First, read the official best practices for reusable bundles. This will help to structure your project in a way that allows other developers to work with it.

Then, clean up the composer.json. Although the one you’ve posted contains much of what is needed, it also has some unneeded values, e.g. classmap and extra are not needed here. You might want to try this one (feel free to add author, keywords, etc.):

{
    "name": "crmpicco/ChargebeeBundle",
    "type": "symfony-bundle",
    "require": {
        "chargebee/chargebee-php": "^2.0"
    },
    "autoload": {
        "psr-4": {
            "Crmpicco\\ChargebeeBundle\\": ""
        }
    },
    "license": "MIT"
}

Then you must decide if you want to serve the bundle from a private repo or from something like Github and want it to be registered in Packagist.

If you want it in a private repo, you must edit your global composer.json and add the following section:

"repositories": [
    {
        "type": "vcs",
        "url": "ssh://git@yourgitserver.example.com/path.to.repo.git" 
    }
]

If you want to make it an official Packagist package, register an account there and follow the instructions to add your package. (Don’t forget to set up the update hook.)

In both cases you will now have to add the package name to the require section of your root composer.json. As long as you haven’t tagged releases in your package, you must add dev-master as required version and also add the line "minimum-stability" : "dev" to the root composer.json.

Now, delete the code from /src/crmpicco/ChargebeeBundle (or move it outside of the Symfony project) and run composer update --prefer-source. You should now find the bundle installed under vendor.

You may also realize that Composer updates all your Symfony packages to some x.x-dev version, which is due to the "miminum-stability" setting. Let it be; you can remove the "miminum-stability" line after the first successful run and then composer update again. It will then downgrade the Symfony dev packages again, but leave your bundle alone. This is a quite quirky approach, but I haven’t found a better one yet. (Maybe somebody else knows a better way to handle this.)

If you run into problems installing, read the output from Composer carefully and follow the instructions. For example, you might have a mismatch between the package name in the bundle’s composer.json and the require line. Or Composer might complain about unresolvable dependencies, which you need to fix.

Good luck! You may still run into other problems, but I would recommend that you post them as separate SO questions – unless they are trivial, in which case you’re welcome to post them as comments, and I’ll try to answer them and/or update this post accordingly.

lxg
  • 12,375
  • 12
  • 51
  • 73