Composer

Install Composer in Project Root

Composer could be stored at various levels on your computer. Also there is the question of whether one is installing it and using it on your development server or on your production server. Even if you install it on both, it is likely to be best to independently install it on both and install packages independently on both using composer. It is not a good idea to install composer into your project on your local machine and install packages and then upload the project including the installed composer packages. Also, if version control such as git is being used, do not commit Composer's vendor directory. I think you should only commit composer.json and composer.lock??? with your project files.

The approach taken here is taken from the Composer download page. Create a directory for your project, install Composer and run init. The following, copy and pasted from my terminal, may look impenetrable but is exactly that.



steve:~/Desk...ples/stripe-payment$ mkdir client-server-stripe
steve:~/Desk...ples/stripe-payment$ cd client-server-stripe/
steve:~/Desk...ples/stripe-payment/client-server-stripe$ clear
steve:~/Desk...ples/stripe-payment/client-server-stripe$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
steve:~/Desk...ples/stripe-payment/client-server-stripe$ php -r "if (hash_file('sha384', 'composer-setup.php') === 'e5325b19b381bfd88ce90a5ddb7823406b2a38cff6bb704b0acc289a09c8128d4a8ce2bbafcd1fcbdc38666422fe2806') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
Installer verified
steve:~/Desk...ples/stripe-payment/client-server-stripe$ php composer-setup.php
All settings correct for using Composer
Downloading...

Composer (version 1.10.8) successfully installed to: /home/steve/Desktop/web/sserv/sserv-websites/stevespages.org.uk/public/htdocs/code-examples/stripe-payment/client-server-stripe/composer.phar
Use it: php composer.phar

steve:~/Desk...ples/stripe-payment/client-server-stripe$ ls
composer.phar  composer-setup.php
steve:~/Desk...ples/stripe-payment/client-server-stripe$ php -r "unlink('composer-setup.php');"
PHP Warning:  unlink(composer-setup.php): No such file or directory in Command line code on line 1
steve:~/Desk...ples/stripe-payment/client-server-stripe$ ls
composer.phar
steve:~/Desk...ples/stripe-payment/client-server-stripe$ php composer.phar init

                                            
  Welcome to the Composer config generator  
                                            


This command will guide you through creating your composer.json config.

Package name (/) [steve/client-server-stripe]: 
Description []: Learning About Stripe Integration                                                                
Author [Stephen Greig , n to skip]: 
Minimum Stability []: 
Package Type (e.g. library, project, metapackage, composer-plugin) []: project
License []: 

Define your dependencies.

Would you like to define your dependencies (require) interactively [yes]?  
Search for a package: 
Would you like to define your dev dependencies (require-dev) interactively [yes]? n

{
    "name": "steve/client-server-stripe",
    "description": "Learning About Stripe Integration",
    "type": "project",
    "authors": [
        {
            "name": "Stephen Greig",
            "email": "greigsteve@gmail.com"
        }
    ],
    "require": {}
}

Do you confirm generation [yes]? y
steve:~/Desk...ples/stripe-payment/client-server-stripe$ ls
composer.json  composer.phar

    

Add a Package to our Project

If we look at the composer.json file displayed when we ran the php composer.phar init command we can see that there are no packages in "require": {}. Let us install stripe-php by running the command: php composer.phar require stripe/stripe-php. The output:


