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