summaryrefslogtreecommitdiff
path: root/framework/Configuration/TProtectedConfiguration.php
blob: da643014484491c5a070e7be7cb7731134fe8b6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/**
 * TProtectedConfiguration class.
 * Provides access to the protected-configuration providers for the current application's configuration file.
 *
 * @author Jason Ragsdale <jrags@jasrags.net>
 * @version $Id: TProtectedConfiguration.php 1398 2006-09-08 19:31:03Z xue $
 * @package System.Configuration
 * @since 3.1
 */
final class TProtectedConfiguration extends TModule 
{
	private $_defaultProvider;
	/**
	 * @var array list of providers available
	 */
	private $_providers=array();
	/**
	 * @var string external configuration file
	 */
	private $_configFile=null;
	
	public function getDefaultProvider()
	{
		return $this->_defaultProvider;
	}
	public function setDefaultProvider($value)
	{
		$this->_defaultProvider = TPropertyValue::ensureString($value);
	}
	public function getProvider($value=null)
	{
		if ($value)
			$index = $value;
		else 	
			$index = $this->_defaultProvider;
			
		$provider = $this->_providers[$index];
			
		if (!$provider instanceof TProviderBase)
			throw new TConfigurationException('protectedconfiguration_not_a_provider',$index);
			
		return $provider;
	}
	
	public function init($config)
	{
		if($this->_configFile!==null)
		{
			if(is_file($this->_configFile))
			{
				$dom=new TXmlDocument;
				$dom->loadFromFile($this->_configFile);
				$this->loadConfig($dom);
			}
			else
				throw new TConfigurationException('protectedconfiguration_configfile_invalid',$this->_configFile);
		}
		$this->loadConfig($config);
//		$this->getApplication()->attachEventHandler('OnEndRequest',array($this,'collectLogs'));
	}
	/**
	 * Loads configuration from an XML element
	 * @param TXmlElement configuration node
	 * @throws TConfigurationException if log route class or type is not specified
	 */
	private function loadConfig($xml)
	{
		foreach($xml->getElementsByTagName('provider') as $providerConfig)
		{
			$properties=$providerConfig->getAttributes();
			if(($class=$properties->remove('class'))===null)
				throw new TConfigurationException('protectedconfiguration_providerclass_required');
			$provider=Prado::createComponent($class);
			if(!($provider instanceof TProviderBase))
				throw new TConfigurationException('protectedconfiguration_providertype_invalid');
			foreach($properties as $name=>$value)
				$provider->setSubproperty($name,$value);
			$this->_providers[$provider->getId()]=$provider;
			$provider->init($providerConfig);
		}
	}
}
?>