piotrpress/composer-formatter
Composer 安装命令:
composer require piotrpress/composer-formatter
包简介
This Composer plugin formats (sorts and normalizes) composer.json and other JSON files according to defined rules.
关键字:
README 文档
README
This Composer plugin formats (sorts and normalizes) composer.json and other JSON files according to defined rules.
It provides composer format command for formatting and checking files, as well as reusable GitHub Workflows for automated formatting in CI/CD pipelines.
Installation
Install the plugin as a development dependency in your project:
composer require --dev piotrpress/composer-formatter
Usage
composer format [<input>] [--output|-o [OUTPUT]] [--format|-f [FORMAT]] [--check|-c]
Arguments
input- Path to input JSON file (default:composer.jsonin current directory).
Options
--output|-o- Path to output JSON file. If flag provided without value, outputs to STDOUT (default: overwrite input JSON file).--format|-f- Path to config JSON file with formatting rules or inline JSON string. Falls back toextra.formatsection incomposer.jsonor default rules.--check|-c- Check if the file is properly formatted without making changes. Exits with code0if formatted,1otherwise.
Example
- Format
composer.jsonin place:
composer format
- Format a custom JSON file:
composer format path/to/custom.json
- Check if file is properly formatted:
composer format --check
- Print formatted output to STDOUT:
composer format --output
- Write formatted output to a different file:
composer format --output path/to/output.json
- Format file using custom formatting rules:
composer format --format='{"order":{"require": false}}'
Configuration
Formatting rules can be defined via --format|-f option or composer.json under the extra.format key. Format option always takes precedence over JSON key.
Configuration options
schema-path- Path to JSON schema used for key ordering (default: Composer schema).order-array- Custom order (default: empty array).breaker-cr,lf,crlf- Line breaker style (default:lf).indent.style-space,tab- Indentation style (default:space).indent.size-integer- Indentation size (default:4).
Default configuration
{
"extra": {
"format": {
"schema": "composer-schema.json",
"order": [],
"breaker": "lf",
"indent": {
"style": "space",
"size": 4
}
}
}
}
Custom order configuration
The order option accepts an object where each key is a dot-notation path or glob pattern pointing to a section of JSON file, and the value defines the sorting behavior for that section:
[ "key1", "key2", ... ]- An array of keys that specifies the exact order of keys in that section. Keys not listed will be sorted alphabetically after the specified keys.false- Disables sorting for that section, preserving the original order of keys.null- Sorts all keys in that section alphabetically.
Wildcard * in the path matches any array index (e.g. authors.* applies rules to every item in the authors array).
Example
{
"extra": {
"format": {
"order": {
"root": [ "name", "type", "description" ],
"authors.*": [ "name", "homepage" ],
"require": false,
"autoload.classmap": null
}
}
}
}
GitHub Workflows
This plugin also comes with two GitHub Reusable Workflows that can be used to automatically check and fix formatting of JSON files in your project on GitHub:
- format.yml - Automatically formats the file and commits the changes if needed.
- check.yml - Checks if the file is properly formatted and fails if not.
and additionally two ready-to-use workflow files that utilize the above reusable workflows to check and fix formatting of composer.json file in your project:
- composer-format.yml - Automatically formats
composer.jsonfile and commits the changes if needed. - composer-check.yml - Checks if
composer.jsonfile is properly formatted and fails if not.
Options for format.yml reusable workflow
input- Path to input JSON file (default:composer.jsonin current directory).output- Path to JSON file to save formatted content (default: overwrite input file).format- Path to JSON file or string with formatting rules (default: useextra.formatsection incomposer.jsonor default rules if not defined).message- Commit message for changes (default:chore: format composer.json).
Custom example usage of format.yml in your workflow
name: Composer format on: push: paths: - 'composer.json' pull_request: paths: - 'composer.json' workflow_dispatch: jobs: format: permissions: contents: write uses: piotrpress/composer-formatter/.github/workflows/format.yml@master with: input: composer.json output: composer.json format: '{"order":{"root":["name","description","type"]}}' message: 'Format composer.json file'
Options for check.yml reusable workflow
input- Path to input JSON file (default:composer.jsonin current directory).format- Path to JSON file or string with formatting rules (default: useextra.formatsection incomposer.jsonor default rules if not defined).
Custom example usage of check.yml in your workflow
name: Composer format check on: push: paths: - 'composer.json' pull_request: paths: - 'composer.json' workflow_dispatch: jobs: check: uses: piotrpress/composer-formatter/.github/workflows/check.yml@master with: input: composer.json format: '{"order":{"root":["name","description","type"]}}'
Recommendations
- Install the plugin as a separate tool in your project:
mkdir -p tools/composer-formatter && \ composer require piotrpress/composer-formatter \ --working-dir=tools/composer-formatter \ --no-plugins \ --no-interaction && \ composer config allow-plugins.piotrpress/composer-formatter true \ --working-dir=tools/composer-formatter \ --no-plugins
- Add a script to project's
composer.jsonfile for easy access:
{
"scripts": {
"format:fix": "@composer format ../../composer.json --working-dir=tools/composer-formatter",
"format:check": "@composer format ../../composer.json --working-dir=tools/composer-formatter --check"
}
}
Requirements
- PHP
>= 8.2 - Composer
^2.0
License
piotrpress/composer-formatter 适用场景与选型建议
piotrpress/composer-formatter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「plugin」 「linter」 「composer」 「order」 「command」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 piotrpress/composer-formatter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 piotrpress/composer-formatter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 piotrpress/composer-formatter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CakePHP 4.x AdminLTE Theme.
Kinikit - PHP Application development framework MVC component
ext-json wrapper with sane defaults
Lot's of tools for git, file and static source code analysis.
A package to cast json fields, each sub-keys is castable
Validate Blade templates for syntax errors, security issues, and best practices
统计信息
- 总下载量: 27
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 41
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-02