posternak/composer-file
Composer 安装命令:
composer require posternak/composer-file
包简介
The joyful way to programmatically read and edit composer.json and composer.lock in PHP — drop it into any script or automation.
README 文档
README
The joyful way to programmatically read and edit composer.json and composer.lock in PHP — drop it into any script or automation.
Requirements
- PHP 8.2+
Installation
composer require posternak/composer-file
Usage
The package ships two classes — one for each Composer file.
ComposerJsonFile — read and edit composer.json
Given a composer.json like:
{
"name": "acme/app",
"require": {
"php": "^8.2",
"laravel/framework": "^12.8.1",
"thecodingmachine/safe": "^3.0.2"
},
"require-dev": {
"phpunit/phpunit": "^12.1.2"
}
}
You can read, add, update, remove and iterate over its dependencies:
use Posternak\ComposerFile\ComposerJsonFile; $file = new ComposerJsonFile('/path/to/composer.json'); // Read a constraint from either `require` or `require-dev` $file->getPackageVersionConstraint('laravel/framework'); // "^12.8.1" $file->getPackageVersionConstraint('phpunit/phpunit'); // "^12.1.2" $file->getPackageVersionConstraint('vendor/missing'); // throws — guard with has() if unsure // Update an existing constraint $file->setPackageVersionConstraint('laravel/framework', '^12.9.0'); // Add a new dependency $file->addPackage('symfony/console', '^7.2'); $file->addPackage('mockery/mockery', '^1.6', dev: true); // Remove a dependency (no-op if it's not there) $file->removePackage('thecodingmachine/safe'); // Iterate dependencies as a name => constraint map foreach ($file->getRequire() as $name => $constraint) { echo "$name => $constraint\n"; } foreach ($file->getRequireDev() as $name => $constraint) { // ... }
A few rules to keep in mind:
setPackageVersionConstraint()updates only — it throws if the package isn't already declared inrequireorrequire-dev. UseaddPackage()for new ones.addPackage()adds only — it throws if the package is already declared anywhere. UsesetPackageVersionConstraint()to change an existing constraint.removePackage()is idempotent — calling it on a package that isn't declared is a no-op.
ComposerJsonFile extends Posternak\JsonFile\JsonFile, so you also get the generic has(), get(), set(), remove() and save() methods on it — handy for poking at any other field in the file (autoload, scripts, config, …).
ComposerLockFile — read installed versions from composer.lock
use Posternak\ComposerFile\ComposerLockFile; $lock = new ComposerLockFile('/path/to/composer.lock'); // Installed version of a single package $lock->getInstalledPackageVersion('laravel/framework'); // "v12.8.1" $lock->getInstalledPackageVersion('vendor/missing'); // throws — package not installed // Full lock entry — everything composer.lock records for the package $lock->getPackageInfo('laravel/framework'); // => ["name" => "laravel/framework", "version" => "v12.8.1", "type" => "library", ...] // Walk the lock file — all installed, just runtime, or just dev $lock->getInstalledPackages(); // packages + packages-dev $lock->getInstalledRuntimePackages(); // packages only $lock->getInstalledDevPackages(); // packages-dev only
Both getInstalledPackageVersion() and getPackageInfo() throw RuntimeException if the package isn't installed.
Example: bump every package under a vendor prefix
A short script that updates every laravel/* constraint in a project's composer.json:
use Posternak\ComposerFile\ComposerJsonFile; $file = new ComposerJsonFile($argv[1] ?? __DIR__ . '/composer.json'); foreach ($file->getRequire() as $name => $constraint) { if (str_starts_with($name, 'laravel/')) { $file->setPackageVersionConstraint($name, '^12.9.0'); } }
The same shape works for any "for each package, do X" automation — audits, batch upgrades, CI lockstep checks across multiple repos.
License
Released under the MIT License.
posternak/composer-file 适用场景与选型建议
posternak/composer-file 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 06 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「composer」 「automation」 「composer-json」 「composer-lock」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 posternak/composer-file 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 posternak/composer-file 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 posternak/composer-file 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Command-line utility for Vtiger CRM.
Historical accounting for contacts
This composer plugin enables installation of GravityForms WordPress plugin and its addons.
Extends Mautic Lead Bundle's Lead List (Segment) functionality.
Simple ASCII output of array data
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 33
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-06-10