Newer
Older
<?php /** @noinspection UnusedConstructorDependenciesInspection */
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
*/
private $composer;
* Content of the composer.json file.
private $content;
* The path of the directory of the composer.json file.
private $sourcePath;
/**
* The full filename of the composer.json file.
*
* @var string
*/
private $composerJsonPath;
/**
* @var string[]
*/
private static $ignoredGitPatterns;
/**
* 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 = $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(): self {
$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(): self {
$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.
}
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 $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 $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 $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): self {
$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): self {
$this->content[$key1][$key2] = $value;
return $this;
}
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* @param string $command
*/
public static function git($command) {
passthru(escapeshellcmd('git -c "user.email=composer@lakedrops.com" ' . $command));
}
/**
* @param string $pattern
*/
public static function gitIgnore($pattern) {
if (self::$ignoredGitPatterns === NULL) {
if (file_exists('.gitignore')) {
self::$ignoredGitPatterns = explode(PHP_EOL, file_get_contents('.gitignore'));
}
else {
self::$ignoredGitPatterns = [];
}
}
if (\in_array($pattern, self::$ignoredGitPatterns, TRUE)) {
return;
}
self::$ignoredGitPatterns[] = $pattern;
self::git('ignore ' . $pattern);
}