tacman/composer-json-manipulator

Package to load, merge and save composer.json file(s)

11.4 2023-12-03 20:42 UTC

This package is auto-updated.

Last update: 2024-05-04 01:01:03 UTC


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());
    }
}