Table of Contents
6. Handwrite a composer package" >6. Handwrite a composer package
Home Development Tools composer How to handwrite a composer package and upload it for publication

How to handwrite a composer package and upload it for publication

Jul 19, 2021 pm 02:32 PM
composer

1. About composer

Composer is a dependency management tool for PHP5.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 easily use 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 requireInstall a software package
    How to handwrite a composer package and upload it for publication
  • composer updatewill detect the environment and dependencies required by the new version of the package and automatically implement the upgrade of the tool package
    How to handwrite a composer package and upload it for publication
  • composer installIf there is no composer.json file, the dependencies in composer.json will be installed
    How to handwrite a composer package and upload it for publication
  • composer init : will initialize a composer.json configuration file
    How to handwrite a composer package and upload it for publication

    ##5. Composer installation Refer to the novice tutorial

Create a new directory under the site named

blog.

1. Execute in the directory: composer init

How to handwrite a composer package and upload it for publicationAfter execution, let us enter a package name. Just call it
guangtouqiang/blog.

Package name

name

    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:

How to handwrite a composer package and upload it for publication Next let’s enter a description.

Description

description

    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

How to handwrite a composer package and upload it for publication##4.
Enter author name (optional)

How to handwrite a composer package and upload it for publication

Authors
authors

This 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).
5. Enter the minimum stable version

Available stability identifiers:

dev, alpha, beta, RC, stable.

, generally choose dev.

