Location: PHPKode > projects > NmnLogger > NmnLogger1.1/NmnLoggerConfig.php
<?php 
    /*
    NmnLogger is a library that provides logging functionnality to php applications
    Copyright (C) 2006  Ivan Preziosi from netmeans.net - Rome.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    For more informations or to join the development of the library contact
    the author at: hide@address.com
    */

require_once 'logger/NmnLoggerDevice.php';

/**
 * This class is intended to parse and store all the informations written in the 
 * nmn-logger-config.xml.
 * 
 * @author  Ivan Preziosi <hide@address.com>
 * @version 1.1
 * @since   NmnLogger 0.5
 * @package NmnLogger
 *
 */
Class NmnLoggerConfig{
	/**
	* the path to look for the xml config file.
	*/
	private $configPath;

	/**
	* instance of the SimpleXMLElement parser element.
	*/
	private $xmlConfig;

	/**
	* instance of the NmnMessageFactory class with provides message instances.
	*/
	private $messageFactory;

	/**
	* family the NmnLoggerObject is registered to.
	*/
	private $family;

	/**
	* array containing all the NmnLoggerDevice instances created while parsing the config file.
	*/
	private $devices;


	/**
	* Expects the location of the config file and the family this istance belongs to.
	*/
	public function __construct($myConfigPath = 'logger/nmn-logger-config.xml',$myFamily = 'default'){
		$this->configPath = $myConfigPath;
		$this->family = $myFamily;
		$this->devices = array();
		$this->loadConfigFile();
	}

	
	public function getFamily(){
		return $this->family;
	}

	public function getDevices(){
		return $this->devices;
	}

	public function getMessageFactory(){
		return $this->messageFactory;
	}

	/**
	* Parses the config file and valorizes all the properties of the config object
	*/
	private function parseConfigFile(){
		$this->xmlConfig = simplexml_load_file($this->configPath);
			$familyIdFound = false;
			$messageFactoryFound = false;
			foreach($this->xmlConfig->children() as $nodeName => $nodeNameData){

				switch ($nodeName){
					case "message-factory":
						$this->messageFactory = $this->import($nodeNameData);
						$messageFactoryFound = true;
						break;
					case "logger-family":
						$fammilyId = $this->findXmlAttribute($nodeNameData,'id');	
						if (!$fammilyId){
							throw new Exception("NmnLogger: LoggerFamily with no id specified");
						}
						if ($fammilyId == $this->family){
							$familyIdFound = true;												
							foreach($nodeNameData->children() as $device => $deviceData){
								if ($device == 'logger-device'){ // check if right node
									$deviceLevel = $this->findXmlAttribute($deviceData,'level');	
									if (!$deviceLevel){
										throw new Exception("NmnLogger: logger device with no level specified");
									}
									$deviceDrivers = array();
									
									foreach($deviceData->children() as $driver => $driverName){
										if ($driver == 'driver'){// check if right node
											$driverObj = $this->import($driverName);
											$deviceDrivers["'".$driverName."'"] = $driverObj;
										}
									}
									array_push($this->devices, new NmnLoggerDevice($deviceLevel,$deviceDrivers));		
								}
							}
						}
						break;
				}					
			}
			if (!$familyIdFound){
				throw new Exception("NmnLogger: specified family not found");
			}
	}
	
	/**
	* Tries to load the specified config file.
	*/
	private function loadConfigFile(){
		$this->configPath = $this->findFile($this->configPath);
		if ($this->configPath != ''){
			$this->parseConfigFile($this->configPath);
		}else{
			throw new Exception('NmnLogger: ConfigFile NotFound');
		}
	}

	/**
	* Extracts the value of the specified attribute from the given SimpleXMLElement object.
	*/
	private function findXmlAttribute($object,$key) {
		$myReturn = false;
		foreach($object->attributes() as $pippo => $pino){
			if ($pippo == $key){ $myReturn = (String)$pino;}
		}
		return $myReturn;
	}

	/**
	* Given a SimpleXMLElement object, looks for, includes and iniatialize the config specified class.
	*/
	private function import($xmlNode) {
		
		$class = $xmlNode['class'];
		if (!class_exists($class, false)) {
            $path = strtr($class, '.', DIRECTORY_SEPARATOR) . '.php';
            $ret = include_once($path);
            if ($ret === false) {
                throw new Exception("NmnLogger: Unable to load xmlNode class: " . $class);
            }
            $pos = strrpos($class, '.');
            if ($pos !== false) {
                $class = substr($class, $pos + 1);
            }
            if (!class_exists($class)) {
                throw new Exception("NmnLogger: Unable to find loaded class: $class (Hint: make sure classname matches filename)");
            }
        }
		$classObj = new $class;
		foreach($xmlNode->attributes() as $param => $value){
			if ($param != 'class'){ //parametro riservato
			$classObj->setParams($param,$value);
			}
		}

        return $classObj;
    }

	/**
	* Tries to find a file or directory scanning in the include_path.
	* If succeds returns the actual filename.
	* On failure returns ''
	*/
	private function findFile($filePath,$type = "file"){
		$includePathArray = explode( PATH_SEPARATOR, get_include_path() );
		foreach ($includePathArray as $includePath){
			$tempfilePath = $includePath.$filePath;
			if ($type == "file"){
				if (file_exists($tempfilePath)){
					return $tempfilePath;
				}
			}else if ($type == "dir"){
				if (is_dir($tempfilePath)){
					return $tempfilePath;
				}
			}
		}
		return '';
	}

}
?>
Return current item: NmnLogger