]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/config/ConfigJson.php
2 namespace Shaarli\Config
;
5 * Class ConfigJson (ConfigIO implementation)
7 * Handle Shaarli's JSON configuration file.
9 class ConfigJson
implements ConfigIO
14 public function read($filepath)
16 if (! is_readable($filepath)) {
19 $data = file_get_contents($filepath);
20 $data = str_replace(self
::getPhpHeaders(), '', $data);
21 $data = str_replace(self
::getPhpSuffix(), '', $data);
22 $data = json_decode($data, true);
24 $error = json_last_error();
25 throw new \
Exception('An error occurred while parsing JSON file: error code #'. $error);
33 public function write($filepath, $conf)
35 // JSON_PRETTY_PRINT is available from PHP 5.4.
36 $print = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT
: 0;
37 $data = self
::getPhpHeaders() . json_encode($conf, $print) . self
::getPhpSuffix();
38 if (!file_put_contents($filepath, $data)) {
39 throw new \
IOException(
41 'Shaarli could not create the config file.
42 Please make sure Shaarli has the right to write in the folder is it installed in.'
50 public function getExtension()
56 * The JSON data is wrapped in a PHP file for security purpose.
57 * This way, even if the file is accessible, credentials and configuration won't be exposed.
59 * Note: this isn't a static field because concatenation isn't supported in field declaration before PHP 5.6.
61 * @return string PHP start tag and comment tag.
63 public static function getPhpHeaders()
65 return '<?php /*'. PHP_EOL
;
69 * Get PHP comment closing tags.
71 * Static method for consistency with getPhpHeaders.
73 * @return string PHP comment closing.
75 public static function getPhpSuffix()
77 return PHP_EOL
. '*/ ?>';