108 lines
2.4 KiB
PHP
108 lines
2.4 KiB
PHP
<?php
|
|
|
|
class App_Service_JsonConfiguration
|
|
{
|
|
const CONFIGURATION_FOLDER_PATH = APPLICATION_PATH . "/configs/";
|
|
|
|
protected $_config;
|
|
protected $_writer;
|
|
|
|
public function __construct($nomeFile)
|
|
{
|
|
|
|
$this->initializeConfigurationFile($nomeFile);
|
|
}
|
|
|
|
private function initializeConfigurationFile($name)
|
|
{
|
|
|
|
$filePath = self::CONFIGURATION_FOLDER_PATH . $name . ".json";
|
|
if (!file_exists($filePath)) throw new Zend_Exception("File di configurazione \"$name.json\" non trovato");
|
|
$readerName = strtoupper($name) . '_CONFIG' . '_READER';
|
|
$writerName = strtoupper($name) . '_CONFIG' . '_WRITER';
|
|
//READER CONFIG FILE
|
|
if (Zend_Registry::isRegistered($readerName)){
|
|
$this->_config = Zend_Registry::get($readerName);
|
|
|
|
return;
|
|
}
|
|
$this->_config = new Zend_Config_Json($filePath, null, array('skipExtends' => true, 'allowModifications' => true));
|
|
Zend_Registry::set($readerName, $this->_config);
|
|
//WRITER
|
|
if (Zend_Registry::isRegistered($writerName)){
|
|
$this->_writer = Zend_Registry::get($writerName);
|
|
|
|
return;
|
|
}
|
|
$this->_writer = new Zend_Config_Writer_Json(array('config' => $this->_config, 'filename' => $filePath));
|
|
$this->_writer->setPrettyPrint(true);
|
|
Zend_Registry::set($writerName, $this->_writer);
|
|
}
|
|
|
|
/**
|
|
* Aggiorna l'oggetto config ricaricando tutte le impostazioni memorizzate
|
|
* nel file config.ini
|
|
*/
|
|
public function update()
|
|
{
|
|
|
|
$this->_config = new Zend_Config_Json(APPLICATION_PATH . '/configs/config.ini', null, array('skipExtends' => true, 'allowModifications' => true));
|
|
Zend_Registry::set('config', $this->_config);
|
|
}
|
|
|
|
public function write()
|
|
{
|
|
|
|
$this->_writer->config = $this->_config;
|
|
$this->_writer->write();
|
|
}
|
|
|
|
public function set($key, $value)
|
|
{
|
|
|
|
$this->_config->__set($key, $value);
|
|
$this->_writer->write();
|
|
}
|
|
|
|
/**
|
|
* Restituisce il valore del parametro richiesto
|
|
*
|
|
* @param string $name
|
|
*
|
|
* @return string
|
|
*/
|
|
public function get($name)
|
|
{
|
|
|
|
return $this->_config->get($name);
|
|
}
|
|
|
|
/**
|
|
* Elimina il parametro richiesto se esistente
|
|
*
|
|
* @param string $param
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function delete($param)
|
|
{
|
|
if (!$this->_config->get($param)) return false;
|
|
$this->_config->__unset($param);
|
|
$this->write();
|
|
|
|
return true;
|
|
}
|
|
|
|
public function getAllParams()
|
|
{
|
|
|
|
$data = array();
|
|
foreach ($this->_config as $key => $value){
|
|
$data[$key] = $value;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
}
|