<?php
 namespace App\Config; abstract class XmlBasicAbstract extends BaseAbstract { protected $fileName=null; protected $fileNameWithPath=null; protected function constructInit() { if (!$this->fileName) { throw new \Exception('You did not set the `fileName` attribute for the class `'.get_called_class().'`.'); } $this->fileNameWithPath = APP_REAL_PATH . FILEPATH_APPCONFIGS . $this->fileName; } protected function constructAdmin() { global $S; $className=get_called_class(); if (isset($S['admin']['configApp'][$className]['itemsList'])) { $this->itemsList=$S['admin']['configApp'][$className]['itemsList']; } if (isset($S['admin']['configApp'][$className]['changedFlag'])) { $this->changedFlag=$S['admin']['configApp'][$className]['changedFlag']; } $S['admin']['configApp'][$className]['itemsList']=&$this->itemsList; $S['admin']['configApp'][$className]['changedFlag']=&$this->changedFlag; } protected function getAll_rawData() { if (!file_exists($this->fileNameWithPath)) return $this->getAll_rawData_fileNotExists(); $xml=@simplexml_load_file($this->fileNameWithPath); if (!($xml instanceof \SimpleXMLElement)) throw new \Exception('The config file is not a well formed XML (class = '.get_called_class().' ; file = '.$this->fileNameWithPath.').'); return $this->getAll_rawData_extractDataFromFile($xml); } protected function getAll_rawData_fileNotExists() { return []; } abstract protected function getAll_rawData_extractDataFromFile(\SimpleXMLElement $xml); public function set($var, $value) { if ($this->constructType!='admin') throw new \Exception("You can use the method `".get_called_class().'::'.__FUNCTION__."` only if the constructor type is `admin`."); if (!is_string($var) and !is_numeric($var)) throw new \Exception("You did not set() a correct name for the key."); $this->keyValueCheck($var, $value); $this->itemsList[$var]=$value; $this->changedFlag=true; } protected function keyValueCheck($code, $itemDetails) {} public function setAll($vars) { if ($this->constructType!='admin') throw new \Exception("You can use the method `".get_called_class().'::'.__FUNCTION__."` only if the constructor type is `admin`."); if (!is_array($vars)) throw new \Exception("You did not setAll() a correct list of elements - the parameter is not an array."); $this->itemsList=$vars; $this->changedFlag=true; } protected function save_write_do() { $xmlObj=$this->save_write_do_getXml(); if (!($xmlObj instanceof \SimpleXMLElement)) throw new \Exception('The output of the method `'.get_called_class().'::save_write_do_getXml` must be a `SimpleXMLElement` object.'); $fileChmod=false; if (file_exists($this->fileNameWithPath)) { if (!is_writeable($this->fileNameWithPath)) $this->addErrorMessage('The config file exists but it is not writeable.'); } else { $fileChmod=true; } if (!file_put_contents($this->fileNameWithPath, $xmlObj->asXml())) { $this->addErrorMessage('The config file could not be (over)written.'); } if ($fileChmod) { if (!chmod($this->fileNameWithPath, 0777)) { $this->addErrorMessage('The config file could not have its acess rights updated.'); } } } abstract protected function save_write_do_getXml(); } 