Creating first composer package to packagist

URLs to note

Composer Commands Ref, Packagist, Github,
My Sample(osolutils/helpers)

Submit to Packagist References

https://www.youtube.com/watch?v=B2DFoO_CMwQ
https://github.com/rakibtg/PHP-random-quotes-generator

composer require  rakibtg/php-random-quotes-generator

After install , check the file vendor/composer/autoload_ps4.php to see the mapping

Another reference
https://blog.jgrossi.com/2013/creating-your-first-composer-packagist-package/

Steps

Add PHP Project to Composer Packagist

Ensure that the classes are stored in a folder named "src"

To install without command line there is a service called PHP Download

  1. create a git repository of the files you want to upload to composer packagist

    1. Login to Github
    2. Create a new repository
    3. Clone to PC
    4. Copy needed files to the clone.Ensure that the classes are stored in a folder named "src"
    5. commit
    6. push
  2. Create composer.json

    composer init

    It will prompt for

    1. Package Name. Default will be fine. or may some meaning full name . eg : osolutils/helpers
    2. Description. Add a Description
    3. Author
    4. Minimum Stability. Must be empty or one of: stable, RC, beta, alpha, dev
    5. Package Type(library, project,metapackage, composer-plugin): libary if class,project if it is a full blown framework/cms
    6. License : MIT
    7. Would you like to define your dependencies (require) interactively [yes]? no
    8. Would you like to define your dev dependencies (require-dev) interactively [yes]? no
    9. Add PSR-4 autoload mapping? Maps namespace "Osolutils\Helpers" to the entered relative path. [src/, n to skip]:src/
      after saving correct it to "OSOLHelpers\\": "src/". because 'OSOLHelpers' is the correct namespace I used in my classes
    10. Would you like the vendor directory added to your .gitignore [yes]?yes

Hit Enter
It will Prompt Do you confirm generation?. Type yes
composer.json will be generated

  1. If not auto added, Add autoload property in composer.json above "require": {}
,
"autoload": {
        "psr-4": {
            "OSOLHelpers\\": "src/"
        }
    },
    "require": {
        "osolutils/helpers":"<release tag. This is to be set in github>"
    }
  1. git status

  2. git add .
    git commit -m 'Composer file added'
    git push origin main
  3. login at http://www.packagist.org

  4. Hit "Submit" Button

  5. In the form that appears,

    1. Enter repository URL
    2. Click Submit
  6. Setup Github Service Hook, so that packagist is updated when ever you push

    1. Old Method
      1. Get Packist API Token .Click "Show API Token"
      2. Login to Github
      3. Click "Settings" Tab
      4. Click "Integrations & Services" in the left menu.
      5. Select Packagist from the dropdown in the right.
      6. Enter user name of packagist
      7. Enter API Token got in Step 1
      8. Leave "Domain"
      9. Keep "Active" selected
      10. Click "Add Service"
    2. New Method https://blog.programster.org/creating-a-github-webhook-for-packagist
      1. Go to your Github project's settings>> webhooks. E.g: https://github.com/github-username/my-repo-name/settings/hooks
        ie https://github.com/osolgithub/OSOLHelpers/settings/hooks
      2. Click "Add Webhook" Button in the top right
      3. Set content type to application/json
      4. Enter Payload URL. Eg https://packagist.org/api/update-package?username=[USERNAME]
        ie https://packagist.org/api/update-package?username=osolgithub
      5. Click "Add Webhook" Button in the bottom
  7. Create a new release
    1.Click on "releases" in git hub

    1. Click "Draft New Release"
    2. Enter release details
    3. Tag Version
    4. Title
    5. Description
  8. Go back to packagist, click update. It will show release addded

  9. Your Library is ready

  10. Copy the install code shown in packagist site (eg: composer require...)

  11. Try it in command line

    composer require osolutils/helpers
    composer remove osolutils/helpers
    composer reinstall osolutils/helpers
    composer update
  12. Error

    [InvalidArgumentException]
    Could not find a version of package osolutils/helpers matching your minimum-stability (stable). Require it with an
    explicit version constraint allowing its desired stability.

    Solution:
    Add a release

  13. https://www.w3resource.com/php/composer/how-to-use-composer-installed-packages.php

  14. Updating

    1. edit composer.json require <new version> and commit
      ,
      "require": {
      "osolutils/helpers":"<new version>"
      }
    2. in github setup new release with as tag
    3. edit main composer.json , search for 'osolutils/helpers' under "require", edit to "osolutils/helpers": "^<new version>"
    4. composer update
  15. Adding dependency of another composer library

    1. edit composer.json add the library in require and also change version require <new version> and commit
      ,
      "require": {
      "osolutils/helpers":"^<new version>",     
      "phpmailer/phpmailer":"*"
      }
    2. Perform Steps 2 to 4 of above "Updating"

Go back to 'Steps'