Completed
Push — master ( c612e7...f3e1c8 )
by Christian
06:23
created

JsonFile::__construct()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 22
rs 8.9197
cc 4
eloc 12
nc 6
nop 2
1
<?php
2
3
/**
4
 * This file is part of tenside/core.
5
 *
6
 * (c) Christian Schiffler <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * This project is provided in good faith and hope to be usable by anyone.
12
 *
13
 * @package    tenside/core
14
 * @author     Christian Schiffler <[email protected]>
15
 * @copyright  2015 Christian Schiffler <[email protected]>
16
 * @license    https://github.com/tenside/core/blob/master/LICENSE MIT
17
 * @link       https://github.com/tenside/core
18
 * @filesource
19
 */
20
21
namespace Tenside\Core\Util;
22
23
/**
24
 * Generic path following json file handler.
25
 *
26
 * @author Christian Schiffler <https://github.com/discordier>
27
 */
28
class JsonFile extends JsonArray
29
{
30
    /**
31
     * The filename.
32
     *
33
     * @var string
34
     */
35
    private $filename;
36
37
    /**
38
     * The name of the backup file to create when saving.
39
     *
40
     * @var null|string
41
     */
42
    private $backupFile;
43
44
    /**
45
     * Create a new instance.
46
     *
47
     * @param string      $filename   The filename.
48
     *
49
     * @param null|string $backupFile The name of a backup file to create (if none shall be created, pass null).
50
     *                                The default name of the backup file is the filename with a tilde (~) appended.
51
     *
52
     * @throws \RuntimeException When the file contents are invalid.
53
     */
54
    public function __construct($filename, $backupFile = '')
55
    {
56
        $this->filename = (string) $filename;
57
58
        if ('' === $backupFile) {
59
            $backupFile = $filename . '~';
60
        }
61
62
        $this->backupFile = $backupFile;
63
64
        if (file_exists($this->filename)) {
65
            try {
66
                parent::__construct(file_get_contents($this->filename));
67
            } catch (\Exception $exception) {
68
                throw new \RuntimeException('Error: json file ' . $this->filename . ' is invalid.', 1, $exception);
69
            }
70
71
            return;
72
        }
73
74
        parent::__construct();
75
    }
76
77
    /**
78
     * Set a value.
79
     *
80
     * @param string $path  The path of the value.
81
     *
82
     * @param mixed  $value The value to set.
83
     *
84
     * @return JsonFile
85
     */
86
    public function set($path, $value)
87
    {
88
        parent::set($path, $value);
89
90
        $this->save();
91
92
        return $this;
93
    }
94
95
    /**
96
     * Retrieve the file name.
97
     *
98
     * @return string
99
     */
100
    public function getFilename()
101
    {
102
        return $this->filename;
103
    }
104
105
    /**
106
     * Copy the file contents over to the backup.
107
     *
108
     * @return void
109
     */
110
    private function makeBackup()
111
    {
112
        if ((null === $this->backupFile) || !file_exists($this->filename)) {
113
            return;
114
        }
115
116
        if (!is_dir(dirname($this->backupFile))) {
117
            mkdir(dirname($this->backupFile), 0700, true);
118
        }
119
120
        copy($this->filename, $this->backupFile);
121
    }
122
123
    /**
124
     * Save the file data.
125
     *
126
     * @return JsonFile
127
     */
128
    public function save()
129
    {
130
        $this->makeBackup();
131
132
        if (!is_dir(dirname($this->filename))) {
133
            mkdir(dirname($this->filename), 0700, true);
134
        }
135
136
        file_put_contents($this->filename, (string) $this);
137
138
        return $this;
139
    }
140
}
141