How to handwrite a composer package and upload it for publication
1. About composer
Composer is a
dependency management tool
forPHP5.3
or above. It allows you to declare the code libraries your project depends on and it will install them for you in your project. With it, we can easilyuse a command
to reference other excellent codes into our project.
2. Dependency package management tool
java has maven
, and the front-end has npm
, Android has Gradle
, and as the best language in the world
also has composer
3. Composer Advantages
- You have a project that depends on several libraries.
- Some of these libraries depend on other libraries.
- You declare what you depend on.
- Composer will figure out which versions of packages need to be installed, and install them (download them into your project).
4. Common composer commands
-
composer require
:Install a software package -
composer update
:will detect the environment and dependencies required by the new version of the package and automatically implement the upgrade of the tool package -
composer install
:If there is no composer.json file, the dependencies in composer.json will be installed -
composer init
: will initialize a composer.json configuration file##5. Composer installation Refer to the novice tutorial
6. Handwrite a composer package
Create a new directory under the site namedblog.
1. Execute in the directory: composer init
After execution, let us enter a package name. Just call it
guangtouqiang/blog.
Package namename
The name of the package, which consists of the vendor name and the project name, separated by /.
- This is required for packages (libraries) that need to be published.
- For example, phpmailer/phpmailer
2. Execute guangtouqiang/blog:
Next let’s enter a description.
Descriptiondescription
A brief description of a package. Usually this is only one line at most.
- This is required for packages (libraries) that need to be published.
3. Enter the description this is guangtouqiang test bag
##4.
Enter author name (optional)
authors5. Enter the minimum stable versionThis object must contain all the following properties.
- name: The author's name, usually his real name.
- email: The author’s email address.
- homepage: The URL address of the author's homepage.
- role: The author's role in this project (e.g. developer or translator).
Available stability identifiers:
dev, alpha, beta, RC, stable., generally choose dev
.
Packet type
type
<ul> <li>包的安装类型,默认为<code>library。
library: 这是默认类型,它会简单的将文件复制到 vendor 目录。 project: 这表示当前包是一个项目,而不是一个库。例:框架应用程序 Symfony standard edition,内容管理系统 SilverStripe installer 或者完全成熟的分布式应用程序。使用 IDE 创建一个新的工作区时,这可以为其提供项目列表的初始化。 metapackage: 当一个空的包,包含依赖并且需要触发依赖的安装,这将不会对系统写入额外的文件。因此这种安装类型并不需要一个 dist 或 source。 composer-plugin: 一个安装类型为 composer-plugin 的包,它有一个自定义安装类型,可以为其它包提供一个 installler。详细请查看 自定义安装类型。
默认为 library
我们选择1,library
即可!
7、需要输入开源协议
选择输入GPL,LGPL,BSD,MIT,Apache
这些开源协议即可:
8、是否依赖于其它包,是否依赖于其它环境,这边都yes即可。
Search for a package
:选择搜索php。
9、生成成功。
此时,目录下已经有个composer
这些配置:
七、composer目录的修改
看下刚刚生成的composer.json
:
{ "name": "guangtouqiang/blog", "description": "this is guangtouqiang test bag", "type": "1", "require": { "php": "^7.0" }, "license": "Apache-2.0", "authors": [ { "name": "guangtouqiang", "email": "xiaozhengguo521@gmail.com" } ], "minimum-stability": "dev"}
再次基础上增加:
{ "name": "guangtouqiang/blog", "description": "this is guangtouqiang test bag", "type": "1", "require": { "php": "^7.0" }, "license": "Apache-2.0", "authors": [ { "name": "guangtouqiang", "email": "xiaozhengguo521@gmail.com" } ], "minimum-stability": "dev", "autoload": { "psr-4": { "guangtouqiang\\": "src/guangtouqiang" } }}
命令行执行:composer dump-autoload
,更新composer的命名空间与文件夹映射关系。
同样,需要新建一个src
和guangtouqiang
的文件夹:
在根目录新建个index.php
测试一波:
输出:
注意命名空间的层级与文件夹层级是否相同,类名与文件名相同才能自动加载。
八、将写好的composer包上传
1、在github上新建个仓库
2、将本地的composer包上传到新仓库
git init
git add .
git commit -m "xxx"
git remote add origin 你的远程仓库地址
git push -u origin master
3、登录https://packagist.org/packages/submit
4、将你github上的仓库地址复制进去:
5、配置webhook:
6、复制上图地址到github。
Addwebhook
:
At this time, the github warehouse and packagist have been associated. But it still needs to be released to the official version before it can be used.
7. Tag the version
git tag -a v1.0.1 -m "First Edition"
git push origin v1.0.1
At this point, the labeling has been successful, and all our versions can be seen from packagist.
Execute composer require xx/xxx
and it will be available for others to use.
9. Summary
For detailed description of composer.json, please refer to : Detailed use of composer.json
For more composer-related technical articles, please visit the composer tutorial column!
The above is the detailed content of How to handwrite a composer package and upload it for publication. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

I encountered a tricky problem when developing a new Laravel project: how to quickly build a fully functional and easy-to-manage content management system (CMS). I tried multiple solutions, but all gave up because of complex configuration and inconvenient maintenance. Until I discovered the LaravelCMS package mki-labs/espresso, which not only simple to install, but also provides powerful functions and intuitive management interface, which completely solved my problem.

I'm having a tough problem when developing a complex web application: how to effectively handle JavaScript errors and log them. I tried several methods, but none of them could meet my needs until I discovered the library dvasilenko/alterego_tools. I easily solved this problem through the installation of this library through Composer and greatly improved the maintainability and stability of the project. Composer can be learned through the following address: Learning address

In project development, it is often necessary to create command-line tools to simplify daily tasks or automate processes. However, creating a command line interface that is beautiful and easy to test is not easy. Recently, I encountered this problem while developing a project that requires command line tools. After some exploration, I found the Symfony/Console library, which greatly simplifies the creation process of command line interfaces.

I encountered a common but tricky problem when developing a large PHP project: how to effectively manage and inject dependencies. Initially, I tried using global variables and manual injection, but this not only increased the complexity of the code, it also easily led to errors. Finally, I successfully solved this problem by using the PSR-11 container interface and with the power of Composer.

During development, HTTP requests are often required, which may be to get data, send data, or interact with external APIs. However, when faced with complex network environments and changing request requirements, how to efficiently handle HTTP requests becomes a challenge. I have encountered a problem in a project: I need to send requests to different APIs frequently, and log the requests to facilitate subsequent debugging and analysis. After trying several methods, I discovered the yiche/http library. It not only simplifies the processing of HTTP requests, but also provides dynamic logging functions, greatly improving development efficiency.

When developing Yii framework projects, you often encounter situations where you need to obtain a large amount of data from the database. If appropriate measures are not taken, directly obtaining all data may cause memory overflow and affect program performance. Recently, when I was dealing with a project on a large e-commerce platform, I encountered this problem. After some research and trial, I finally solved the problem through the extension library of pavle/yii-batch-result.
