承接 piotrpress/composer-formatter 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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.json in 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 to extra.format section in composer.json or default rules.
  • --check|-c - Check if the file is properly formatted without making changes. Exits with code 0 if formatted, 1 otherwise.

Example

  • Format composer.json in 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.json file and commits the changes if needed.
  • composer-check.yml - Checks if composer.json file is properly formatted and fails if not.

Options for format.yml reusable workflow

  • input - Path to input JSON file (default: composer.json in 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: use extra.format section in composer.json or 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.json in current directory).
  • format - Path to JSON file or string with formatting rules (default: use extra.format section in composer.json or 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

  1. 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
  1. Add a script to project's composer.json file 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

MIT

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 piotrpress/composer-formatter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 27
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 41
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-02