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
-
create a git repository of the files you want to upload to composer packagist
- Login to Github
- Create a new repository
- Clone to PC
- Copy needed files to the clone.Ensure that the classes are stored in a folder named "src"
- commit
- push
-
Create composer.json
composer init
It will prompt for
- Package Name. Default will be fine. or may some meaning full name . eg :
osolutils/helpers
- Description. Add a Description
- Author
- Minimum Stability. Must be empty or one of: stable, RC, beta, alpha, dev
- Package Type(library, project,metapackage, composer-plugin):
libary
if class,project
if it is a full blown framework/cms - License : MIT
- Would you like to define your dependencies (require) interactively [yes]? no
- Would you like to define your dev dependencies (require-dev) interactively [yes]? no
- 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 - Would you like the vendor directory added to your .gitignore [yes]?yes
- Package Name. Default will be fine. or may some meaning full name . eg :
Hit Enter
It will Prompt Do you confirm generation?
. Type yes
composer.json will be generated
- 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>"
}
-
git status
-
git add . git commit -m 'Composer file added' git push origin main
-
login at http://www.packagist.org
-
Hit "Submit" Button
-
In the form that appears,
- Enter repository URL
- Click Submit
-
Setup
Github Service Hook
, so that packagist is updated when ever you push- Old Method
- Get Packist API Token .Click "Show API Token"
- Login to Github
- Click "Settings" Tab
- Click "Integrations & Services" in the left menu.
- Select Packagist from the dropdown in the right.
- Enter user name of packagist
- Enter API Token got in Step 1
- Leave "Domain"
- Keep "Active" selected
- Click "Add Service"
- New Method https://blog.programster.org/creating-a-github-webhook-for-packagist
- 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 - Click "Add Webhook" Button in the top right
- Set
content type
toapplication/json
- Enter Payload URL. Eg https://packagist.org/api/update-package?username=[USERNAME]
ie https://packagist.org/api/update-package?username=osolgithub - Click "Add Webhook" Button in the bottom
- Go to your Github project's settings>> webhooks. E.g: https://github.com/github-username/my-repo-name/settings/hooks
- Old Method
-
Create a new release
1.Click on "releases" in git hub- Click "Draft New Release"
- Enter release details
- Tag Version
- Title
- Description
-
Go back to packagist, click update. It will show release addded
-
Your Library is ready
-
Copy the install code shown in packagist site (eg:
composer require
...) -
Try it in command line
composer require osolutils/helpers composer remove osolutils/helpers composer reinstall osolutils/helpers composer update
-
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 -
https://www.w3resource.com/php/composer/how-to-use-composer-installed-packages.php
-
Updating
- edit composer.json
require <new version>
and commit, "require": { "osolutils/helpers":"<new version>" }
- in github setup new release with
as tag - edit main composer.json , search for 'osolutils/helpers' under "require", edit to
"osolutils/helpers": "^<new version>"
composer update
- edit composer.json
-
Adding dependency of another composer library
- edit composer.json add the library in require and also change version
require <new version>
and commit, "require": { "osolutils/helpers":"^<new version>", "phpmailer/phpmailer":"*" }
- Perform
Steps 2 to 4
of above "Updating"
- edit composer.json add the library in require and also change version
|
|