Lithron.LDatabase
[ class tree: Lithron.LDatabase ] [ index: Lithron.LDatabase ] [ all elements ]

Source for file LDatabaseConfiguration.php

Documentation is available at LDatabaseConfiguration.php

  1. <?php
  2.  
  3.  
  4. /**
  5. * LDatabaseConfiguration class file.
  6. *
  7. * @license http://opensource.org/licenses/mozilla1.1.php Mozilla Public License
  8. * @copyright 2005, diemeisterei GmbH. All rights reserved.
  9. * @author $Author: schmunk $
  10. * @version $Revision: 233 $ $Date: 2006-05-30 17:14:13 +0200 (Di, 30 Mai 2006) $
  11. * @package Lithron.LDatabase
  12. * @subpackage Support
  13. */
  14.  
  15. /**
  16. * Handles propel & creole configurations
  17. *
  18. * @package Lithron.LDatabase
  19. * @subpackage Support
  20. */
  21. class LDatabaseConfiguration
  22. {
  23. /**
  24. * Array of master config
  25. */
  26. public $_config;
  27.  
  28. /**
  29. * Returns array of master config
  30. */
  31. public function getConfig()
  32. {
  33. return $this->_config;
  34. }
  35.  
  36. /**
  37. * Inits the config handler
  38. */
  39. public function init($config)
  40. {
  41. # depreceated
  42. /*if (!is_file($masterConfigFile))
  43. {
  44. require(dirname(__FILE__)."/Error.php");
  45. exit;
  46. Prado::log("LDatabase master config file not found - aborting init!", 5, "Lithron.LDatabase");
  47. exit("<h1>error</h1>LDatabase is not configured correctly - master config file not found!<br>Create the file <br><b>".dirname(__FILE__)."/MasterConfig.php</b><br> and adjust your parameters.");
  48. return;
  49. }*/
  50.  
  51. // TODO - harcoded logging
  52. $_logdir = Prado::getPathOfAlias("Application").'/runtime/propel/propel.log';
  53. $this->_config['log'] = array ('ident' => 'propellog', 'level' => '7', 'name' => $_logdir, 'type' => 'file');
  54. $_cpd = & $this->_config['propel']['datasources'];
  55. $xmlObjs = $config->getElementsByTagName('database');
  56. foreach ($xmlObjs AS $xmlObj)
  57. {
  58. $attrs = $xmlObj->getAttributes();
  59. $connection["phptype"] = $attrs->itemAt("adapter");
  60. $connection["hostspec"] = $attrs->itemAt("hostspec");
  61. $connection["database"] = $attrs->itemAt("database");
  62. $connection["username"] = $attrs->itemAt("username");
  63. $connection["password"] = $attrs->itemAt("password");
  64. $_cpd[$attrs->itemAt("id")]['adapter'] = $attrs->itemAt("adapter");
  65. $_cpd[$attrs->itemAt("id")]['connection'] = $connection;
  66. }
  67. $_cpd['default'] = Prado :: getApplication()->getMode();
  68.  
  69. #$this->_config = include ($masterConfigFile);
  70.  
  71. $xmlObjs = $config->getElementsByTagName('scan');
  72. $pkgConfigs = array();
  73. foreach ($xmlObjs AS $xmlObj)
  74. {
  75. $attrs = $xmlObj->getAttributes();
  76. $pkgConfigs = array_merge($pkgConfigs,$this->findConfigurations($attrs->itemAt("namespace")));
  77. }
  78. if (is_array($pkgConfigs))
  79. foreach ($pkgConfigs AS $config)
  80. $this->_config = LDatabaseConfiguration :: addConfiguration($this->_config, $config);
  81. Prado :: trace("Propel config files parsed & merged", 'Lithron.LDatabaseConfiguration');
  82. #var_dump($this->_config);
  83.  
  84. }
  85.  
  86. /**
  87. * Reads package configs
  88. */
  89. private function findConfigurations($alias)
  90. {
  91. #Prado :: trace("Searching for LPropel configs ...", 'Lithron.LPropel');
  92. $pkgConfigs = array ();
  93. $baseDir = Prado :: getPathOfNamespace($alias);
  94. if (is_dir($baseDir))
  95. {
  96. $scan = scandir($baseDir);
  97. foreach ($scan AS $item)
  98. {
  99. $pkgDir = $baseDir."/".$item;
  100. if (is_dir($pkgDir))
  101. {
  102. $lPropelDir = $pkgDir."/db/propel";
  103. if (is_dir($lPropelDir))
  104. {
  105. $scan = scandir($lPropelDir);
  106. foreach ($scan AS $item)
  107. {
  108. if (substr($item, -9) == "-conf.php")
  109. {
  110. $pkgConfigs[$item] = include_once ($lPropelDir."/".$item);
  111. #Prado :: trace("found conf '$item' in '$alias'", 'Lithron.LDatabaseConfiguration');
  112. }
  113. }
  114. }
  115.  
  116. }
  117. }
  118. }
  119. return $pkgConfigs;
  120.  
  121. }
  122.  
  123. /**
  124. * Adds a package config to the config
  125. */
  126. private function addConfiguration($masterConfig, $config)
  127. {
  128. $sources = & $config['propel']['datasources'];
  129. foreach ($sources AS $id => $datasource)
  130. {
  131. if ($id == "value")
  132. continue;
  133. if ($id == "default")
  134. {
  135. $_ds = trim($datasource);
  136. if (isset ($sources[$datasource]))
  137. {
  138. continue;
  139. } else
  140. if (!empty ($_ds) && !isset ($sources[$datasource]))
  141. {
  142. $defaultKey = $masterConfig['propel']['datasources']['default'];
  143. Prado :: trace("Config for '$datasource' not found, assuming '$defaultKey'", 'Lithron.LDatabaseConfiguration');
  144. $masterConfig['propel']['datasources'][$datasource] = $masterConfig['propel']['datasources'][$defaultKey];
  145. continue;
  146. }
  147. }
  148. Prado :: trace("Config for '$id' added.", 'Lithron.LDatabaseConfiguration');
  149. $masterConfig['propel']['datasources'][$id] = $datasource;
  150.  
  151. # echo "<hr>$id";
  152.  
  153. }
  154. #var_dump($masterConfig);
  155.  
  156. return $masterConfig;
  157. }
  158.  
  159. /**
  160. * Returns a DSN from the config
  161. */
  162. public function getDsn($name)
  163. {
  164. $config = $this->_config;
  165. $c = $config['propel']['datasources'][$name]['connection'];
  166.  
  167. if ($c['username'])
  168. {
  169. $prefix = $c['username'].":".$c['password']."@";
  170. } else
  171. {
  172. $prefix = "";
  173. }
  174. $dsn = $c['phptype']."://".$prefix."".$c['hostspec']."/".$c['database'];
  175. return $dsn;
  176. }
  177.  
  178. }

Documentation generated on Tue, 20 Jun 2006 05:15:08 +0200 by phpDocumentor 1.3.0RC4