Newer
Older
<?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.
* 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->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))) {
try {
$jsonFile->write($this->content);
}
catch (\Exception $ex) {
// Let's ignore this, if composer.json is read-only there is a general problem.
}
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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] : [];
}
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* 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;
}
}