Completed
Branch master (099915)
by Fabio
08:02
created

TSqlMapXmlConfiguration::resolveResultMapping()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 24.432

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 15
c 1
b 0
f 0
nc 8
nop 0
dl 0
loc 21
ccs 3
cts 15
cp 0.2
crap 24.432
rs 9.2222
1
<?php
2
/**
3
 * TSqlMapXmlConfigBuilder, TSqlMapXmlConfiguration, TSqlMapXmlMappingConfiguration classes file.
4
 *
5
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6
 * @link https://github.com/pradosoft/prado
7
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
8
 * @package Prado\Data\SqlMap\Configuration
9
 */
10
11
namespace Prado\Data\SqlMap\Configuration;
12
13
use Prado\Data\SqlMap\DataMapper\TSqlMapConfigurationException;
14
15
/**
16
 * TSqlMapXmlConfig class.
17
 *
18
 * Configures the TSqlMapManager using xml configuration file.
19
 *
20
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
21
 * @package Prado\Data\SqlMap\Configuration
22
 * @since 3.1
23
 */
24
class TSqlMapXmlConfiguration extends TSqlMapXmlConfigBuilder
25
{
26
	/**
27
	 * @var TSqlMapManager manager
0 ignored issues
show
Bug introduced by
The type Prado\Data\SqlMap\Configuration\TSqlMapManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
	 */
29
	private $_manager;
30
	/**
31
	 * @var string configuration file.
32
	 */
33
	private $_configFile;
34
	/**
35
	 * @var array global properties.
36
	 */
37
	private $_properties = [];
38
39
	/**
40
	 * @param TSqlMapManager $manager manager instance.
41
	 */
42 3
	public function __construct($manager)
43
	{
44 3
		$this->_manager = $manager;
45 3
	}
46
47 3
	public function getManager()
48
	{
49 3
		return $this->_manager;
50
	}
51
52
	protected function getConfigFile()
53
	{
54
		return $this->_configFile;
55
	}
56
57
	/**
58
	 * Configure the TSqlMapManager using the given xml file.
59
	 * @param string $filename SqlMap configuration xml file.
60
	 */
61 3
	public function configure($filename = null)
62
	{
63 3
		$this->_configFile = $filename;
64 3
		$document = $this->loadXmlDocument($filename, $this);
65
66 3
		foreach ($document->xpath('//property') as $property) {
67
			$this->loadGlobalProperty($property);
68
		}
69
70 3
		foreach ($document->xpath('//typeHandler') as $handler) {
71
			$this->loadTypeHandler($handler);
72
		}
73
74 3
		foreach ($document->xpath('//connection[last()]') as $conn) {
75
			$this->loadDatabaseConnection($conn);
76
		}
77
78
		//try to load configuration in the current config file.
79 3
		$mapping = new TSqlMapXmlMappingConfiguration($this);
80 3
		$mapping->configure($filename);
81
82 2
		foreach ($document->xpath('//sqlMap') as $sqlmap) {
83 1
			$this->loadSqlMappingFiles($sqlmap);
84
		}
85
86 2
		$this->resolveResultMapping();
87 2
		$this->attachCacheModels();
88 2
	}
89
90
	/**
91
	 * Load global replacement property.
92
	 * @param SimpleXmlElement $node property node.
0 ignored issues
show
Bug introduced by
The type Prado\Data\SqlMap\Configuration\SimpleXmlElement was not found. Did you mean SimpleXmlElement? If so, make sure to prefix the type with \.
Loading history...
93
	 */
94
	protected function loadGlobalProperty($node)
95
	{
96
		$this->_properties[(string) $node['name']] = (string) $node['value'];
97
	}
98
99
	/**
100
	 * Load the type handler configurations.
101
	 * @param SimpleXmlElement $node type handler node
102
	 */
103
	protected function loadTypeHandler($node)
104
	{
105
		$handler = $this->createObjectFromNode($node);
106
		$this->_manager->getTypeHandlers()->registerTypeHandler($handler);
107
	}
108
109
	/**
110
	 * Load the database connection tag.
111
	 * @param SimpleXmlElement $node connection node.
112
	 */
113
	protected function loadDatabaseConnection($node)
114
	{
115
		$conn = $this->createObjectFromNode($node);
116
		$this->_manager->setDbConnection($conn);
117
	}
118
119
	/**
120
	 * Load SqlMap mapping configuration.
121
	 * @param unknown_type $node
0 ignored issues
show
Bug introduced by
The type Prado\Data\SqlMap\Configuration\unknown_type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
122
	 */
123 1
	protected function loadSqlMappingFiles($node)
124
	{
125 1
		if (strlen($resource = (string) $node['resource']) > 0) {
126
			if (strpos($resource, '${') !== false) {
127
				$resource = $this->replaceProperties($resource);
128
			}
129
130
			$mapping = new TSqlMapXmlMappingConfiguration($this);
131
			$filename = $this->getAbsoluteFilePath($this->_configFile, $resource);
132
			$mapping->configure($filename);
133
		}
134 1
	}
135
136
	/**
137
	 * Resolve nest result mappings.
138
	 */
139 2
	protected function resolveResultMapping()
140
	{
141 2
		$maps = $this->_manager->getResultMaps();
142 2
		foreach ($maps as $entry) {
143
			foreach ($entry->getColumns() as $item) {
144
				$resultMap = $item->getResultMapping();
145
				if (strlen($resultMap) > 0) {
146
					if ($maps->contains($resultMap)) {
147
						$item->setNestedResultMap($maps[$resultMap]);
148
					} else {
149
						throw new TSqlMapConfigurationException(
150
							'sqlmap_unable_to_find_result_mapping',
151
							$resultMap,
152
							$this->_configFile,
153
							$entry->getID()
154
						);
155
					}
156
				}
157
			}
158
			if ($entry->getDiscriminator() !== null) {
159
				$entry->getDiscriminator()->initialize($this->_manager);
160
			}
161
		}
162 2
	}
163
164
	/**
165
	 * Set the cache for each statement having a cache model property.
166
	 */
167 2
	protected function attachCacheModels()
168
	{
169 2
		foreach ($this->_manager->getMappedStatements() as $mappedStatement) {
170 2
			if (strlen($model = $mappedStatement->getStatement()->getCacheModel()) > 0) {
171
				$cache = $this->_manager->getCacheModel($model);
172 2
				$mappedStatement->getStatement()->setCache($cache);
173
			}
174
		}
175 2
	}
176
177
	/**
178
	 * Replace the place holders ${name} in text with properties the
179
	 * corresponding global property value.
180
	 * @param string $string original string.
181
	 * @return string string with global property replacement.
182
	 */
183 3
	public function replaceProperties($string)
184
	{
185 3
		foreach ($this->_properties as $find => $replace) {
186
			$string = str_replace('${' . $find . '}', $replace, $string);
187
		}
188 3
		return $string;
189
	}
190
}
191