Adding composer.json to package

Permalink
Can anyone share how we would load a composer package into a package we're developing? I understand we need a composer.json file with require and the name of the composer package. The question is where to put it? We don't actually want this to load at the root of the package, it's Stripe API and we want it inside /src/payments/methods/stripe/api/.

Also wondering what else do we have to do? Does composer.json get found and read automatically by C5 or do we have to register it somehow? And where will the files actually get put? Do we need to make a /vendor folder or is it automatically created?

Would love to see a How To on this subject is anybody is up for that.

This is for C5.7 btw.

razorcommerce
View Replies: View Best Answer
MrKDilkington replied on at Permalink Best Answer Reply
MrKDilkington
@razorcommerce

I've been dealing with the same issue and just got it working.

This is the approach I used successfully.
https://www.concrete5.org/community/forums/5-7-discussion/add-3rdpar...

Here is an example using Stripe PHP -https://github.com/stripe/stripe-php...
1. Install Composer.
https://getcomposer.org/doc/00-intro.md#installation-linux-unix-osx...
https://getcomposer.org/doc/00-intro.md#installation-windows...
2. Create a composer.json file in your package root.
3. Open composer.json and paste this code into it to declare your dependencies. The dependencies are available on the project GitHub, Packagist, or project website.
{
  "require": {
    "stripe/stripe-php": "2.*"
  }
}

4. Run Composer to download the dependencies.
https://getcomposer.org/doc/00-intro.md#using-composer...
5. Once Composer finishes downloading, you will see a new folder called vendor and a new file called composer.lock.
composer.lock - This file locks the dependencies of your project to a known state. It is a list of the exact versions of the dependencies it installed in the vendor file.
vendor - The vendor folder contains all your dependencies, an autoload.php file, and a composer folder that contains additional autoload files.
6. In your package controller, above the class declaration, require the autoload.php file once.
require_once __DIR__ . '/vendor/autoload.php';

Now you should be able to create a new Stripe object in local scope and \Stripe object in global scope.

For anyone more experienced with this, If you see an error in my explanation, please reply and correct me.
razorcommerce replied on at Permalink Reply
razorcommerce
This is a great explanation that deserves to be turned into a How To. Still a bit unclear about step 4 having not done that before. If I'm understanding it correctly this is a "one-time" process? In other words, the package developer does this once, but the site developer who wants to use the package would not need to do this? Because I'd found some of these steps before and it seemed like we were requiring everybody who wants to use the package to have to open up a command line and start getting the assets. But the way you're describing it, it's more about getting that .lock file and the /vendor files "setup" on a one-time basis?

Anything you can add around that part would help. Thanks again and I'll take a shot at this and then add anything I find.
MrKDilkington replied on at Permalink Reply
MrKDilkington
@razorcommerce

Regarding step 4 and downloading, my only experience is with Windows. I have the Composer system path set for a global install. This allows me to run Composer from any command prompt. I open the folder with the composer.json file, open a command prompt from that folder, and run "composer install".

This looks like a good walk-through for Mac (which I believe is the same for Linux and Unix).
http://blog.teamtreehouse.com/dependency-management-in-php-using-co...

It can be a one time process, where you download it once and are done. The site developer doesn't have to do anything with it.

Optionally, Composer can be used again in the future to update the dependencies to the current version.
MrKDilkington replied on at Permalink Reply
MrKDilkington
@razorcommerce

I wanted to follow up and see if you were able to get Composer and your library working.
manup replied on at Permalink Reply
manup
I found how to run Composer inside the directory (https://github.com/thephpleague/omnipay-stripe)

First cd/navigate ( use CMD/Terminal ) to the directory where your composer.json file located, then apply the following commands.

$ curl -shttp://getcomposer.org/installer... | php
$ php composer.phar update

Once you have done, you will get a new directory(vendor) with all dependancies downloaded in it, you can then use the same directory for another project without running composer.

Cheers
manup replied on at Permalink Reply
manup
I found how to run Composer inside the directory (https://github.com/thephpleague/omnipay-stripe)

First cd/navigate ( use CMD/Terminal ) to the directory where your composer.json file located, then apply the following commands.

$ curl -shttp://getcomposer.org/installer... | php
$ php composer.phar update

Once you have done, you will get a new directory(vendor) with all dependancies downloaded in it, you can then use the same directory for another project without running composer.

Cheers