Skip to content

Commit 6f5fb43

Browse files
committed
Add JsonVersionProvider
1 parent b39d4b5 commit 6f5fb43

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

app/Repository/VersionProviderFactory.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Repository;
44

55
use App\Repository\VersionProviders\FlatFileVersionProvider;
6+
use App\Repository\VersionProviders\JsonVersionProvider;
67
use App\Repository\VersionProviders\PhpArrayVersionProvider;
78
use App\Repository\VersionProviders\VersionProviderInterface;
89
use App\Exceptions\VersionProviderException;
@@ -39,6 +40,23 @@ public function getProvider(string $type, array $config, string $workingDirector
3940

4041
return new PhpArrayVersionProvider($config['filename'], $config['key'], $workingDirectory);
4142

43+
case 'json':
44+
if (!isset($config['filename'])) {
45+
throw new VersionProviderException('json', 'Missing filename in config!');
46+
}
47+
48+
if (!isset($config['key'])) {
49+
throw new VersionProviderException('json', 'Missing array key in config!');
50+
}
51+
52+
return new JsonVersionProvider(
53+
filename: $config['filename'],
54+
key: $config['key'],
55+
workingDirectory: $workingDirectory,
56+
pretty_print: ($config['pretty'] ?? 'true') == 'true',
57+
unescaped_slashes: ($config['unescaped_slashes'] ?? 'false') == 'true',
58+
);
59+
4260
default:
4361
throw new VersionProviderException($type, 'Invalid provider type!');
4462
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
namespace App\Repository\VersionProviders;
4+
5+
6+
use App\Exceptions\VersionProviderException;
7+
use JsonException;
8+
9+
/***
10+
* @example json?filename=composer.json&key=extra.version&pretty=true&unescaped_slashes=false
11+
*/
12+
readonly class JsonVersionProvider implements VersionProviderInterface
13+
{
14+
use FilePathToolsTrait;
15+
16+
public string $filename;
17+
18+
public function __construct(
19+
string $filename,
20+
public string $key,
21+
string $workingDirectory,
22+
public bool $pretty_print = true,
23+
public bool $unescaped_slashes = false,
24+
)
25+
{
26+
$this->filename = $this->getFilePath($filename, $workingDirectory);
27+
}
28+
29+
/**
30+
* @throws VersionProviderException
31+
*/
32+
public function getVersion(): string
33+
{
34+
if (!file_exists($this->filename)) {
35+
throw new VersionProviderException('json', 'File does not exist');
36+
}
37+
38+
$json = file_get_contents($this->filename);
39+
try {
40+
$json = json_decode($json, true, flags: JSON_THROW_ON_ERROR);
41+
} catch (JsonException $e) {
42+
throw new VersionProviderException('json', 'File is not a valid json file');
43+
}
44+
$key = explode('.', $this->key);
45+
46+
foreach ($key as $k) {
47+
if(!isset($json[$k])) {
48+
throw new VersionProviderException('json', 'JSON does contains the provided key (' . $k . '): '. $this->key);
49+
}
50+
51+
$json = $json[$k];
52+
}
53+
54+
if(is_array($json)) {
55+
throw new VersionProviderException('json', 'The provided JSON key, contains an array: '. $this->key);
56+
}
57+
58+
if(!is_string($json)) {
59+
throw new VersionProviderException('json', 'The provided JSON key, is not a string value: '. $this->key);
60+
}
61+
62+
return $json;
63+
}
64+
65+
/**
66+
* @throws VersionProviderException
67+
*/
68+
public function setVersion(string $version): void
69+
{
70+
$json = file_get_contents($this->filename);
71+
try {
72+
$json = json_decode($json, true, flags: JSON_THROW_ON_ERROR);
73+
} catch (JsonException $e) {
74+
throw new VersionProviderException('json', 'File is not a valid json file');
75+
}
76+
77+
$json = $this->dot_set($json, $this->key, $version);
78+
79+
$flags = 0;
80+
81+
if($this->pretty_print) {
82+
$flags |= JSON_PRETTY_PRINT;
83+
}
84+
85+
if($this->unescaped_slashes) {
86+
$flags |= JSON_UNESCAPED_SLASHES;
87+
}
88+
89+
$json = json_encode($json, $flags);
90+
file_put_contents($this->filename, $json);
91+
}
92+
93+
/**
94+
* @throws VersionProviderException
95+
*/
96+
private function dot_set(mixed $json, string $key, string $value): mixed
97+
{
98+
if(!is_array($json)) {
99+
throw new VersionProviderException('json', 'The provided JSON key, does not contains an array: '. $key);
100+
}
101+
102+
$k = explode('.', $key);
103+
$current_key = $k[0];
104+
if(!array_key_exists($current_key, $json)) {
105+
throw new VersionProviderException('json', 'The provided JSON key, does not contains an key: '. $current_key);
106+
}
107+
108+
if(count($k) > 1) {
109+
unset($k[0]);
110+
$json[$current_key] = $this->dot_set($json[$current_key], implode('.', $k), $value);
111+
} else {
112+
$json[$current_key] = $value;
113+
}
114+
return $json;
115+
}
116+
}

0 commit comments

Comments
 (0)