Table of Contents
初始化 composer.json 文件
项目结构
提交至 GitHub
提交至 Packagist
Home Backend Development PHP Tutorial 动手开发自己的第一个 composer 包

动手开发自己的第一个 composer 包

May 15, 2018 pm 04:56 PM

composer 是 PHP 的依赖管理工具,本篇文章就来说明如何构建一个包,并提交到 Packagist,这样别人就可以方便地通过 composer 使用你的包了。

开发 composer 包有以下几个步骤:

  1. 初始化 composer.json 文件

  2. 定义命名空间及包名

  3. 实现包需要实现的功能

  4. 提交到 GitHub

  5. 在 Packagist 注册包

初始化 composer.json 文件

安装好 composer 后即可在本地运行 composer init通过交互式命令行设置 composer.json 。

下面介绍其中的几个属性,以及常规的设置:

  1. name此属性定义包名,以 /隔开,前面的为供应商名字,后面为包名,供应商代表 Packagist 网站为开发者提供的唯一的名字,用来组织包以及防止命名冲突。所以提交时最好先访问 https://packagist.org/packages/yourvendorname将 url 中的 yourvendorname 替换为你想要取的名字,如果页面没有 404 ,说明已经被注册了。

  2. license许可证。关于许可证,建议看两篇文章, 开源项目 license 介绍、 如何选择 license

  3. require安装当前包所需的依赖。只有所有依赖被安装当前包才会被安装。

  4. autoload此配置下主要是 PSR-4或者 PSR-0设置,更推荐使用 PSR-4 标准。

http://json-schema.org/上介绍了 JSON Schema 的定义以及各个语言对其各种功能的实现,有 validator 的实现,其中 JSON Schema Validator是在线的验证服务。其实最简单的就是使用 composer validate composer.json来验证文件是否是有错误。

这是我演示的设置 composer.json 的视频

项目结构

以我开发的 单点登录 SDK为例,此项目基于 Laravel ,实现了站点接入单点登录系统的简单接入,应用只需在服务端注册并实现指定接口,即可接入 SSO 。

项目结构是典型的 MVC 结构,

.└── geo    └── geosso        ├── LICENSE        ├── README.md        ├── composer.json        └── src            ├── Contracts            ├── Http            │   ├── Controllers            │   ├── Middleware            │   └── Requests            ├── ParamsBean            ├── Providers            ├── Support            └── config12 directories
Copy after login

LICENSE、README.md 及 composer.json 是运行 tree -d之后手工添加上去的。

项目根目录定义在 src 下,在 composer.json 中也有定义,这样当 composer 加载这个包时就知道如何通过命名空间去解析文件路径。

Http 目录代表请求响应,之下的 Controllers 表示合法请求的控制器,Middleware 代表请求的第一道关卡,通过中间件去拦截请求,Requests 去获取前端请求并对请求过滤。

Contracts 代表接口定义。ParamsBean 代表应用层与底层服务沟通时的参数封装,通过 Bean 去获取各个参数,而不是传递 array 使得调用一致,并且强制接口调用时做类型检测,可以很大程度上统一各层之间的参数传递。

Providers 代表 Laravel 的服务容器,通过服务容器,可以注册路由与配置,加载助手类,绑定接口与其实现。

Support 就是一些助手类,对常用的与逻辑无关的功能的封装,config 代表应用自己的配置,通过 config 可以方便地将配置设置并使用全局函数 config()调用。

提交至 GitHub

按照前面的步骤,一个包就有了基本的骨架,接下来就是上传至 GitHub ,配置项目,集成持续集成服务,发布开源项目许可证。

GitHub 初始化项目时可以选择生成 .gitignore 文件,选择许可证,初始化 README.md 文件,切换至本地的项目目录后,按如下步骤即可将目录上传至 GitHub:

>git init # 初始化仓库>git remote set-url origin --push --add git@github.com:jayxhj/geosso.git # 添加远程追踪仓库地址> git add .> git commit> git push origin master
Copy after login

提交至 Packagist

Packagist 为 composer 默认获取包元数据信息的地址,从 Packagist 获取到元数据信息后,再从 GitHub 上拉取代码。因此,当把你开发的包上传至 GitHub 后还需要将其在 Packagist 注册,这样全世界的人都能通过 composer 去拉去你的代码了。

提交至 Packagist 只需三个步骤:

  1. 注册帐号

  2. 在 https://packagist.org/packages/submit提交开发包

  3. 设置 webhook 以便提交包更新后能及时地同步至 Packagist

自此,一个基本的包开发就结束了。通过 composer 来管理 PHP 的依赖,通过编写 composer package 去扩展自己的类库,通过引入其他的类库来填充自己的功能,就不用重复造轮子了。

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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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)

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

See all articles