6. Enter the packet type

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即可!

    How to handwrite a composer package and upload it for publication
    7、需要输入开源协议

    选择输入GPL,LGPL,BSD,MIT,Apache这些开源协议即可:
    How to handwrite a composer package and upload it for publication
    8、是否依赖于其它包,是否依赖于其它环境,这边都yes即可。

    Search for a package:选择搜索php。

    How to handwrite a composer package and upload it for publication
    9、生成成功。
    How to handwrite a composer package and upload it for publication
    此时,目录下已经有个composer这些配置:
    How to handwrite a composer package and upload it for publication

    七、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的命名空间与文件夹映射关系。

    How to handwrite a composer package and upload it for publication
    同样,需要新建一个srcguangtouqiang的文件夹:
    How to handwrite a composer package and upload it for publication
    在根目录新建个index.php测试一波:
    How to handwrite a composer package and upload it for publication
    输出:
    How to handwrite a composer package and upload it for publication

    注意命名空间的层级与文件夹层级是否相同,类名与文件名相同才能自动加载。

    八、将写好的composer包上传

    1、在github上新建个仓库
    How to handwrite a composer package and upload it for publication
    2、将本地的composer包上传到新仓库

    • git init
    • git add .
    • git commit -m "xxx"
    • git remote add origin 你的远程仓库地址
    • git push -u origin master

    How to handwrite a composer package and upload it for publication
    3、登录https://packagist.org/packages/submit
    How to handwrite a composer package and upload it for publication
    4、将你github上的仓库地址复制进去:

    How to handwrite a composer package and upload it for publication
    5、配置webhook:
    How to handwrite a composer package and upload it for publication
    How to handwrite a composer package and upload it for publication

    6、复制上图地址到github。

    Addwebhook:
    How to handwrite a composer package and upload it for publication
    How to handwrite a composer package and upload it for publication

    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.

    How to handwrite a composer package and upload it for publication

    Execute composer require xx/xxx and it will be available for others to use.

    9. Summary

    How to handwrite a composer package and upload it for publication
    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!

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    PHP Tutorial
    1517
    276
    How does Composer manage dependencies in a PHP project, and what is the role of composer.json and composer.lock? How does Composer manage dependencies in a PHP project, and what is the role of composer.json and composer.lock? Jun 14, 2025 am 12:31 AM

    ComposermanagesdependenciesinPHPprojectsbylettingyoudeclarerequiredlibrarieswithversionconstraintsincomposer.json,whilecomposer.lockrecordsexactinstalledversions.1.composer.jsondefinesprojectmetadataanddependencieswithversionranges(e.g.,"monolog

    PHP calls AI intelligent voice assistant PHP voice interaction system construction PHP calls AI intelligent voice assistant PHP voice interaction system construction Jul 25, 2025 pm 08:45 PM

    User voice input is captured and sent to the PHP backend through the MediaRecorder API of the front-end JavaScript; 2. PHP saves the audio as a temporary file and calls STTAPI (such as Google or Baidu voice recognition) to convert it into text; 3. PHP sends the text to an AI service (such as OpenAIGPT) to obtain intelligent reply; 4. PHP then calls TTSAPI (such as Baidu or Google voice synthesis) to convert the reply to a voice file; 5. PHP streams the voice file back to the front-end to play, completing interaction. The entire process is dominated by PHP to ensure seamless connection between all links.

    What is Packagist, and what role does it play in Composer? What is Packagist, and what role does it play in Composer? Jun 25, 2025 am 12:04 AM

    Packagist is Composer's default package repository for centralized management and discovery of PHP packages. It stores the metadata of the package instead of the code itself, allowing developers to define dependencies through composer.json and get the code from the source (such as GitHub) at installation time. Its core functions include: 1. Provide centralized package browsing and search; 2. Manage versions to meet dependency constraints; 3. Automatic updates are achieved through webhooks. While custom repositories can be configured to use Composer, Packagist simplifies the distribution process of public packages. The publishing package needs to be submitted to Packagist and set up a webhook, so that others can install it with one click through composerrequire.

    How do I view information about a specific package using Composer? (composer show) How do I view information about a specific package using Composer? (composer show) Jun 21, 2025 am 12:02 AM

    To quickly get detailed information about a specific package in Composer, use the composershowvendor/package command. For example, composershowmonolog/monolog, which will display version, description, dependencies and other information; if you are not sure of the name, you can use some names to combine --platform to view platform requirements; add --name-only to simplify output; use -v to display more detailed content; support wildcard search, such as monolog/*.

    What are some best practices for using Composer in production environments? What are some best practices for using Composer in production environments? Jul 08, 2025 am 01:00 AM

    When using Composer in a production environment, you need to pay attention to safety, stability and performance. 1. Use composerinstall-no-dev to reduce unnecessary development dependencies and reduce online environment risks; 2. Always submit and rely on composer.lock files to ensure version consistency, and avoid using updates during deployment; 3. Optional configuration platform-check=false ignores platform differences warnings, which is suitable for building packaging scenarios; 4. Enable APCU to accelerate automatic loading to improve performance, especially suitable for high concurrency services, while paying attention to namespace uniqueness to avoid cache conflicts.

    How do I install Composer on my operating system (Windows, macOS, Linux)? How do I install Composer on my operating system (Windows, macOS, Linux)? Jul 01, 2025 am 12:15 AM

    Installing Composer takes only a few steps and is suitable for Windows, macOS, and Linux. Windows users should download Composer-Setup.exe and run it to ensure that PHP is installed or XAMPP is used; macOS users need to execute download, verification, and global installation commands through the terminal; Linux users operate similarly to macOS, and then use the corresponding package manager to install PHP and download and move the Composer file to the global directory.

    How do I manage environment-specific configurations with Composer? How do I manage environment-specific configurations with Composer? Jun 22, 2025 am 12:08 AM

    Managing environment configuration in PHP projects can be achieved in a variety of ways. First, use the .env file of the Dotenv library to create configuration files for different environments such as .env.development and .env.production, and load them through vlucas/phpdotenv, and submit the sample files and ignore the real files; second, store non-sensitive metadata in the extra part of composer.json, such as cache time and log levels for script reading; third, maintain independent configuration files such as config/development.php for different environments, and load the corresponding files according to the APP_ENV variable at runtime; finally, use CI/C

    How to use PHP to combine AI to generate image. PHP automatically generates art works How to use PHP to combine AI to generate image. PHP automatically generates art works Jul 25, 2025 pm 07:21 PM

    PHP does not directly perform AI image processing, but integrates through APIs, because it is good at web development rather than computing-intensive tasks. API integration can achieve professional division of labor, reduce costs, and improve efficiency; 2. Integrating key technologies include using Guzzle or cURL to send HTTP requests, JSON data encoding and decoding, API key security authentication, asynchronous queue processing time-consuming tasks, robust error handling and retry mechanism, image storage and display; 3. Common challenges include API cost out of control, uncontrollable generation results, poor user experience, security risks and difficult data management. The response strategies are setting user quotas and caches, providing propt guidance and multi-picture selection, asynchronous notifications and progress prompts, key environment variable storage and content audit, and cloud storage.

    See all articles