steve:~/Desktop/web/sserv/sserv-websites/stevespages.org.uk/public/htdocs/code-examples/stripe-payment/client-server-stripe$ php composer.phar require stripe/stripe-php
Using version ^7.40 for stripe/stripe-php
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Installation request for stripe/stripe-php ^7.40 -> satisfiable by stripe/stripe-php[v7.40.0].
    - stripe/stripe-php v7.40.0 requires ext-curl * -> the requested PHP extension curl is missing from your system.

  To enable extensions, verify that they are enabled in your .ini files:
    - /etc/php/7.2/cli/php.ini
    - /etc/php/7.2/cli/conf.d/10-mysqlnd.ini
    - /etc/php/7.2/cli/conf.d/10-opcache.ini
    - /etc/php/7.2/cli/conf.d/10-pdo.ini
    - /etc/php/7.2/cli/conf.d/20-calendar.ini
    - /etc/php/7.2/cli/conf.d/20-ctype.ini
    - /etc/php/7.2/cli/conf.d/20-exif.ini
    - /etc/php/7.2/cli/conf.d/20-fileinfo.ini
    - /etc/php/7.2/cli/conf.d/20-ftp.ini
    - /etc/php/7.2/cli/conf.d/20-gettext.ini
    - /etc/php/7.2/cli/conf.d/20-iconv.ini
    - /etc/php/7.2/cli/conf.d/20-json.ini
    - /etc/php/7.2/cli/conf.d/20-mysqli.ini
    - /etc/php/7.2/cli/conf.d/20-pdo_mysql.ini
    - /etc/php/7.2/cli/conf.d/20-phar.ini
    - /etc/php/7.2/cli/conf.d/20-posix.ini
    - /etc/php/7.2/cli/conf.d/20-readline.ini
    - /etc/php/7.2/cli/conf.d/20-shmop.ini
    - /etc/php/7.2/cli/conf.d/20-sockets.ini
    - /etc/php/7.2/cli/conf.d/20-sysvmsg.ini
    - /etc/php/7.2/cli/conf.d/20-sysvsem.ini
    - /etc/php/7.2/cli/conf.d/20-sysvshm.ini
    - /etc/php/7.2/cli/conf.d/20-tokenizer.ini
  You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.

Installation failed, reverting ./composer.json to its original content.
steve:~/Desktop/web/sserv/sserv-websites/stevespages.org.uk/public/htdocs/code-examples/stripe-payment/client-server-stripe$
    

Oh dear! That failed because I have not got the curl extension enabled in the PHP programme running on my local machine. Rather than enable that extension, I will install Composer in the root directory for this project on my production server (hosted with Bytemark) and install the stripe-php package there. First I used SSH to access the server and then ran through the same steps as described above. I have copied the output from this terminal session and pasted it here:


Last login: Mon Jul  6 21:09:35 2020 from 82.71.43.56
admin@sserv:~$ php -v
PHP 7.0.33-0+deb9u8 (cli) (built: Jul  5 2020 06:34:50) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.33-0+deb9u8, Copyright (c) 1999-2017, by Zend Technologies
admin@sserv:~$ cd stevespages.org.uk/public/htdocs/code-examples/composer/
admin@sserv:~/stevespages.org.uk/public/htdocs/code-examples/composer$ clear
admin@sserv:~/stevespages.org.uk/public/htdocs/code-examples/composer$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
admin@sserv:~/stevespages.org.uk/public/htdocs/code-examples/composer$ php -r "if (hash_file('sha384', 'composer-setup.php') === 'e5325b19b381bfd88ce90a5ddb7823406b2a38cff6bb704b0acc289a09c8128d4a8ce2bbafcd1fcbdc38666422fe2806') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
Installer verified
admin@sserv:~/stevespages.org.uk/public/htdocs/code-examples/composer$ php composer-setup.php
All settings correct for using Composer
Downloading...

Composer (version 1.10.8) successfully installed to: /srv/stevespages.org.uk/public/htdocs/code-examples/composer/composer.phar
Use it: php composer.phar

admin@sserv:~/stevespages.org.uk/public/htdocs/code-examples/composer$ php -r "unlink('composer-setup.php');"
admin@sserv:~/stevespages.org.uk/public/htdocs/code-examples/composer$ ls
composer.phar  index.php
admin@sserv:~/stevespages.org.uk/public/htdocs/code-examples/composer$ php composer.phar require stripe/stripe-php
Using version ^7.40 for stripe/stripe-php
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing stripe/stripe-php (v7.40.0): Downloading (100%)         
Writing lock file
Generating autoload files
admin@sserv:~/stevespages.org.uk/public/htdocs/code-examples/composer$ ls
composer.json  composer.lock  composer.phar  index.php	vendor
admin@sserv:~/stevespages.org.uk/public/htdocs/code-examples/composer$
    

You can see from the output of the final ls command that I now have the following files and directories related to Composer: composer.json, composer.lock, composer.phar and vendor/.

Without init

If you install Composer using the 4 step method from the project's Download page, you do not have to then run the php composer.phar init command. You can, taking stripe/stripe-php as an example, run php composer.phar require stripe/stripe-php. This well create composer.json, composer.lock and vendor/.

Workflow

From now on I will continue to use the project root (client-server-stripe) on my local machine for writing files to and storing files in that are required for the project. When I want to run the web application I will transfer them to the client-server-stripe directory on my remote server where I have Composer and any necessary Composer packages such as stripe-php