/framework/Data/SqlMap/TSqlMapConfig.php

https://bitbucket.org/volatileeight/prado · PHP · 179 lines · 99 code · 15 blank · 65 comment · 13 complexity · a2d5a335b1b220443ee0a07a263d91b9 MD5 · raw file

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