<?php namespace LakeDrops\Component\Composer; use Composer\Composer; use Composer\Json\JsonFile; /** * Manages composer.json files. */ final class Utils { /** * The composer object running this session. * * @var \Composer\Composer */ protected $composer; /** * Content of the composer.json file. * * @var array */ protected $content; /** * The path of the directory of the composer.json file. * * @var string */ protected $sourcePath; /** * The full filename of the composer.json file. * * @var string */ protected $composerJsonPath; /** * Utils constructor. * * @param \Composer\Composer $composer * The composer object running this session. * @param string|null $sourcePath * The path name if not the project's root directory. */ public function __construct(Composer $composer, $sourcePath = NULL) { $this->composer = $composer; $this->sourcePath = isset($sourcePath) ? $sourcePath : getcwd() . $composer->getPackage()->getTargetDir(); $this->composerJsonPath = $this->sourcePath . '/composer.json'; $this->read(); } /** * Read the current content of the project's composer.json file. * * @return $this */ public function read() { $this->content = []; if (file_exists($this->composerJsonPath)) { $jsonFile = new JsonFile($this->composerJsonPath); $this->content = $jsonFile->read(); } return $this; } /** * Write the config to composer.json file. * * @return $this */ public function write() { $jsonFile = new JsonFile($this->composerJsonPath); if (file_exists($this->composerJsonPath)) { $content = $jsonFile->read(); if (md5(json_encode($content)) === md5(json_encode($this->content))) { // Nothing has changed. return $this; } } try { $jsonFile->write($this->content); } catch (\Exception $ex) { // Let's ignore this, if composer.json is read-only there is a general problem. } return $this; } /** * Get settings for [$key]. * * @param string $key * The key for the section. * * @return array|string * The settings of the key. */ public function getSection($key) { return isset($this->content[$key]) ? $this->content[$key] : []; } /** * Get settings for [$key1][$key2]. * * @param string $key1 * The key for the section. * @param string $key2 * The key for the sub-section. * * @return array|string * The settings of the key. */ public function getSubSection($key1, $key2) { $section = $this->getSection($key1); return isset($section[$key2]) ? $section[$key2] : []; } /** * Get settings for [$key1][$key2][$key3]. * * @param string $key1 * The key for the section. * @param string $key2 * The key for the sub-section. * @param string $key3 * The key for the sub-sub-section. * * @return array|string * The settings of the key. */ public function getSubSubSection($key1, $key2, $key3) { $subSection = $this->getSubSection($key1, $key2); return isset($subSection[$key3]) ? $subSection[$key3] : []; } /** * Set settings for [$key]. * * @param string $key * The key for the section. * @param array|string $value * The value for the section. * * @return $this */ public function setSection($key, $value) { $this->content[$key] = $value; return $this; } /** * Set settings for [$key1][$key2]. * * @param string $key1 * The key for the section. * @param string $key2 * The key for the sub-section. * @param array|string $value * The value for the sub-section. * * @return $this */ public function setSubSection($key1, $key2, $value) { $this->content[$key1][$key2] = $value; return $this; } }