]>
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(trim($data), true);
24 $errorCode = json_last_error();
26 'An error occurred while parsing JSON configuration file (%s): error code #%d',
30 $error .= '<br>➜ <code>' . json_last_error_msg() .'</code>';
31 if ($errorCode === JSON_ERROR_SYNTAX
) {
33 $error .= 'Please check your JSON syntax (without PHP comment tags) using a JSON lint tool such as ';
34 $error .= '<a href="http://jsonlint.com/">jsonlint.com</a>.';
36 throw new \
Exception($error);
44 public function write($filepath, $conf)
46 // JSON_PRETTY_PRINT is available from PHP 5.4.
47 $print = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT
: 0;
48 $data = self
::getPhpHeaders() . json_encode($conf, $print) . self
::getPhpSuffix();
49 if (empty($filepath) || !file_put_contents($filepath, $data)) {
50 throw new \Shaarli\Exceptions\
IOException(
52 t('Shaarli could not create the config file. '.
53 'Please make sure Shaarli has the right to write in the folder is it installed in.')
61 public function getExtension()
67 * The JSON data is wrapped in a PHP file for security purpose.
68 * This way, even if the file is accessible, credentials and configuration won't be exposed.
70 * Note: this isn't a static field because concatenation isn't supported in field declaration before PHP 5.6.
72 * @return string PHP start tag and comment tag.
74 public static function getPhpHeaders()
80 * Get PHP comment closing tags.
82 * Static method for consistency with getPhpHeaders.
84 * @return string PHP comment closing.
86 public static function getPhpSuffix()