ComposerJsonFixes::getJSON()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Sunnysideup\UpgradeToSilverstripe4\Tasks\Helpers;
4
5
use Sunnysideup\UpgradeToSilverstripe4\Traits\HelperInst;
6
7
class ComposerJsonFixes
8
{
9
    use HelperInst;
10
11
    public function getJSON(string $dir): array
12
    {
13
        $location = $dir . '/composer.json';
14
        $jsonString = file_get_contents($location);
15
16
        return json_decode($jsonString, true);
17
    }
18
19
    public function setJSON(string $dir, array $data)
20
    {
21
        $location = $dir . '/composer.json';
22
        $newJsonString = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
23
        file_put_contents($location, $newJsonString);
24
    }
25
26
    public function UpdateJSONViaCommandLine(string $dir, string $code, string $comment)
27
    {
28
        $location = $dir . '/composer.json';
29
        $this->mu()->execMe(
30
            $dir,
31
            'php -r  \''
32
                . '$jsonString = file_get_contents("' . $location . '"); '
33
                . '$data = json_decode($jsonString, true); '
34
                . $code
35
                . '$newJsonString = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); '
36
                . 'file_put_contents("' . $location . '", $newJsonString); '
37
                . '\'',
38
            $comment . ' --- in ' . $location,
39
            false
40
        );
41
    }
42
}
43