简体   繁体   中英

Symfony 2 bundle with its own composer.json

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?

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, eg 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 xx-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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM