Introducer
I don’t need to introduce the composer management package too much. I think you all know it. Some time ago, I published the composer package to the intranet hosting platform. Today, I have time to try to publish it to packagist and sort out the records.
Preparation
The preparation is very simple. There are two points in total
- Create a new project in GitHub to manage the code of the composer package
- Register an account with packagist and use GitHub account
Development
Make a simple demo
- Clone the project in GitHub to local
- implement
composer init
Command to initialize. If there are other dependencies, you can configurerequire
andrequire-dev
parameter - It will be created after the previous step
composer.json
The document can also be modified directly if it needs to be modifiedcomposer.json
- Next, write a simple function to create
src/helpers.php
, the code is as follows
<?php
function init()
{
echo 'test composer';
}
- Finally, we need to
composer.json
The loading method is defined in. For the loading mechanism, see this article. Because demo is relatively simple, usefiles
That’s it, eventuallycomposer.json
as follows
{
"name": "test/test-composer",
"description": "test",
"type": "library",
"require": {
"php": "^7.2"
},
"license": "MIT",
"authors": [
{
"name": "test",
"email": "[email protected]"
}
],
"autoload": {
"files": [
"src/helpers.php"
]
}
}
Release
- Don’t forget the version number
git tag v1.0.0
- Push code to GitHub and tag to push
git push origin --tags
- Click submit on packagist platform, fill in the address of GitHub project, and click Check
- You can use it later
composer require
Install the package just released - Packagist will update automatically when the code is submitted to GitHub later. If there is no update, please refer to this article for configuration
epilogue
It’s a very simple demo, and it doesn’t writephpunit
, mainly to be familiar with the next process It is very important.
Reference: composer package development is so simple. Learn to develop your own composer package. Use GitHub to update to packagist in real time, and learn about composer autoload mechanism in depth.