summaryrefslogtreecommitdiff
path: root/framework/Data/SqlMap/Configuration/TSqlMapXmlConfiguration.php
blob: bd2070bb46e4da02f4a93ec7eda823317d706610 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
 * TSqlMapXmlConfigBuilder, TSqlMapXmlConfiguration, TSqlMapXmlMappingConfiguration classes file.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2014 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @package System.Data.SqlMap.Configuration
 */']";
			foreach($document->xpath($xpath) as $node)
				return $node;
		}
	}

	/**
	 * @return string configuration file.
	 */
	protected abstract function getConfigFile();
}

/**
 * TSqlMapXmlConfig class.
 *
 * Configures the TSqlMapManager using xml configuration file.
 *
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
 * @package System.Data.SqlMap.Configuration
 * @since 3.1
 */
class TSqlMapXmlConfiguration extends TSqlMapXmlConfigBuilder
{
	/**
	 * @var TSqlMapManager manager
	 */
	private $_manager;
	/**
	 * @var string configuration file.
	 */
	private $_configFile;
	/**
	 * @var array global properties.
	 */
	private $_properties=array();

	/**
	 * @param TSqlMapManager manager instance.
	 */
	public function __construct($manager)
	{
		$this->_manager=$manager;
	}

	public function getManager()
	{
		return $this->_manager;
	}

	protected function getConfigFile()
	{
		return $this->_configFile;
	}

	/**
	 * Configure the TSqlMapManager using the given xml file.
	 * @param string SqlMap configuration xml file.
	 */
	public function configure($filename=null)
	{
		$this->_configFile=$filename;
		$document = $this->loadXmlDocument($filename,$this);

		foreach($document->xpath('//property') as $property)
			$this->loadGlobalProperty($property);

		foreach($document->xpath('//typeHandler') as $handler)
			$this->loadTypeHandler($handler);

		foreach($document->xpath('//connection[last()]') as $conn)
			$this->loadDatabaseConnection($conn);

		//try to load configuration in the current config file.
		$mapping = new TSqlMapXmlMappingConfiguration($this);
		$mapping->configure($filename);

		foreach($document->xpath('//sqlMap') as $sqlmap)
			$this->loadSqlMappingFiles($sqlmap);

		$this->resolveResultMapping();
		$this->attachCacheModels();
	}

	/**
	 * Load global replacement property.
	 * @param SimpleXmlElement property node.
	 */
	protected function loadGlobalProperty($node)
	{
		$this->_properties[(string)$node['name']] = (string)$node['value'];
	}

	/**
	 * Load the type handler configurations.
	 * @param SimpleXmlElement type handler node
	 */
	protected function loadTypeHandler($node)
	{
		$handler = $this->createObjectFromNode($node);
		$this->_manager->getTypeHandlers()->registerTypeHandler($handler);
	}

	/**
	 * Load the database connection tag.
	 * @param SimpleXmlElement connection node.
	 */
	protected function loadDatabaseConnection($node)
	{
		$conn = $this->createObjectFromNode($node);
		$this->_manager->setDbConnection($conn);
	}

	/**
	 * Load SqlMap mapping configuration.
	 * @param unknown_type $node
	 */
	protected function loadSqlMappingFiles($node)
	{
		if(strlen($resource = (string)$node['resource']) > 0)
		{
			if( strpos($resource, '${') !== false)
				$resource = $this->replaceProperties($resource);

			$mapping = new TSqlMapXmlMappingConfiguration($this);
			$filename = $this->getAbsoluteFilePath($this->_configFile, $resource);
			$mapping->configure($filename);
		}
	}

	/**
	 * Resolve nest result mappings.
	 */
	protected function resolveResultMapping()
	{
		$maps = $this->_manager->getResultMaps();
		foreach($maps as $entry)
		{
			foreach($entry->getColumns() as $item)
			{
				$resultMap = $item->getResultMapping();
				if(strlen($resultMap) > 0)
				{
					if($maps->contains($resultMap))
						$item->setNestedResultMap($maps[$resultMap]);
					else
						throw new TSqlMapConfigurationException(
							'sqlmap_unable_to_find_result_mapping',
								$resultMap, $this->_configFile, $entry->getID());
				}
			}
			if($entry->getDiscriminator()!==null)
				$entry->getDiscriminator()->initialize($this->_manager);
		}
	}

	/**
	 * Set the cache for each statement having a cache model property.
	 */
	protected function attachCacheModels()
	{
		foreach($this->_manager->getMappedStatements() as $mappedStatement)
		{
			if(strlen($model = $mappedStatement->getStatement()->getCacheModel()) > 0)
			{
				$cache = $this->_manager->getCacheModel($model);
				$mappedStatement->getStatement()->setCache($cache);
			}
		}
	}

	/**
	 * Replace the place holders ${name} in text with properties the
	 * corresponding global property value.
	 * @param string original string.
	 * @return string string with global property replacement.
	 */
	public function replaceProperties($string)
	{
		foreach($this->_properties as $find => $replace)
			$string = str_replace('${'.$find.'}', $replace, $string);
		return $string;
	}
}