nadar / php-composer-reader
读取和操作 composer.json
2.0.0
2023-04-02 18:34 UTC
Requires
- php: >=8
- composer/semver: ^3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.15
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9
- rector/rector: ^0.15.23
README
A small PHP library for manipulating and reading the composer.json file. It allows you to add new sections, check if it's writable/readable, or retrieve information from the composer schema such as description, title, and more.
安装
通过 Composer 安装
composer require nadar/php-composer-reader
使用方法
将 composer.json 文件加载到 ComposerReader 中
require 'vendor/autoload.php'; $reader = new ComposerReader('path/to/composer.json'); if (!$reader->canRead()) { throw new Exception("Unable to read the JSON file."); } if (!$reader->canWrite()) { throw new Exception("Unable to write to the JSON file."); } // Dump the full content var_dump($reader->getContent());
读取部分数据
检索 composer.json 文件中 require
部分中每个软件包的对象数组
$reader = new ComposerReader('path/to/composer.json'); $section = new RequireSection($reader); foreach ($section as $package) { echo $package->name . ' with ' . $package->constraint; // Check if the package version is greater than a given version constraint. if ($package->greaterThan('^6.5')) { echo "Numerous releases available!"; } }
检索 composer.json 文件中 autoload
部分中每个 PSR 定义的对象数组
$reader = new ComposerReader('path/to/composer.json'); $section = new AutoloadSection($reader, AutoloadSection::TYPE_PSR4); foreach ($section as $autoload) { echo $autoload->namespace . ' with ' . $autoload->source; }
以下部分读取器可用于 composer 架构(Composer 架构文档)
可以通过 ComposerReader 对象检索附加架构信息:$reader->contentSection('extra', null);
更改部分数据
向现有的 composer.json 文件添加新的 PSR 自动加载定义并保存它
$reader = new ComposerReader('path/to/composer.json'); // Generate a new autoload section object $new = new Autoload($reader, 'Foo\\Bar\\', 'src/foo/bar', AutoloadSection::TYPE_PSR4); // Store the new autoload object in the autoload section and save $section = new AutoloadSection($reader); $section->add($new)->save();
运行命令
要执行 composer 操作,请使用 runCommand()
方法
$reader = new ComposerReader('path/to/composer.json'); $reader->runCommand('dump-autoload'); // This is equivalent to running `composer dump-autoload`
这尝试执行指定 composer.json 文件的 dump-autoload 命令。 这需要在您的系统上全局安装 Composer 命令(全局安装 Composer)。