{% else-1 %}
Писал для своего фреймворка (ВЕЛОСИПЕДЫ РУЛЕЗ!!) *

Позволяет вам обращаться к конфигурации приложения которая хранится в JSON формате *

пример в конце кода, код прокомментирован

                        
<?php
namespace VendorApp;

interface JsonConfigInterface
{
/*
Returns an instance of itself
*/
static function getInstance ();
/*
A parameter of type string

The method parameter is the path to the configuration file
*/
function setConfigPath ($path);
/*
A parameter of type string

The default file which will be used in the case where the method getItem() is not passed to the second parameter
*/
function setDefaultFile ($fileName);
/*
A parameter of type string
A parameter of type string

Reads the JSON file "$file".
Converts from JSON to an array and looks for the key "$item" in case of success returns its value otherwise NULL
*/
function getItem ($item, $file = '');
}

class JsonConfig implements JsonConfigInterface
{
private $readData = [];
private $configPath = '';
private $defaultFile = 'config';
private static $instance;

static function getInstance ()
{
if (!is_object(self::$instance))
{
self::$instance = new self();
}

return self::$instance;
}

public function getItem ($item, $file = '')
{
if (empty($file))
{
$file = $this->defaultFile;
}

$fullPathFile = rtrim($this->configPath, '/').'/'.$file.'.json';

if (!file_exists($fullPathFile))
{
throw new Exception('The configuration file is not found');
}

if (!isset($this->readData[$file]))
{
$jsonData = file_get_contents($fullPathFile);
$jsonData = json_decode($jsonData, TRUE);

switch (json_last_error())
{
case JSON_ERROR_DEPTH:
throw new Exception('You have reached the maximum stack depth');
break;
case JSON_ERROR_STATE_MISMATCH:
throw new Exception('Incorrect digits or the coincidence of modes');
break;
case JSON_ERROR_CTRL_CHAR:
throw new Exception('Invalid control character');
break;
case JSON_ERROR_SYNTAX:
throw new Exception('Syntax error, not a valid JSON');
break;
case JSON_ERROR_UTF8:
throw new Exception('Incorrect UTF-8 characters, possibly invalid encoding');
break;
}

$this->readData[$file] = $jsonData;
}

return (isset($this->readData[$file][$item]) ? $this->readData[$file][$item] : NULL);
}

public function setDefaultFile ($fileName)
{
$this->defaultFile = $fileName;
}

public function setConfigPath ($path)
{
if (!is_dir($path))
{
throw new Exception('Path to configuration files is not correct');
}

$this->configPath = $path;
}

protected function __clone() { }
protected function __construct() {}
}

/*
Example use class:

$jsonConfig = JsonConfig::getInstance();
$jsonConfig->setConfigPath('/var/www/tcs.lo/www/');
$jsonConfig->setDefaultFile('test');

echo $jsonConfig->getItem('site-name');
echo $jsonConfig->getItem('username', 'database');
*/
-1 65 0
Без комментариев...