<?php

/**
 * This file is part of the mobileOK Pythia plug-in common files and defines the
 * {@link DDRServiceConfigurationFactory} class.
 * 
 * @author Sylvain Lequeux
 * @author Francois Daoust <fd@w3.org>
 * @package mobileOKPythia
 * @version $Revision: 1.1 $
 * @license http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231.html W3C Software Notice and License
 * @copyright Copyright (c) 2009, W3C (MIT, ERCIM, Keio)
 */


/**
 * The DDRServiceConfigurationFactory class defines a static getServiceConfiguration
 * method that returns the configuration parameters to use when initializing
 * the implementation of the DDR Simple API used within the mobileOK Pythia
 * plug-in to identify and retrieve properties of the requesting device. 
 *
 * @author Sylvain Lequeux
 * @author Francois Daoust <fd@w3.org>
 * @package mobileOKPythia
 * @version $Revision: 1.1 $
 * @license http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231.html W3C Software Notice and License
 * @copyright Copyright (c) 2009, W3C (MIT, ERCIM, Keio)
 */
class DDRServiceConfigurationFactory {
	/**
	 * Initializes and return the {@link Service} configuration settings to
	 * use when accessing the underlying device description repository.
	 *
	 * The configuration setting are to be sent to the {@link ServiceFactory}
	 * class or {@link Service::initialize()}.
	 * 
	 * The class tries to include and instantiate a class named after the
	 * provided $clazz argument. The class must be called:
	 *  [$clazz]ServiceConfiguration
	 * and must implement the {@link DDRServiceConfiguration} interface defined
	 * below.
	 * 
	 * If the class is not already defined, a file with the same name as the
	 * class is looked up in the directory of the DDR Simple API implementation
	 * or in current directory, and included.
	 * 
	 * @param string $clazz the DDR implementation name
	 * @param mixed $params Parameters that will be passed to created instance
	 * @return mixed the configuration settings to use with that DDR implementation
	 */
	public static function getServiceConfiguration($clazz, $params) {
		$className = $clazz . 'ServiceConfiguration';
		
		if (!class_exists($className)) {
			$path = dirname(__FILE__) . '/../../' . $className . '.php';
			if (file_exists($path)) {
				require_once($path);
			}
			else {
				$path = dirname(__FILE__) . '/../../includes/' . $className . '.php';
				var_dump($path);
				if (file_exists($path)) {
					require_once($path);
				}
				else {
					$path = dirname(__FILE__) . '/../ddrsimpleapi/implementation/' . $clazz . '/' . $className . '.php';
					var_dump($path);
					if (file_exists($path)) {
						require_once($path);
					}
				}
			}
		}
		
		$config = null;
		if (class_exists($className)){
			$config = new $className($params);
		}
		
		return $config;
	}
}


/**
 * Interface that must be implemented by classes returned by the
 * {@link DDRServiceConfigurationFactory} class.
 *
 * @author Sylvain Lequeux
 * @author Francois Daoust <fd@w3.org>
 * @package mobileOKPythia
 * @version $Revision: 1.1 $
 * @license http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231.html W3C Software Notice and License
 * @copyright Copyright (c) 2009, W3C (MIT, ERCIM, Keio)
 */
interface DDRServiceConfiguration {
	/**
	 * Retrieves the configuration settings passed along to 
	 * {@link Service::initialize()}.
	 * 
	 * @return mixed Service configuration settings
	 */
	function getConfig();
	
	/**
	 * Registers the options that may be set to fine-tune
	 * the underlying {@link Service} implementation. 
	 */
	function addOptions();
	
	/**
	 * Prints and gives the possibility to change the options
	 * in admin pages.
	 * 
	 * NB: options saving should be automatically handled by
	 * the underlying CMS tool.
	 */
	function printOptions();
	
	/**
	 * Returns the registered options as a comma separated value
	 * list. The returned string must end with a ',' or be empty.
	 *
	 * @return string The list of options 
	 */
	function getOptions();
}