- <?php
-
-
- /**
- * LDatabaseConfiguration class file.
- *
- * @license http://opensource.org/licenses/mozilla1.1.php Mozilla Public License
- * @copyright 2005, diemeisterei GmbH. All rights reserved.
- * @author $Author: schmunk $
- * @version $Revision: 233 $ $Date: 2006-05-30 17:14:13 +0200 (Di, 30 Mai 2006) $
- * @package Lithron.LDatabase
- * @subpackage Support
- */
-
- /**
- * Handles propel & creole configurations
- *
- * @package Lithron.LDatabase
- * @subpackage Support
- */
- class LDatabaseConfiguration
- {
- /**
- * Array of master config
- */
- public $_config;
-
- /**
- * Returns array of master config
- */
- public function getConfig()
- {
- return $this->_config;
- }
-
- /**
- * Inits the config handler
- */
- public function init($config)
- {
- # depreceated
- /*if (!is_file($masterConfigFile))
- {
- require(dirname(__FILE__)."/Error.php");
- exit;
- Prado::log("LDatabase master config file not found - aborting init!", 5, "Lithron.LDatabase");
- 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.");
- return;
- }*/
-
- // TODO - harcoded logging
- $_logdir = Prado::getPathOfAlias("Application").'/runtime/propel/propel.log';
- $this->_config['log'] = array ('ident' => 'propellog', 'level' => '7', 'name' => $_logdir, 'type' => 'file');
- $_cpd = & $this->_config['propel']['datasources'];
- $xmlObjs = $config->getElementsByTagName('database');
- foreach ($xmlObjs AS $xmlObj)
- {
- $attrs = $xmlObj->getAttributes();
- $connection["phptype"] = $attrs->itemAt("adapter");
- $connection["hostspec"] = $attrs->itemAt("hostspec");
- $connection["database"] = $attrs->itemAt("database");
- $connection["username"] = $attrs->itemAt("username");
- $connection["password"] = $attrs->itemAt("password");
- $_cpd[$attrs->itemAt("id")]['adapter'] = $attrs->itemAt("adapter");
- $_cpd[$attrs->itemAt("id")]['connection'] = $connection;
- }
- $_cpd['default'] = Prado :: getApplication()->getMode();
-
- #$this->_config = include ($masterConfigFile);
-
- $xmlObjs = $config->getElementsByTagName('scan');
- $pkgConfigs = array();
- foreach ($xmlObjs AS $xmlObj)
- {
- $attrs = $xmlObj->getAttributes();
- $pkgConfigs = array_merge($pkgConfigs,$this->findConfigurations($attrs->itemAt("namespace")));
- }
- if (is_array($pkgConfigs))
- foreach ($pkgConfigs AS $config)
- $this->_config = LDatabaseConfiguration :: addConfiguration($this->_config, $config);
- Prado :: trace("Propel config files parsed & merged", 'Lithron.LDatabaseConfiguration');
- #var_dump($this->_config);
-
- }
-
- /**
- * Reads package configs
- */
- private function findConfigurations($alias)
- {
- #Prado :: trace("Searching for LPropel configs ...", 'Lithron.LPropel');
- $pkgConfigs = array ();
- $baseDir = Prado :: getPathOfNamespace($alias);
- if (is_dir($baseDir))
- {
- $scan = scandir($baseDir);
- foreach ($scan AS $item)
- {
- $pkgDir = $baseDir."/".$item;
- if (is_dir($pkgDir))
- {
- $lPropelDir = $pkgDir."/db/propel";
- if (is_dir($lPropelDir))
- {
- $scan = scandir($lPropelDir);
- foreach ($scan AS $item)
- {
- if (substr($item, -9) == "-conf.php")
- {
- $pkgConfigs[$item] = include_once ($lPropelDir."/".$item);
- #Prado :: trace("found conf '$item' in '$alias'", 'Lithron.LDatabaseConfiguration');
- }
- }
- }
-
- }
- }
- }
- return $pkgConfigs;
-
- }
-
- /**
- * Adds a package config to the config
- */
- private function addConfiguration($masterConfig, $config)
- {
- $sources = & $config['propel']['datasources'];
- foreach ($sources AS $id => $datasource)
- {
- if ($id == "value")
- continue;
- if ($id == "default")
- {
- $_ds = trim($datasource);
- if (isset ($sources[$datasource]))
- {
- continue;
- } else
- if (!empty ($_ds) && !isset ($sources[$datasource]))
- {
- $defaultKey = $masterConfig['propel']['datasources']['default'];
- Prado :: trace("Config for '$datasource' not found, assuming '$defaultKey'", 'Lithron.LDatabaseConfiguration');
- $masterConfig['propel']['datasources'][$datasource] = $masterConfig['propel']['datasources'][$defaultKey];
- continue;
- }
- }
- Prado :: trace("Config for '$id' added.", 'Lithron.LDatabaseConfiguration');
- $masterConfig['propel']['datasources'][$id] = $datasource;
-
- # echo "<hr>$id";
-
- }
- #var_dump($masterConfig);
-
- return $masterConfig;
- }
-
- /**
- * Returns a DSN from the config
- */
- public function getDsn($name)
- {
- $config = $this->_config;
- $c = $config['propel']['datasources'][$name]['connection'];
-
- if ($c['username'])
- {
- $prefix = $c['username'].":".$c['password']."@";
- } else
- {
- $prefix = "";
- }
- $dsn = $c['phptype']."://".$prefix."".$c['hostspec']."/".$c['database'];
- return $dsn;
- }
-
- }