tacman / composer-json-manipulator
Package to load, merge and save composer.json file(s)
Fund package maintenance!
tomasvotruba
www.paypal.me/rectorphp
Requires
- php: >=8.1
- nette/utils: ^3.2 || ^4.0
- symfony/config: ^6.3 | ^7.0
- symfony/dependency-injection: ^6.3 | ^7.0
- symfony/filesystem: ^6.3 | ^7.0
Requires (Dev)
- phpunit/phpunit: ^9.5.26
README
The original code for this repository is now marked as deprecated, I use this fork simply to avoid seeing the deprecation warning.
see https://github.com/deprecated-packages/composer-json-manipulator
- load to
composer.json
to an object - use handful methods
- merge it with others
- print it back to
composer.json
in human-like format
Install
composer require tacman/composer-json-manipulator
Add to your config/config.php
:
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; use Symplify\ComposerJsonManipulator\ValueObject\ComposerJsonManipulatorConfig; return static function (ContainerConfigurator $containerConfigurator): void { $containerConfigurator->import(ComposerJsonManipulatorConfig::FILE_PATH); };
Usage
namespace App; use Symplify\ComposerJsonManipulator\ComposerJsonFactory; class SomeClass { /** * @var ComposerJsonFactory */ private $composerJsonFactory; public function __construct(ComposerJsonFactory $composerJsonFactory) { $this->composerJsonFactory = $composerJsonFactory; } public function run(): void { // ↓ instance of \Symplify\ComposerJsonManipulator\ValueObject\ComposerJson $composerJson = $this->composerJsonFactory->createFromFilePath(getcwd() . '/composer.json'); // Add a PRS-4 namespace $autoLoad = $composerJson->getAutoload(); $autoLoad['psr-4']['Cool\\Stuff\\'] = './lib/'; $composerJson->setAutoload($autoLoad); $this->jsonFileManager->printComposerJsonToFilePath($composerJson, $composerJson->getFileInfo()->getRealPath()); } }