mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-28 23:10:51 +00:00
6c20d257e3
d3d18d442c
keep relative paths
56 lines
1.8 KiB
PHP
56 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace RectorPrefix202209\Symplify\SmartFileSystem\Json;
|
|
|
|
use RectorPrefix202209\Nette\Utils\Arrays;
|
|
use RectorPrefix202209\Nette\Utils\Json;
|
|
use RectorPrefix202209\Symplify\SmartFileSystem\FileSystemGuard;
|
|
use RectorPrefix202209\Symplify\SmartFileSystem\SmartFileSystem;
|
|
/**
|
|
* @api
|
|
* @see \Symplify\SmartFileSystem\Tests\Json\JsonFileSystem\JsonFileSystemTest
|
|
*/
|
|
final class JsonFileSystem
|
|
{
|
|
/**
|
|
* @var \Symplify\SmartFileSystem\FileSystemGuard
|
|
*/
|
|
private $fileSystemGuard;
|
|
/**
|
|
* @var \Symplify\SmartFileSystem\SmartFileSystem
|
|
*/
|
|
private $smartFileSystem;
|
|
public function __construct(FileSystemGuard $fileSystemGuard, SmartFileSystem $smartFileSystem)
|
|
{
|
|
$this->fileSystemGuard = $fileSystemGuard;
|
|
$this->smartFileSystem = $smartFileSystem;
|
|
}
|
|
/**
|
|
* @return mixed[]
|
|
*/
|
|
public function loadFilePathToJson(string $filePath) : array
|
|
{
|
|
$this->fileSystemGuard->ensureFileExists($filePath, __METHOD__);
|
|
$fileContent = $this->smartFileSystem->readFile($filePath);
|
|
return Json::decode($fileContent, Json::FORCE_ARRAY);
|
|
}
|
|
/**
|
|
* @param array<string, mixed> $jsonArray
|
|
*/
|
|
public function writeJsonToFilePath(array $jsonArray, string $filePath) : void
|
|
{
|
|
$jsonContent = Json::encode($jsonArray, Json::PRETTY) . \PHP_EOL;
|
|
$this->smartFileSystem->dumpFile($filePath, $jsonContent);
|
|
}
|
|
/**
|
|
* @param array<string, mixed> $newJsonArray
|
|
*/
|
|
public function mergeArrayToJsonFile(string $filePath, array $newJsonArray) : void
|
|
{
|
|
$jsonArray = $this->loadFilePathToJson($filePath);
|
|
$newComposerJsonArray = Arrays::mergeTree($jsonArray, $newJsonArray);
|
|
$this->writeJsonToFilePath($newComposerJsonArray, $filePath);
|
|
}
|
|
}
|