Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TSqlMapConfig class file.
4
 *
5
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TSqlMapConfig.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Data.SqlMap
11
 */
12
 
13
Prado::using('System.Data.TDataSourceConfig');
14
 
15
/**
16
 * TSqlMapConfig module configuration class.
17
 *
18
 * Database connection and TSqlMapManager configuration.
19
 *
20
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
21
 * @version $Id: TSqlMapConfig.php 2541 2008-10-21 15:05:13Z qiang.xue $
22
 * @package System.Data.SqlMap
23
 * @since 3.1
24
 */
25
class TSqlMapConfig extends TDataSourceConfig
26
{
27
	private $_configFile;
28
	private $_sqlmap;
29
	private $_enableCache=false;
30
 
31
	/**
32
	 * File extension of external configuration file
33
	 */
34
	const CONFIG_FILE_EXT='.xml';
35
 
36
	/**
37
	 * @return string module ID + configuration file path.
38
	 */
39
	private function getCacheKey()
40
	{
41
		return $this->getID().$this->getConfigFile();
42
	}
43
 
44
	/**
45
	 * Deletes the configuration cache.
46
	 */
47
	public function clearCache()
48
	{
49
		$cache = $this->getApplication()->getCache();
50
		if($cache !== null) {
51
			$cache->delete($this->getCacheKey());
52
		}
53
	}
54
 
55
	/**
56
	 * Saves the current SqlMap manager to cache.
57
	 * @return boolean true if SqlMap manager was cached, false otherwise.
58
	 */
59
	protected function cacheSqlMapManager($manager)
60
	{
61
		if($this->getEnableCache())
62
		{
63
			$cache = $this->getApplication()->getCache();
64
			if($cache !== null) {
65
				return $cache->set($this->getCacheKey(), $manager);
66
			}
67
		}
68
		return false;
69
	}
70
 
71
	/**
72
	 * Loads SqlMap manager from cache.
73
	 * @return TSqlMapManager SqlMap manager intance if load was successful, null otherwise.
74
	 */
75
	protected function loadCachedSqlMapManager()
76
	{
77
		if($this->getEnableCache())
78
		{
79
			$cache = $this->getApplication()->getCache();
80
			if($cache !== null)
81
			{
82
				$manager = $cache->get($this->getCacheKey());
83
				if($manager instanceof TSqlMapManager)
84
					return $manager;
85
			}
86
		}
87
	}
88
 
89
	/**
90
	 * @return string SqlMap configuration file.
91
	 */
92
	public function getConfigFile()
93
	{
94
		return $this->_configFile;
95
	}
96
 
97
	/**
98
	 * @param string external configuration file in namespace format. The file
99
	 * extension must be '.xml'.
100
	 * @throws TConfigurationException if the file is invalid.
101
	 */
102
	public function setConfigFile($value)
103
	{
104
		if(is_file($value))
105
			$this->_configFile=$value;
106
		else
107
		{
108
			$file = Prado::getPathOfNamespace($value,self::CONFIG_FILE_EXT);
109
			if($file === null || !is_file($file))
110
				throw new TConfigurationException('sqlmap_configfile_invalid',$value);
111
			else
112
				$this->_configFile = $file;
113
		}
114
	}
115
 
116
	/**
117
	 * Set true to cache sqlmap instances.
118
	 * @param boolean true to cache sqlmap instance.
119
	 */
120
	public function setEnableCache($value)
121
	{
122
		$this->_enableCache = TPropertyValue::ensureBoolean($value);
123
	}
124
 
125
	/**
126
	 * @return boolean true if configuration should be cached, false otherwise.
127
	 */
128
	public function getEnableCache()
129
	{
130
		return $this->_enableCache;
131
	}
132
 
133
	/**
134
	 * Configure the data mapper using sqlmap configuration file.
135
	 * If cache is enabled, the data mapper instance is cached.
136
	 * @return TSqlMapGateway SqlMap gateway instance.
137
	 */
138
	protected function createSqlMapGateway()
139
	{
140
		Prado::using('System.Data.SqlMap.TSqlMapManager');
141
		if(($manager = $this->loadCachedSqlMapManager())===null)
142
		{
143
			$manager = new TSqlMapManager($this->getDbConnection());
144
			if(strlen($file=$this->getConfigFile()) > 0)
145
			{
146
				$manager->configureXml($file);
147
				$this->cacheSqlMapManager($manager);
148
			}
149
		}
150
		return $manager->getSqlmapGateway();
151
	}
152
 
153
	/**
154
	 * Initialize the sqlmap if necessary, returns the TSqlMapGateway instance.
155
	 * @return TSqlMapGateway SqlMap gateway instance.
156
	 */
157
	public function getClient()
158
	{
159
		if($this->_sqlmap===null )
160
			$this->_sqlmap=$this->createSqlMapGateway();
161
		return $this->_sqlmap;
162
	}
163
}
164